Darkpingouin

Darkpingouin

Activate / Deactivate scopes in router from config

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 :slight_smile:

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.

Marked As Solved

Darkpingouin

Darkpingouin

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.

Also Liked

LostKobrakai

LostKobrakai

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.

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement