Skip to content

Frontend customization #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gov.nih.ncats.gsrsfrontend.config;

import java.io.IOException;
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.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
Expand All @@ -10,30 +12,40 @@
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
@Value("${route.prefix:ginas/app/beta/}")
private String prefix = "ginas/app/beta/";


//This is so all the front end refresh/ non-existing files default back to index.html
@Value("${gsrs.frontend.config.dir:classpath:/static/assets/data}")
private String frontendConfigDir = "classpath:/static/assets/data";

private final Resource indexPage = new ClassPathResource("/static/index.html");

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
if(resourcePath.startsWith(prefix)){
resourcePath = resourcePath.substring(prefix.length());
resourcePath = resourcePath.substring(prefix.length());
}
Resource requestedResource;
if (!frontendConfigDir.startsWith("classpath:/")) {
if (resourcePath.startsWith("assets/data/")
|| resourcePath.startsWith("assets/images/")
|| resourcePath.endsWith("styles.custom.css")) {
requestedResource = new FileSystemResource(frontendConfigDir + "/" + resourcePath.substring(resourcePath.lastIndexOf("/")+1));
if (requestedResource.exists() && requestedResource.isReadable()) {
return requestedResource;
}
}
}
Resource requestedResource = location.createRelative(resourcePath);

return (requestedResource.exists() && requestedResource.isReadable()) ? requestedResource
: new ClassPathResource("/static/index.html");
requestedResource = location.createRelative(resourcePath);
return (requestedResource.exists() && requestedResource.isReadable()) ? requestedResource : indexPage;
}
});
}
Expand Down