I want to serve a static asset named api.json located in the priv/static folder of my Phoenix (1.2) app. However, I don’t want to serve it at the root: localhost:4000/api.json, I want to serve it at localhost:4000/v1/api.json. How do I do this?
I tried this but it’s not working for me:
scope "/v1" do
forward "/", Plug.Static, at: "/", from: :api, only: ~w(api.json)
end
This does work, but I’d really like to have this logic in my router.
For example, it would be best if my user could hit an endpoint: /v1/api and have the api.json static asset returned. I’m struggling to figure out how to do this.
Either you have to use Plug.Static to halt the pipeline early when the file does exist, or you have to go through the full pipeline until you hit the view, from which you can return a static string, you can even load it at compile time from the file. But as I said, that needs to run through all plugs and therefore might be slightly slower.