enable new Angular application
Remove the controller method that returned the VueJS index page. Add resource handlers that redirect to the Angular application. I added two implementations, but activated only one. At the moment I am not sure which solution is the better. We keep both so that we can easily switch if need arises.
This commit is contained in:
@@ -7,10 +7,8 @@ import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
@@ -49,14 +47,12 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
@@ -92,17 +88,6 @@ public class PdbController implements HardcodedValues, PropertyKeys {
|
||||
this.plotter = plotter;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public ModelAndView index() {
|
||||
final String view = "main";
|
||||
final Map<String, Object> model = new HashMap<>();
|
||||
// model.put("oldestValue",
|
||||
// LocalDateTime.now().minusDays(7).format(DATE_FORMAT_BEGIN));
|
||||
// model.put("latestValue", LocalDateTime.now().format(DATE_FORMAT_END));
|
||||
model.put("isProduction", modeProduction);
|
||||
return new ModelAndView(view, model);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/plots", //
|
||||
method = RequestMethod.GET, //
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE, //
|
||||
@@ -227,7 +212,7 @@ public class PdbController implements HardcodedValues, PropertyKeys {
|
||||
@ResponseBody
|
||||
AutocompleteResponse autocomplete(@RequestParam(name = "query") final String query,
|
||||
@RequestParam(name = "caretIndex") final int caretIndex,
|
||||
@RequestParam(name = "resultMode", defaultValue = "CUT_AT_DOT") ResultMode resultMode) {
|
||||
@RequestParam(name = "resultMode", defaultValue = "CUT_AT_DOT") final ResultMode resultMode) {
|
||||
|
||||
// TODO get date range from UI
|
||||
final DateTimeRange dateRange = DateTimeRange.max();
|
||||
@@ -249,12 +234,13 @@ public class PdbController implements HardcodedValues, PropertyKeys {
|
||||
}
|
||||
|
||||
private List<Proposal> exampleProposals() {
|
||||
List<Proposal> result = new ArrayList<Proposal>();
|
||||
final List<Proposal> result = new ArrayList<Proposal>();
|
||||
|
||||
if (queryExamples.length() > 0) {
|
||||
final String[] exampleQueries = queryExamples.split(Pattern.quote(";"));
|
||||
for (String example : exampleQueries) {
|
||||
Proposal p = new Proposal(" Example: " + example, example, true, example + " ", example.length() + 1);
|
||||
for (final String example : exampleQueries) {
|
||||
final Proposal p = new Proposal(" Example: " + example, example, true, example + " ",
|
||||
example.length() + 1);
|
||||
result.add(p);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package org.lucares.pdbui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
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.PathResourceResolver;
|
||||
|
||||
@Configuration
|
||||
public class WebConfiguration implements WebMvcConfigurer, HardcodedValues, PropertyKeys {
|
||||
@@ -19,9 +24,65 @@ public class WebConfiguration implements WebMvcConfigurer, HardcodedValues, Prop
|
||||
@Override
|
||||
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
|
||||
|
||||
addResourceHandlerForPlottedImages(registry);
|
||||
|
||||
// addResourceHandlerForAngular(registry);
|
||||
}
|
||||
|
||||
private void addResourceHandlerForPlottedImages(final ResourceHandlerRegistry registry) {
|
||||
final String pathPattern = "/" + WEB_IMAGE_OUTPUT_PATH + "/**";
|
||||
final String resourceLocation = "file:" + Paths.get(outputDir).toAbsolutePath() + "/";
|
||||
|
||||
registry.addResourceHandler(pathPattern).addResourceLocations(resourceLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is one way to make Angular and SpringBoot play together. The other
|
||||
* described in {@link #addViewControllers(ViewControllerRegistry)}.
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private void addResourceHandlerForAngular(final ResourceHandlerRegistry registry) {
|
||||
// Redirect all to Angular for all non existing resources.
|
||||
// This way Angular's routing & navigation (https://angular.io/guide/router) can
|
||||
// work. Without it SpringBoot would only show 404 pages for all of Angular's
|
||||
// pages, because the request would not even be send to Angular.
|
||||
//
|
||||
// Technically we are answering all requests for which SpringBoot does not find
|
||||
// a resource with Angular's index.html. The Angular will then use its routing
|
||||
// to determine which sub-page to show.
|
||||
//
|
||||
// This makes Angular also responsible for all 404 pages.
|
||||
registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/").resourceChain(true)
|
||||
.addResolver(new PathResourceResolver() {
|
||||
@Override
|
||||
protected Resource getResource(final String resourcePath, final Resource location)
|
||||
throws IOException {
|
||||
final Resource requestedResource = location.createRelative(resourcePath);
|
||||
|
||||
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
|
||||
: new ClassPathResource("/resources/index.html");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This is one way to make Angular and SpringBoot play together. The other
|
||||
* described in {@link #addResourceHandlerForAngular(ResourceHandlerRegistry)}
|
||||
*/
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
|
||||
// Redirect requests to Angular.
|
||||
// This works by returning Angular's index.html for requests that should be
|
||||
// handled by Angular.
|
||||
//
|
||||
// Using this solution SpringBoot is responsible for 404s.
|
||||
|
||||
registry.addViewController("/").setViewName("forward:/index.html");
|
||||
registry.addViewController("/vis").setViewName("forward:/index.html");
|
||||
registry.addViewController("/upload").setViewName("forward:/index.html");
|
||||
registry.addViewController("/help").setViewName("forward:/index.html");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user