finally it seems to work

This commit is contained in:
Stepan Usatiuk
2023-12-30 22:52:04 +01:00
parent 65e50d9e02
commit 52f26c15db
10 changed files with 112 additions and 8 deletions

2
server/.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
build
.gradle

View File

@@ -44,3 +44,11 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}
jar {
manifest {
attributes(
'Main-Class': 'com.usatiuk.tjv.y.server.ServerApplication'
)
}
}

View File

@@ -2,9 +2,13 @@ package com.usatiuk.tjv.y.server;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
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;
@@ -12,6 +16,9 @@ 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;
@@ -21,9 +28,9 @@ public class WebConfig implements WebMvcConfigurer {
static class AppResourceResolver implements ResourceResolver {
@Override
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
ClassPathResource res = new ClassPathResource("/app/" + requestPath);
FileSystemResource res = new FileSystemResource("/usr/src/app/client/dist/" + requestPath);
if (res.exists()) return res;
ClassPathResource indexRes = new ClassPathResource("/app/index.html");
FileSystemResource indexRes = new FileSystemResource("/usr/src/app/client/dist/index.html");
if (indexRes.exists()) return indexRes;
return null;
}
@@ -34,9 +41,24 @@ public class WebConfig implements WebMvcConfigurer {
}
}
@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/", "/app/index.html");
registry.addRedirectViewController("/", "/app");
}
@Override

View File

@@ -63,6 +63,7 @@ public class WebSecurityConfig {
.requestMatchers(mvc.pattern("/swagger-ui*/**")).permitAll()
.requestMatchers(mvc.pattern("/v3/**")).permitAll()
.requestMatchers(mvc.pattern("/error")).permitAll()
.requestMatchers(mvc.pattern("/")).permitAll()
.anyRequest().hasAuthority(UserRoles.ROLE_USER.name()))
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class)