haskell-servant / servant

Servant is a Haskell DSL for describing, serving, querying, mocking, documenting web applications and more!
https://docs.servant.dev/
1.83k stars 413 forks source link

Adding an endpoint to serve static files to a pre-existing servant web service #1153

Closed omefire closed 5 years ago

omefire commented 5 years ago

I have created a web service that exposes some REST API, but now I want to add another one to serve static files. However, I don't know how to get started.

Here is the code I want to extend: https://github.com/omefire/trello-reminders-server/blob/master/app/Main.hs

alpmestan commented 5 years ago

If your existing API's API type is called ExistingAPI, with handlers named existingServer :: Server ExistingAPI, then you can do:

type NewAPI = ExistingAPI :<|> "files" :> Raw

newServer :: Server NewAPI
newServer = existingServer :<|> serveDirectoryWebApp "./www"

More file serving functions available at: https://hackage.haskell.org/package/servant-server-0.16/docs/Servant-Server-StaticFiles.html

omefire commented 5 years ago

Thanks a lot! this was helpful. @alpmestan

alpmestan commented 5 years ago

Feel free to close when your problem is solved, or to come back with other questions if you can't get what you want to work.