diff --git a/.gitignore b/.gitignore index 8f399a1..82b99c3 100644 --- a/.gitignore +++ b/.gitignore @@ -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/* diff --git a/README.md b/README.md index 909d3a7..20e7d4c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.gradle b/build.gradle index fe85b67..3ea4d0d 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } } @@ -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() @@ -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) { diff --git a/client/src/main.tsx b/client/src/main.tsx index cdd044b..810bf41 100644 --- a/client/src/main.tsx +++ b/client/src/main.tsx @@ -10,7 +10,7 @@ import './index.scss'; ReactDOM.createRoot(document.getElementById('root')!).render( - + diff --git a/client/vite.config.ts b/client/vite.config.ts index 338dae5..42493c1 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -4,7 +4,7 @@ import tsconfigPaths from 'vite-tsconfig-paths'; // https://vitejs.dev/config/ export default defineConfig({ - base: "", + base: "/react/", build: { sourcemap: true, }, diff --git a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java index d19fa9a..347ec28 100644 --- a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java +++ b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -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 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 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(); + } + } + diff --git a/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java b/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java index a21e280..9224015 100644 --- a/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java +++ b/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java @@ -22,7 +22,7 @@ @Controller class WelcomeController { - @GetMapping("/welcome") + @GetMapping("/") public String welcome() { return "welcome"; }