Search code examples
angularjswebmustachemicroservicesmsf4j

MSF4J: Serving static content


Can MSF4J application serve static content without using the Mustache template engine. I have developed a REST service which will be consumed by an already developed angular web app. Now I need to package the same angular app with the micro service so it will render in the browser and will consume the service via ajax calls.


Solution

  • MSF4J does not directly support serving static content. From your question what I understood is that you want to point the MSF4J server to a directory and serve resources in that directory by their relative path or something similar. In this case what you can do is to write an MSF4J service method with a wildcard path and serve the static content that matches the path of the request.

    @Path("/")
    public class FileServer {
    
        private static final String BASE_PATH = "/your/www/dir";
    
        @GET
        @Path("/**")
        public Response serveFiles(@Context Request request) {
            String uri = request.getUri();
            System.out.println("Requested: " + uri);
            File file = Paths.get(BASE_PATH, uri).toFile();
            if (file.exists()) {
                return Response.ok().entity(file).build();
            } else {
                return Response.status(404).entity("<h1>Not Found</h1>").build();
            }
        }
    }