0

In simple words, I want following routes and resources to be served in the same order, from a micro-service

  • If spring cloud routes matches, then redirect to particular micro service
  • If matches a static resource, then serve the resource
  • If any of the above doesn't match, serve the index.html

In the web application I'm working on there are multiple micro-services that's connected through frontend-gateway service (spring cloud). At the moment, the React front-end static files are served using the frontend-gateway service (using spring boot embedded tomcat as static files).

The project follows a weird approach for some reason but React application static files are being copied to build directory at compile time of frontend-gateway service.

build/
├─ resources/
│  ├─ main/
│  │  ├─ static/
│  │  │  ├─ index.html
│  │  │  ├─ bundle.js
│  │  │  ├─ new_folder/
│  │  │  │  ├─ logo.png
│  │  │  │  ├─ search_icon.svg

At the moment following routes and resources are defined.

Routes

  @Bean
  public RouterFunction<ServerResponse> htmlRemoteAppointmentRouter(
      @Value("classpath:/static/index.html")
      Resource html) {
    return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
        .andRoute(GET("/home/{*path}"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html))
        .andRoute(GET("/user/{*path}"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(html));
  }

Resources

  @Bean
  public RouterFunction<ServerResponse> resourceRouter() {
    return RouterFunctions
        .resources("/resources/**", new ClassPathResource("static/resources/"));
  }

Additionally, routes for micro services are defined in the application.yml

spring:
  cloud:
    service-registry:
      auto-registration: 
        enabled: false
    gateway:
      routes:
      - id: auth-service
        uri: http://127.0.0.1:18100

With all this, when a user navigates to host:port/user for instance, the web application will show the user details. However, something like host:port/doesntexist would result in 404.

I'm not quite sure how to achieve all the listed requirement at the top.

0

Browse other questions tagged or ask your own question.