Skip to content
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

Support multiple calls to staticFileLocation #568

Open
jylin opened this issue May 26, 2016 · 6 comments
Open

Support multiple calls to staticFileLocation #568

jylin opened this issue May 26, 2016 · 6 comments

Comments

@jylin
Copy link

jylin commented May 26, 2016

It seems you currently can't call staticFileLocation twice (I didn't look at the source code, but it looks like the second call is ignored?).

I'd like to be able to have a static assets folder, but also use webjars. However, something like this doesn't seem to work:
staticFileLocation("/public");
staticFileLocation("/META-INF/resources");

It looks like I can't access the files in "/META-INF/resources", but if I comment out the first line, then those files are successfully loaded.

@vacax
Copy link

vacax commented Jun 6, 2016

this issue is important.

@pgrisafi
Copy link

+1
New to Spark. As a webjars fan, this is important.
A quick and dirty hack I made, using a custom route and a guava cache
Disclaimer: It works for me, maybe not for you. It does not handle multiple nested folders, but it is easy to extend to do so.

private static final LoadingCache<String, String> WEBJARS_CACHE = CacheBuilder.newBuilder()
           .maximumSize(1000)
           .expireAfterWrite(365, TimeUnit.DAYS)
           .build(
               new CacheLoader<String, String>() {
                   @Override
                 public String load(String fullPath) throws IOException {
                     InputStream inputStream = Application.class.getClassLoader().getResourceAsStream(fullPath);
                    return IOUtils.toString(inputStream);
                 }
               });

public static void main(String[] args) {
    staticFiles.location("public");
    get("/hello", (req, res) -> "Hello World");

    get("/webjars/:library/:version/:file",  (req, res) -> {
        String library = req.params("library");
        String version = req.params("version");
        String file = req.params("file");
        if(file.endsWith(".js")){
            res.type("application/javascript");   
        }
        if(file.endsWith(".css")){
            res.type("text/css");   
        }
        String fullPath = "META-INF/resources/webjars/" + library + "/" + version +"/" + file;
        return WEBJARS_CACHE.get(fullPath);
    });
}

I can access files using links like

<script src="/webjars/angularjs/1.5.6/angular.min.js"></script>
<link rel="stylesheet" href="/webjars/angular-material/1.1.0-rc4/angular-material.min.css">

@N0odlez
Copy link
Contributor

N0odlez commented Jul 9, 2016

I'm looking to implement this for you but could you give me a bit more detail in what the goal is?

@drxaos
Copy link

drxaos commented Sep 15, 2016

I have multiple jars with resource folder and I had to do some reflection magic to search static resources in all jars.
https://github.com/drxaos/coins/blob/master/coins-spark/src/main/java/com/github/drxaos/coins/spark/config/Http.java#L78

@johnnybigoode-zz
Copy link

I'm late to the party eh?
@pgrisafi I think your case can be solved using staticFileLocation("/META-INF/resources");
That can give you access to all webjars in a single staticFileLocation.

@drxaos Wouldn't be easier to just copy the files to the appropriate folder? Maven has a resource plug in that solves this https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

@perwendel Could you shed some light on this? I personally don't think that any service should have more static resources, but I did notice that the StaticFilesConfiguration.java file has a private List<AbstractResourceHandler> staticResourceHandlers = null; on line 50. On method consumeWithFileResourceHandlers on line 85 we seem to iterate over all registered resourceHandlers.

Maybe I'm being naive, but it's not that hard to add this.

@buckelieg
Copy link

I would prefer to have an array of Strings as the arguments of staticFileLocation method where I can enlist all ROOT directories for static files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants