Comments (9)
Of course, you can recompile. My point is changing the configuration of the app using environment variables or another kind of runtime configuration. So, I compile once my project but I execute it in different environments, production and staging. The binary is exactly the same, the configuration is different. I'd like to change the CORS origin depending on the environment.
I don't know how to do this in your library. Maybe you get some ideas from Michal Muskala's post: http://michal.muskala.eu/2017/07/30/configuring-elixir-libraries.html#recommendations-for-library-authors
Thank you for your time
from cors_plug.
Right, it sounds like functions would be the best way to go for you @alexcastano, can you confirm?
from cors_plug.
Hey @alexcastano can you provide an example please? You should be able to use environment variables as long as you recompile (which might be the culprit here).
See also http://blog.plataformatec.com.br/2016/05/how-to-config-environment-variables-with-elixir-and-exrm/
from cors_plug.
If you find a solution you like, I can submit a pull request to implement it :)
from cors_plug.
You can just use function config for your issue, for instance:
defmodule Api
def origins do
~r/http:\/\/localhost:\d{1,4}$|#{Application.get_env(:api, :allowed_origins)}/
end
end
plug CORSPlug, origin: &Api.origins/0
This way the function Application.get_env
will be called in runtime each time, not during compilation, so your settings will be loaded properly.
I assumed you use erlang release variables like:
config :api, allowed_origins: "${ALLOWED_ORIGINS}"
from cors_plug.
I tried this but I didn't work for me. This is the error I have in the logs:
{"could not start kernel pid",application_controller,"error in config file \"/opt/app/var/sys.config\" (119): syntax error before: Fun"}
In that line I have:
{cors_plug,
[{origin,#Fun<Elixir.Api.Cors.origins.0>},
I use distillery and docker to deploy. I found this issue in bitwalker/exrm#230. It seems not possible to add a function in config.
Any suggestion?
from cors_plug.
I believe your issue is a dupe of #46. Try a named function whose reference you pass instead of (what I assume is) an anonymous function.
from cors_plug.
Hi @mschae, it was not an anonymous function, but I was using config.exs
and Mix.Config
does not support function arguments. Using just the keyword, without Mix.Config
as the argument for the plug works.
Thank you for your time.
from cors_plug.
Thanks for this thread, pointed me in the right direction. Indeed, defining origins
using an anonymous function resolved the issue of not being able to correctly set the origins when dependent on runtime.exs
-
# runtime.exs
import Config
if config_env() == :prod do
config :my_app,
allowed_origins: String.split(System.get_env("ALLOWED_ORIGINS") || "*", " ", trim: true)
end
# endpoint.ex
defmodule MyApp.Endpoint do
plug CORSPlug, origin: &__MODULE__.allowed_origins/0
def allowed_origins(), do: Application.get_env(:my_app, :allowed_origins)
end
from cors_plug.
Related Issues (20)
- Origin validation on OPTIONS HOT 3
- No CORS headers embedded in Plug.ErrorHandler code path HOT 2
- General CORS library HOT 3
- Options Requests without `Access-Control-Request-Method` should not be halted
- Unreachable code? HOT 2
- Support Plug 1.7 dependency HOT 1
- "Access-Control-Allow-Origin" is null when using regex for origin? HOT 3
- Configuration not working with Elixir 1.9 releases HOT 3
- Regex from string HOT 1
- FunctionClauseError when origin option is a list containing a regex HOT 1
- Regex isn't working on preflight request HOT 5
- A way to enable logging - difficult to configure HOT 3
- CORS Header empty HOT 2
- Looks like init function is called during the compile time HOT 1
- Access-Control-Allow-Credentials should not be included if set to false
- Headers cannot be set dynamically
- Minor security issue with origin checks
- Using config.ex does not work HOT 1
- default config of origin: * and credentials: true seems invalid?
- cors-rfc1918
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cors_plug.