Hello,
So I have an API with a config file and several scopes in my router. I was wondering if it would be possible to disable / enable a specific scope from the config file. So that I wouldn’t have to recompile the project to activate or deactivate some routes.
Example
scope “/scope1” do
pipe_through [:authenticate_user, :ensure_admin]
#SOME_ROUTES
end
scope “/scope2” do
pipe_through [:authenticate_user, :ensure_admin]
#SOME_ROUTES
end
Would it be possible to put something in the config.exs that tells the router to block the scope1 ?
Thank you in advance for your help.
So you cannot make the router stop matching those routes – the phoenix router works by creating one big pattern match to select the route, which happens at compile time. Changing that therefore requires fresh compilation.
What you can do at runtime however is have an plug (or pipeline), which stops requests from being further processed.
1 Like
How could I configure such a pipeline to check in the config if it has the right to forward or not ?
Found a way, I did a specific plug that checks the variable in the config file :
def call(conn, _default) do
if Application.get_env(:api, ApiWeb.Endpoint)[:hasAccess] do
conn
else
send_resp(conn, 403, "You do not have access.")
|> halt()
end
end
Then I just call this plug in a pipeline in the router and I tell my scope to run through the pipeline.
Thanks a lot for the suggestion.