cdepillabout / servant-static-th

Embed a directory of static files in your application and serve them from your Servant server
http://hackage.haskell.org/package/servant-static-th
BSD 3-Clause "New" or "Revised" License
14 stars 9 forks source link

Serving root path #6

Closed Soft closed 3 years ago

Soft commented 6 years ago

Is there a way to configure servant-static-th to map particular file to / path? Based on the documentation there doesn't seem to be a way to do this currently. Is there a way around this limitation?

cdepillabout commented 6 years ago

@Soft There is not currently a way around this limitation, unfortunately.

What is your use-case for something like this?

I guess I would accept a pull request adding something like this, but I'm wondering what you need it for? servant-static-th is generally just for easily serving static files, so I guess you want to serve a given static file at a different URL path? (Well, /)

cdepillabout commented 6 years ago

You could always manually redirect the user to the file you want to serve.

So if $(createApiAndServerDecs "FrontEndApi" "frontEndServer" "dir") expands to the following:

type FrontEndAPI =
       "js" :> "test.js" :> Get '[JS] ByteString
  :<|> "index.html" :> Get '[HTML] Html

frontEndServer :: Applicative m => ServerT FrontEndAPI m
frontEndServer =
       pure "console.log(\"hello world\");"
  :<|> pure "<p>Hello World</p>"

You could create a route that just redirects somewhere else:

type FrontEndPlusRedirectApi = FrontEndApi :<|> Get '[HTML] Void -- this Get route actually returns an http 302

frontEndPlusRedirectServer :: ServerT FrontEndPlusRedirectAPI m
frontEndPlusRedirectServer = frontEndServer :<|> undefined -- instead of undefined, redirect the user to /index.html or wherever

Then you could use it like normal:

app :: Application
app = serve (Proxy :: Proxy FrontEndPlusRedirectApi) frontEndPlusRedirectServer

main :: IO ()
main = run 8080 app

This would be a valid work-around, but it is definitely manual and hacky.

Soft commented 6 years ago

Well, I wanted to embed the fronted code for my Servant app directly into the application binary and serve some static html at / that would then query backend's rest api using JavaScript.

cdepillabout commented 6 years ago

Maybe servant-static-th could be changed so that if you have an index.html in a directory, it also adds it so that it is served at just the directory itself? That would make it work similar to how Apache and nginx work.

I would be willing to accept a PR implementing this.