Specify a specific URL for static files using the Spark Framework

I use Spark to serve a webpage. For static files, I initialize Spark as here ::

So, I have this structure:

/src/main/resources/public/ |-- foo/ |-- css/ | |-- bootstrap.css |-- js/ | ... |-- img/ ... 

I made a foo folder to do the trick because my webpage is under /foo url..like this:

http://www.example.com/foo/index

So, my static files are loaded as follows:

http://www.example.com/foo/css/bootstrap.css

Now I want to have this path variable. Since I have different environments and, for example, if I deploy this application in a different domain, I want this to be:

http://www.example2.com/superfoo/css/bootstrap.css

But for this I have to change the release and change the folder ...

For controllers, I made it easy:

Example:

  Spark.get(this.appBasePath + "/users", (request, response) -> { return this.getUsersView(request); }, new FreeMarkerEngine()); 

this.appBasePath comes from the configuration that loads when the environment is this.appBasePath .

So I ask you to set the programmatic URL of static files without creating any folder. Is there any way to achieve this?

+7
java spark-java
source share
1 answer

I ended up working on this issue by creating a route to get all of my static files. It is possible that there are ways to simplify this right into sparks, but writing this code took less time than understanding the details of spark.resource .

First, I define some helper functions to allow me to iterate over all the files in a specific resource directory (Java 8 required):

 /** * Find all resources within a particular resource directory * @param root base resource directory. Should omit the leading / (eg "" for all resources) * @param fn Function called for each resource with a Path corresponding to root and a relative path to the resource. * @throws URISyntaxException */ public static void findResources(String root,BiConsumer<Path,Path> fn) throws URISyntaxException { ClassLoader cl = Main.class.getClassLoader(); URL url = cl.getResource(root); assert "file".equals(url.getProtocol()); logger.debug("Static files loaded from {}",root); Path p = Paths.get(url.toURI()); findAllFiles(p, (path) -> fn.accept(p,p.relativize(path)) ); } /** * Recursively search over a directory, running the specified function for every regular file. * @param root Root directory * @param fn Function that gets passed a Path for each regular file */ private static void findAllFiles(Path root, Consumer<Path> fn) { try( DirectoryStream<Path> directoryStream = Files.newDirectoryStream(root)) { for(Path path : directoryStream) { if(Files.isDirectory(path)) { findAllFiles(path, fn); } else { fn.accept(path); } } } catch (IOException ex) {} } 

Then I use this to define a new GET route for each file.

 String webroot = "/server1"; findResources("static", (root,path) -> { String route = webroot+"/"+path; String resourcePath = "/static/"+path.toString(); logger.debug("Mapping {} to {}",route, resourcePath); get(webroot+"/"+path, (req,res) -> { Files.copy(root.resolve(path), res.raw().getOutputStream()); AbstractFileResolvingResource resource = new ExternalResource(resourcePath); String contentType = MimeType.fromResource(resource); res.type(contentType ); return ""; } ); }); 
+3
source share

All Articles