Code Monkey home page Code Monkey logo

Comments (9)

alexcastano avatar alexcastano commented on July 20, 2024 1

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.

mschae avatar mschae commented on July 20, 2024 1

Right, it sounds like functions would be the best way to go for you @alexcastano, can you confirm?

from cors_plug.

mschae avatar mschae commented on July 20, 2024

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.

alexcastano avatar alexcastano commented on July 20, 2024

If you find a solution you like, I can submit a pull request to implement it :)

from cors_plug.

kelostrada avatar kelostrada commented on July 20, 2024

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.

alexcastano avatar alexcastano commented on July 20, 2024

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.

mschae avatar mschae commented on July 20, 2024

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.

alexcastano avatar alexcastano commented on July 20, 2024

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.Configas the argument for the plug works.

Thank you for your time.

from cors_plug.

darrenklein avatar darrenklein commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.