diff --git a/src/main/java/org/frankframework/application/Application.java b/src/main/java/org/frankframework/application/Application.java index 128c0e7..ed61aff 100644 --- a/src/main/java/org/frankframework/application/Application.java +++ b/src/main/java/org/frankframework/application/Application.java @@ -2,6 +2,15 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.ClassPathResource; +import org.springframework.web.servlet.function.RequestPredicate; +import org.springframework.web.servlet.function.RouterFunction; +import org.springframework.web.servlet.function.ServerResponse; + +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; @SpringBootApplication public class Application { @@ -11,9 +20,21 @@ public static void main(String[] args) { app.run(args); } - - public static SpringApplication configureApplication() { return new SpringApplication(Application.class); } + + /** + * This is a custom router function to accommodate to our single page application that we serve from this spring boot backend as well. + * This RouterFunction will make sure that we serve `frontend/index.html` whenever the path does not start with `/api/`, is not `/error` and does + * not have a path extension (to exclude static resources). + * + * @see Spring framework issue 27257 for more details. + */ + @Bean + RouterFunction spaRouter() { + ClassPathResource index = new ClassPathResource("frontend/index.html"); + RequestPredicate spaPredicate = path("/api/**").or(path("/error")).or(pathExtension(extension -> !extension.isBlank())).negate(); + return route().resource(spaPredicate, index).build(); + } } diff --git a/src/main/java/org/frankframework/application/endpoints/FrontendForwardController.java b/src/main/java/org/frankframework/application/endpoints/FrontendForwardController.java deleted file mode 100644 index a060949..0000000 --- a/src/main/java/org/frankframework/application/endpoints/FrontendForwardController.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.frankframework.application.endpoints; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; - -@Controller -public class FrontendForwardController { - - @GetMapping(value = "/{path:[^\\.]*}") - public String forward(@PathVariable String path) { - return "forward:/index.html"; - } -}