I’m building a web API using only Plug (no Phoenix). I started small but now it is growing and the number of routes is growing too.
I started to use forward to extract some routes to a proper module.
What I don’t like about forward is that it creates mutually exclusive routes.
Do you know if there is a different way to split the routes in different modules so that they are all evaluated until a matching route is found?
Why?
Because I would like to group routes in terms of the functionality not in terms of route url.
For example I want /companies to be in the Company module and /companies/:id/employees in the Employee module (and I don’t like to double forward )
For organization, you can split the routes into different modules that implement a macro, then use that macro in your main router module. That way, all the routes end up in the main router after compilation.
Something like this:
defmodule App.Routes do
import App.Routes.{Web, Api}
web_routes()
api_routes()
end
defmodule App.Routes.Web do
defmacro web_routes do
quote do
# ...module routes here...
end
end
end
defmodule App.Routes.Api do
defmacro api_routes do
quote do
# ...module routes here...
end
end
end