perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

[FeatureRequest] Url for static resources #1039

Closed vlio20 closed 5 years ago

vlio20 commented 5 years ago

Hi, Fom the documentation I see that the static files are read from the resources folder:

// root is 'src/main/resources', so put files in 'src/main/resources/public' staticFiles.location("/public"); // Static files

And would be served when the following url would be provided:

http://{host}:{port}/css/style.css

My question is: is it possible to place the files in the resources/public but access it with a different url. For example: http://{host}:{port}/MyApp/site/static

Note: putting nested directories will result putting the index.html file in to url in order to get it server (which should be the default response)

tipsy commented 5 years ago

Not possible I'm afraid.

vlio20 commented 5 years ago

Is there any way to workaround it? redirects?

tipsy commented 5 years ago

If your main problem with using nested directories is that index.html has to be in one URL, I would write a custom handler that serves that particular file. Or maybe I misunderstood?

vlio20 commented 5 years ago

There are 2 things here:

  1. Say I want to serve my site from: https://mysite.com/site -> this will result the index.html
  2. I want my website assets to be served from https://mysite.com/site/static (example: https://mysite.com/site/static/main.css)
tipsy commented 5 years ago

Something like this?

staticFiles.location("/public"); // put `/site/static/` dirs in here
get("/site", (req, res) ->
    Spark.class.getResourceAsStream("/public/site/static/index.html")
);
vlio20 commented 5 years ago

Can you please also show the Kotlin version of your suggestion?

vlio20 commented 5 years ago

found it: Spark::class.java.getResourceAsStream() Trying to make it work

vlio20 commented 5 years ago

@tipsy it did work. Though I think this feature should be baked into the framework.

Thanks!