Skip to content

Instantly share code, notes, and snippets.

Last active September 16, 2022 21:51
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Phoenix hosts management
defmodule AppWeb.Host do
@moduledoc """
Conveniences for working with host.
def root do
Keyword.get(get_config(), :root)
def root_uri do
Keyword.get(get_config(), :root_uri)
def admin do
Keyword.get(get_config(), :admin)
def admin_uri do
Keyword.get(get_config(), :admin)
defp get_config do
Application.get_env(:your_app, __MODULE__)
# This is the config for production
config :your_app, AppWeb.Host,
root: "",
root_uri: %URI{host: "", scheme: "https", port: 443},
admin: "",
admin_uri: %URI{host: "", scheme: "https", port: 443}
# This is the config for dev or test
# Adapt dependending on your setup
config :your_app, AppWeb.Host,
root: nil,
root_uri: %URI{host: nil, scheme: "http", port: 4000},
admin: nil,
admin_uri: %URI{host: nil, scheme: "http", port: 4000}
# In the router.ex
alias AppWeb.Host
scope "/", BabelBookWeb, host: Host.root() do
get "/users/register", UserRegistrationController, :new
scope "/", BabelBookWeb, host: Host.admin() do
get "/", ConsoleController, :index
# Generate link for routes in root and admin admin with production config
Router.user_registration_url(AppWeb.Host.root_uri(), :new) ->
Router.console_url(AppWeb.Host.admin_uri(), :index) ->
# Generate link for routes in root and admin admin with dev / test config
Router.user_registration_url(AppWeb.Host.root_uri(), :new) -> http://localhost:4000/users/register
Router.console_url(AppWeb.Host.admin_uri(), :index) -> http://localhost:4000/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment