Skip to content

Commit

Permalink
[api] Adapt index to be generated / cleanup static resource handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-julien committed Dec 7, 2023
1 parent 5f14c7f commit 71c156d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
38 changes: 3 additions & 35 deletions openex-api/src/main/java/io/openex/config/MvcConfig.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
package io.openex.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.AbstractResourceResolver;
import org.springframework.web.servlet.resource.EncodedResourceResolver;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolverChain;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;


class ReactIndexResourceResolver extends AbstractResourceResolver {

@Override
protected Resource resolveResourceInternal(HttpServletRequest request, @NotNull String requestPath,
@NotNull List<? extends Resource> locations,
@NotNull ResourceResolverChain chain) {
return new ClassPathResource("/build/index.html");
}

@Override
protected String resolveUrlPathInternal(@NotNull String resourceUrlPath,
@NotNull List<? extends Resource> locations,
@NotNull ResourceResolverChain chain) {
return chain.resolveUrlPath(resourceUrlPath, locations);
}
}

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
Expand All @@ -59,7 +35,6 @@ public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter()
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
//https://springdoc.org/#why-am-i-getting-an-error-swagger-ui-unable-to-render-definition-when-overriding-the-default-spring-registered-httpmessageconverter
messageConverters.add(new ByteArrayHttpMessageConverter());

messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(customJackson2HttpMessageConverter());
}
Expand All @@ -69,20 +44,13 @@ private void addPathStaticResolver(ResourceHandlerRegistry registry, String patt
.addResourceHandler(pattern)
.addResourceLocations(location)
.setCachePeriod(CACHE_PERIOD)
.resourceChain(true)
.resourceChain(false)
.addResolver(new EncodedResourceResolver())
.addResolver(new PathResourceResolver());
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Specific case of react index
registry.addResourceHandler("*")
.addResourceLocations("classpath:/build/")
.setCachePeriod(CACHE_PERIOD)
.resourceChain(true)
.addResolver(new EncodedResourceResolver())
.addResolver(new ReactIndexResourceResolver());
public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
// React statics
addPathStaticResolver(registry, "/static/**", "classpath:/build/static/");
// Specific application images
Expand Down
45 changes: 45 additions & 0 deletions openex-api/src/main/java/io/openex/rest/HomeApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.openex.rest;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;

import static java.nio.charset.StandardCharsets.UTF_8;

@RestController
public class HomeApi {

private static String readResourceAsString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@GetMapping(path = "/**", produces = MediaType.TEXT_HTML_VALUE)
public ResponseEntity<String> home() {
ClassPathResource classPathResource = new ClassPathResource("/build/index.html");
String index = readResourceAsString(classPathResource);
String basePath = ""; // TODO Add this in configuration
String newIndex = index.
replaceAll("%APP_TITLE%", "OpenEx - Crisis Exercises and Adversary Simulation Platform").
replaceAll("%APP_DESCRIPTION%", "OpenEx is an open source platform allowing organizations to plan, schedule and conduct crisis exercises as well as adversary simulation campaign.").
replaceAll("%APP_FAVICON%", basePath + "/static/ext/favicon.png").
replaceAll("%APP_MANIFEST%", basePath + "/static/ext/manifest.json").
replaceAll("%BASE_PATH%", basePath);
return ResponseEntity.ok()
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
.body(newIndex);
}
}

0 comments on commit 71c156d

Please sign in to comment.