List Phoenix API

bexley Source

I would like to have an endpoint /api/api_endpoints/ that list all the public api endpoints. Similar to mix phx.routes, but I only want to list things like

...
GET /api/users
PUT /api/users/name
...

Things like that. I looked in the documentation but did not see anything for listing routes.

elixirphoenix-framework

Answers

answered 6 months ago Mario #1

There is nothing built in, but for something as simple as you want you could always roll your own. All the routes are stored inside your router as a list of Phoenix.Router.Route stucts. For example in a project called Residential:

iex(1)> ResidentialWeb.Router.__routes__
[%Phoenix.Router.Route{assigns: %{}, helper: "page", host: nil, kind: :match,
opts: :index, path: "/", pipe_through: [:browser],
plug: ResidentialWeb.PageController, private: %{}, verb: :get},
%Phoenix.Router.Route{assigns: %{}, helper: "user", host: nil, kind: :match,
opts: :index, path: "/users", pipe_through: [:browser],
plug: ResidentialWeb.UserController, private: %{}, verb: :get},
%Phoenix.Router.Route{assigns: %{}, helper: "user", host: nil, kind: :match,
opts: :edit, path: "/users/:id/edit", pipe_through: [:browser],
plug: ResidentialWeb.UserController, private: %{}, verb: :get},

You can map over them and extract the verb and path and show them as you please.

This is pretty much how mix phx.routes works, if you take a look at the implementation

comments powered by Disqus