apoorv-2204
Hi there Elixir Community,
IN the phoenix Endpoint, I want to have different session options for dev environment and the production environment, like setting cookie secure, etc.
Issue I am facing is when I use Application.get_env or Application.fetch_env it gives warning and CI fails ,
the Warning: is discouraged in the module body, use Application.compile_env/3 instead
When I use Compile env I get this error.
* (ArgumentError) could not fetch application environment [AdminWeb.Endpoint, :session_options] for application :admin because the application was not loaded nor configured
(elixir 1.14.4) lib/application.ex:602: Application.compile_env!/3
lib/admin_web/endpoint.ex:5: (module)
(elixir 1.14.4) lib/kernel/parallel_compiler.ex:340: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
Elixir
I have two apps in umbrella
AdminWeb and ServiceProviderWeb
Code for AdminWeb ,ServiceProvider
defmodule AdminWeb.Endpoint do
@moduledoc false
use Phoenix.Endpoint, otp_app: :admin
@session_options Application.compile_env!(:admin, [__MODULE__, :session_options])
socket("/admin/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [session: @session_options]]
)
#other lines omitted
plug(Plug.Session, @session_options)
end
defmodule ServiceProvider.Endpoint do
@moduledoc false
use Phoenix.Endpoint, otp_app: :service_provider
@session_options Application.compile_env!(:service_provider, [__MODULE__, :session_options])
socket("/service_provider/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [session: @session_options]]
)
plug(Plug.Session, @session_options)
end
dev.exs
config :admin, AdminWeb.Endpoint,
server: true,
http: [port: 4001],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
cd: Path.expand("../apps/admin/assets", __DIR__)
]
],
live_reload: [
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"priv/gettext/.*(po)$",
~r"lib/admin_web/.*(ex)$",
~r"lib/admin_web/.*(eex)$",
~r"lib/admin_web/.*(heex)$"
],
url: "/admin/phoenix/live_reload/socket"
],
session_options: [
max_age: 120 * 60 * 60,
store: AdminWeb.Session.Store,
key: "_a_dev_web_key_",
signing_salt: "rand_Contatvalie"
]
config :service_provider, ServiceProvider.Endpoint,
server: true,
http: [port: 4003],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
cd: Path.expand("../apps/service_provider/assets", __DIR__)
]
],
live_reload: [
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"priv/gettext/.*(po)$",
~r"lib/service_provider/.*(ex)$",
~r"lib/service_provider/.*(eex)$",
~r"lib/service_provider/.*(heex)$"
],
url: "/service_provider/phoenix/live_reload/socket"
],
session_options: [
store: :cookie,
key: "_sp_dev_web_key_",
signing_salt: "rand_Constant tvallue",
max_age: 120 * 60 * 60
]
Prod.exs
config :admin, AdminWeb.Endpoint,
cache_static_manifest: "priv/static/cache_manifest.json",
session_options: [
max_age: 120 * 60 * 60,
store: AdminWeb.Session.Store,
key: "_a_web_key_",
signing_salt: "random_sallt",
secure: true,
http_only: true,
sign: true
]
config :service_provider, ServiceProvider.Endpoint,
cache_static_manifest: "priv/static/cache_manifest.json",
session_options: [
store: :cookie,
key: "_sp_web_key_",
signing_salt: "random_sallt",
max_age: 120 * 60 * 60,
secure: true,
http_only: true,
sign: true,encrypt: true
]
`My questions`
Q1. What is the solution for it, what mistake I am making?.What wrong settings I have done here?
Q2. Are dev.exs and prod.exs executed once on compilation? so can i use `Base.encode64(:crypto.strong_rand_bytes(64), padding: false)` for `signing_salt`.
Q3. what are the generral best practices tips that you guys can share?
Trending in Discussions
Hey folks,
I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Hi everyone,
I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
A note on form. Joe Armstrong, co-creator of the Erlang programming language, died in April 2019. What follows is a work of narrative non...
New
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
apoorv-2204
elixir 1.14.4-otp-25
erlang 25.2.2
linux
already tried pruging _build , deps, plts,etc
gushonorato
Hey @apoorv-2204
The problem is that Application.compile_env!/3 does not accept a list as an argument. Instead, you should do the following:
@session_options Application.compile_env!(:admin, ServiceProvider.Endpoint) |> Keyword.fetch!(:session_options)You must not use
Base.encode64/2to generate thesigning_saltin this way. If you do so, a newsigning_saltwill be generated with each deployment, and all sessions will be invalidated in production every time the application is deployed.You can use any
signing_saltfor development and store it hardcoded in yourdev.exsfile. However, for production, it is not recommended to store thesigning_salthardcoded in version-controlled files. Instead, store it as an environment variable and load it inconfig/runtime.exs.apoorv-2204
thanks a lot sir
it solved my issue
@session_options Application.compile_env!(:admin, ServiceProvider.Endpoint) |> Keyword.fetch!(:session_options)