mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 02:27:49 +01:00
prettier spa controllers
This commit is contained in:
@@ -21,6 +21,7 @@ RUN mkdir -p client/dist
|
|||||||
COPY --from=client /usr/src/app/client/dist ./client/dist
|
COPY --from=client /usr/src/app/client/dist ./client/dist
|
||||||
|
|
||||||
ENV spring_profiles_active=prod
|
ENV spring_profiles_active=prod
|
||||||
|
ENV webdatadir=/usr/src/app/client/dist/
|
||||||
|
|
||||||
COPY ./dockerentry.sh .
|
COPY ./dockerentry.sh .
|
||||||
|
|
||||||
|
|||||||
@@ -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<? extends Resource> 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<? extends Resource> 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());
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<? extends Resource> locations, ResourceResolverChain chain) {
|
||||||
|
return resourceResourceImpl(requestPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
|
||||||
|
if (resourceResourceImpl(resourcePath) != null)
|
||||||
|
return resourcePath;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user