From 01a20644a953b20c348b591246b33f4c1b12d637 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Tue, 2 Jan 2024 21:45:37 +0100 Subject: [PATCH] prettier spa controllers --- Dockerfile | 1 + .../com/usatiuk/tjv/y/server/WebConfig.java | 72 ------------------- .../spasupport/AppResourceResolver.java | 49 +++++++++++++ .../y/server/spasupport/AppRootContoller.java | 35 +++++++++ .../tjv/y/server/spasupport/WebConfig.java | 31 ++++++++ 5 files changed, 116 insertions(+), 72 deletions(-) delete mode 100644 server/src/main/java/com/usatiuk/tjv/y/server/WebConfig.java create mode 100644 server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppResourceResolver.java create mode 100644 server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppRootContoller.java create mode 100644 server/src/main/java/com/usatiuk/tjv/y/server/spasupport/WebConfig.java diff --git a/Dockerfile b/Dockerfile index 073b556..041fc52 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,7 @@ RUN mkdir -p client/dist COPY --from=client /usr/src/app/client/dist ./client/dist ENV spring_profiles_active=prod +ENV webdatadir=/usr/src/app/client/dist/ COPY ./dockerentry.sh . diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/WebConfig.java b/server/src/main/java/com/usatiuk/tjv/y/server/WebConfig.java deleted file mode 100644 index b3e97d0..0000000 --- a/server/src/main/java/com/usatiuk/tjv/y/server/WebConfig.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.usatiuk.tjv.y.server; - -import jakarta.servlet.http.HttpServletRequest; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.http.CacheControl; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.resource.ResourceResolver; -import org.springframework.web.servlet.resource.ResourceResolverChain; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.time.Duration; -import java.util.List; - -@Configuration -@EnableWebMvc -public class WebConfig implements WebMvcConfigurer { - static class AppResourceResolver implements ResourceResolver { - @Override - public Resource resolveResource(HttpServletRequest request, String requestPath, List locations, ResourceResolverChain chain) { - FileSystemResource res = new FileSystemResource("/usr/src/app/client/dist/" + requestPath); - if (res.exists()) return res; - FileSystemResource indexRes = new FileSystemResource("/usr/src/app/client/dist/index.html"); - if (indexRes.exists()) return indexRes; - return null; - } - - @Override - public String resolveUrlPath(String resourcePath, List locations, ResourceResolverChain chain) { - return null; - } - } - - - @RestController - @RequestMapping(value = "/app", produces = MediaType.TEXT_HTML_VALUE) - static class AppRootContoller { - @GetMapping - public String get() throws IOException { - return Files.readString(Path.of("/usr/src/app/client/dist/index.html")); - } - - @GetMapping("/") - public String getSlash() throws IOException { - return Files.readString(Path.of("/usr/src/app/client/dist/index.html")); - } - } - - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addRedirectViewController("/", "/app"); - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/app/**") - .setCacheControl(CacheControl.maxAge(Duration.ofDays(365))) - .resourceChain(true) - .addResolver(new AppResourceResolver()); - - } -} \ No newline at end of file diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppResourceResolver.java b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppResourceResolver.java new file mode 100644 index 0000000..fda7177 --- /dev/null +++ b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppResourceResolver.java @@ -0,0 +1,49 @@ +package com.usatiuk.tjv.y.server.spasupport; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.resource.ResourceResolver; +import org.springframework.web.servlet.resource.ResourceResolverChain; + +import java.io.File; +import java.nio.file.Paths; +import java.util.List; + +@Component +@Profile("prod") +public class AppResourceResolver implements ResourceResolver { + private final String rootPath; + + AppResourceResolver(@Value("${webdatadir}") String rootPath) { + this.rootPath = rootPath; + + var indexFile = new File(rootPath, "index.html"); + if (!indexFile.exists() || indexFile.isDirectory()) + throw new IllegalArgumentException("index.html doesn't exist!"); + + } + + private Resource resourceResourceImpl(String requestPath) { + FileSystemResource res = new FileSystemResource(Paths.get(rootPath, requestPath)); + if (res.exists()) return res; + FileSystemResource indexRes = new FileSystemResource(Paths.get(rootPath, "index.html")); + if (indexRes.exists()) return indexRes; + return null; + } + + @Override + public Resource resolveResource(HttpServletRequest request, String requestPath, List locations, ResourceResolverChain chain) { + return resourceResourceImpl(requestPath); + } + + @Override + public String resolveUrlPath(String resourcePath, List locations, ResourceResolverChain chain) { + if (resourceResourceImpl(resourcePath) != null) + return resourcePath; + return null; + } +} diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppRootContoller.java b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppRootContoller.java new file mode 100644 index 0000000..2b637e7 --- /dev/null +++ b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/AppRootContoller.java @@ -0,0 +1,35 @@ +package com.usatiuk.tjv.y.server.spasupport; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +@RestController +@RequestMapping(value = "/app", produces = MediaType.TEXT_HTML_VALUE) +@Profile("prod") +class AppRootContoller { + private final File indexFile; + + AppRootContoller(@Value("${webdatadir}") String rootPath) { + this.indexFile = new File(rootPath, "index.html"); + if (!this.indexFile.exists() || this.indexFile.isDirectory()) + throw new IllegalArgumentException("index.html doesn't exist!"); + } + + @GetMapping + public String get() throws IOException { + return Files.readString(indexFile.toPath()); + } + + @GetMapping("/") + public String getSlash() throws IOException { + return Files.readString(indexFile.toPath()); + } +} diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/WebConfig.java b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/WebConfig.java new file mode 100644 index 0000000..c95df4d --- /dev/null +++ b/server/src/main/java/com/usatiuk/tjv/y/server/spasupport/WebConfig.java @@ -0,0 +1,31 @@ +package com.usatiuk.tjv.y.server.spasupport; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +@Profile("prod") +public class WebConfig implements WebMvcConfigurer { + private final AppResourceResolver appResourceResolver; + + public WebConfig(AppResourceResolver appResourceResolver) { + this.appResourceResolver = appResourceResolver; + } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addRedirectViewController("/", "/app"); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/app/**") + .resourceChain(true) + .addResolver(appResourceResolver); + } +} \ No newline at end of file