From a9d88f5fa5612c2ced94e6cf7f5ffaa9948bb3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Mon, 18 Sep 2023 12:40:35 +0200 Subject: [PATCH] Move endpoint config into the case --- config/runtime.exs | 154 +++++++++++++++++++++++---------------------- config/test.exs | 1 - 2 files changed, 78 insertions(+), 77 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 3013f776..6229d77b 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -121,80 +121,82 @@ config :jellyfish, config :opentelemetry, traces_exporter: :none -# we set ip and port here to allow for -# running multiple Jellyfishes in development -config :jellyfish, JellyfishWeb.Endpoint, - # Binding to loopback ipv4 address prevents access from other machines. - # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. - http: [ip: {127, 0, 0, 1}, port: port] - -if prod? do - token = - System.fetch_env!("SERVER_API_TOKEN") || - raise """ - environment variable SERVER_API_TOKEN is missing. - SERVER_API_TOKEN is used for HTTP requests and - server WebSocket authorization. - """ - - config :jellyfish, server_api_token: token - - # The secret key base is used to sign/encrypt cookies and other secrets. - # A default value is used in config/dev.exs and config/test.exs but you - # want to use a different value for prod and you most likely don't want - # to check this value into version control, so we use an environment - # variable instead. - secret_key_base = - System.get_env("SECRET_KEY_BASE") || - raise """ - environment variable SECRET_KEY_BASE is missing. - You can generate one by calling: mix phx.gen.secret - """ - - check_origin? = System.get_env("CHECK_ORIGIN", "true") == "true" - - config :jellyfish, JellyfishWeb.Endpoint, - url: [host: host, port: 443, scheme: "https"], - check_origin: check_origin?, - http: [ - # Enable IPv6 and bind on all interfaces. - # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. - # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html - # for details about using IPv6 vs IPv4 and loopback vs public addresses. - ip: {0, 0, 0, 0, 0, 0, 0, 0}, - port: port - ], - secret_key_base: secret_key_base - - # ## SSL Support - # - # To get SSL working, you will need to add the `https` key - # to your endpoint configuration: - # - # config :jellyfish, JellyfishWeb.Endpoint, - # https: [ - # ..., - # port: 443, - # cipher_suite: :strong, - # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), - # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") - # ] - # - # The `cipher_suite` is set to `:strong` to support only the - # latest and more secure SSL ciphers. This means old browsers - # and clients may not be supported. You can set it to - # `:compatible` for wider support. - # - # `:keyfile` and `:certfile` expect an absolute path to the key - # and cert in disk or a relative path inside priv, for example - # "priv/ssl/server.key". For all supported SSL configuration - # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 - # - # We also recommend setting `force_ssl` in your endpoint, ensuring - # no data is ever sent via http, always redirecting to https: - # - # config :jellyfish, JellyfishWeb.Endpoint, - # force_ssl: [hsts: true] - # - # Check `Plug.SSL` for all available options in `force_ssl`. +case config_env() do + env when env != :prod -> + # we set ip and port here to allow for + # running multiple Jellyfishes in development + config :jellyfish, JellyfishWeb.Endpoint, + # Binding to loopback ipv4 address prevents access from other machines. + # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. + http: [ip: {127, 0, 0, 1}, port: port] + + :prod -> + token = + System.fetch_env!("SERVER_API_TOKEN") || + raise """ + environment variable SERVER_API_TOKEN is missing. + SERVER_API_TOKEN is used for HTTP requests and + server WebSocket authorization. + """ + + config :jellyfish, server_api_token: token + + # The secret key base is used to sign/encrypt cookies and other secrets. + # A default value is used in config/dev.exs and config/test.exs but you + # want to use a different value for prod and you most likely don't want + # to check this value into version control, so we use an environment + # variable instead. + secret_key_base = + System.get_env("SECRET_KEY_BASE") || + raise """ + environment variable SECRET_KEY_BASE is missing. + You can generate one by calling: mix phx.gen.secret + """ + + check_origin? = System.get_env("CHECK_ORIGIN", "true") == "true" + + config :jellyfish, JellyfishWeb.Endpoint, + url: [host: host, port: 443, scheme: "https"], + check_origin: check_origin?, + http: [ + # Enable IPv6 and bind on all interfaces. + # Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access. + # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html + # for details about using IPv6 vs IPv4 and loopback vs public addresses. + ip: {0, 0, 0, 0, 0, 0, 0, 0}, + port: port + ], + secret_key_base: secret_key_base + + # ## SSL Support + # + # To get SSL working, you will need to add the `https` key + # to your endpoint configuration: + # + # config :jellyfish, JellyfishWeb.Endpoint, + # https: [ + # ..., + # port: 443, + # cipher_suite: :strong, + # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), + # certfile: System.get_env("SOME_APP_SSL_CERT_PATH") + # ] + # + # The `cipher_suite` is set to `:strong` to support only the + # latest and more secure SSL ciphers. This means old browsers + # and clients may not be supported. You can set it to + # `:compatible` for wider support. + # + # `:keyfile` and `:certfile` expect an absolute path to the key + # and cert in disk or a relative path inside priv, for example + # "priv/ssl/server.key". For all supported SSL configuration + # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 + # + # We also recommend setting `force_ssl` in your endpoint, ensuring + # no data is ever sent via http, always redirecting to https: + # + # config :jellyfish, JellyfishWeb.Endpoint, + # force_ssl: [hsts: true] + # + # Check `Plug.SSL` for all available options in `force_ssl`. end diff --git a/config/test.exs b/config/test.exs index eb9df258..f38bc283 100644 --- a/config/test.exs +++ b/config/test.exs @@ -7,7 +7,6 @@ config :jellyfish, # We don't run a server during test. If one is required, # you can enable the server option below. config :jellyfish, JellyfishWeb.Endpoint, - http: [ip: {127, 0, 0, 1}, port: 4002], secret_key_base: "DtVd7qfpae0tk5zRgAM75hOaCc+phk38gDFVvLPyqVN/vvVg0EPmksTSm5JcyjoJ", server: false