Skip to content

Commit

Permalink
Move react app to /react and legacy app entrypoint back to /
Browse files Browse the repository at this point in the history
  • Loading branch information
dsellarsnr committed Oct 14, 2024
1 parent 31564ed commit 2e442c7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ logs/
/client/node_modules
.idea/

src/main/resources/static/assets
src/main/resources/static/index.html
src/main/resources/static/react

.kotlin/*

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ NEW_RELIC_LICENSE_KEY=12345 ./gradlew bootRun
Substitute 12345 with your New Relic license key.

You can then access petclinic here:
- react: http://localhost:8081
- legacy app: http://localhost:8081/welcome
- react: http://localhost:8081/react
- legacy app: http://localhost:8081/

The main page will have some links that exercise auto and manual instrumentation in different modes.

Expand Down
12 changes: 5 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ tasks.register('buildFrontEnd', DefaultTask) {
commandLine cmd, 'run', 'build'
}

delete 'src/main/resources/static/assets'
delete 'src/main/resources/static/index.html'
mkdir 'src/main/resources/static/assets'
delete 'src/main/resources/static/react'

copy {
from "client/dist"
into "src/main/resources/static"
into "src/main/resources/static/react"
}
}

Expand All @@ -93,7 +91,7 @@ tasks.register('buildFrontEnd', DefaultTask) {
}

def templateJavaScript = file("$rootDir/browserMonitoringTemplate.js")
def indexFile = file("$rootDir/src/main/resources/static/index.html")
def indexFile = file("$rootDir/src/main/resources/static/react/index.html")

def indexFileContents = indexFile.getText()
def templateContents = templateJavaScript.getText()
Expand All @@ -110,8 +108,8 @@ tasks.register('buildFrontEnd', DefaultTask) {
indexFile.write(newIndexFileContent)
}

outputs.file('src/main/resources/static/index.html')
outputs.dir('src/main/resources/static/assets')
outputs.file('src/main/resources/static/react/index.html')
outputs.dir('src/main/resources/static/react/assets')
}

tasks.register('downloadNewRelicAgent', DefaultTask) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import './index.scss';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<BrowserRouter>
<BrowserRouter basename={"/react"}>
<ApiProvider api={ownersApi}>
<App />
</ApiProvider>
Expand Down
2 changes: 1 addition & 1 deletion client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tsconfigPaths from 'vite-tsconfig-paths';

// https://vitejs.dev/config/
export default defineConfig({
base: "",
base: "/react/",
build: {
sourcemap: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,53 @@

package org.springframework.samples.petclinic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.function.RequestPredicate;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.Arrays;
import java.util.List;

import static org.springframework.web.servlet.function.RequestPredicates.path;
import static org.springframework.web.servlet.function.RequestPredicates.pathExtension;
import static org.springframework.web.servlet.function.RouterFunctions.route;

/**
* PetClinic Spring Boot Application.
*
* @author Dave Syer
*
*/
@SpringBootApplication
@ImportRuntimeHints(PetClinicRuntimeHints.class)
@EnableScheduling
public class PetClinicApplication {
private static final Logger logger = LoggerFactory.getLogger(PetClinicApplication.class);

public static void main(String[] args) {
SpringApplication.run(PetClinicApplication.class, args);
}

@Bean
RouterFunction<ServerResponse> spaRouter() {
logger.info("Setting up SPA router");
ClassPathResource index = new ClassPathResource("static/react/index.html");
if (!index.exists()) {
logger.error("No react app found!!");
return null;
}
List<String> extensions = Arrays.asList("js", "css", "svg", "ico", "png", "jpg", "gif", "eot", "ttf",
"woff", "woff2");
RequestPredicate spaPredicate = path("/react/**").and(pathExtension(extensions::contains).negate());
return route().resource(spaPredicate, index).build();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Controller
class WelcomeController {

@GetMapping("/welcome")
@GetMapping("/")
public String welcome() {
return "welcome";
}
Expand Down

0 comments on commit 2e442c7

Please sign in to comment.