Skip to content

Instantly share code, notes, and snippets.

@mrdotb
Last active August 20, 2024 00:39
Show Gist options
  • Save mrdotb/63cdbe22ceb36ec6f3dc828fe9310b62 to your computer and use it in GitHub Desktop.
Save mrdotb/63cdbe22ceb36ec6f3dc828fe9310b62 to your computer and use it in GitHub Desktop.
Phoenix hosts management
defmodule AppWeb.Host do
@moduledoc """
Conveniences for working with host.
"""
def root do
Keyword.get(get_config(), :root)
end
def root_uri do
Keyword.get(get_config(), :root_uri)
end
def admin do
Keyword.get(get_config(), :admin)
end
def admin_uri do
Keyword.get(get_config(), :admin)
end
defp get_config do
Application.get_env(:your_app, __MODULE__)
end
end
# This is the config for production
config :your_app, AppWeb.Host,
root: "yourdomain.co",
root_uri: %URI{host: "yourdomain.co", scheme: "https", port: 443},
admin: "admin.yourdomain.co",
admin_uri: %URI{host: "admin.yourdomain.co", 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
...
end
scope "/", BabelBookWeb, host: Host.admin() do
...
get "/", ConsoleController, :index
...
end
# Generate link for routes in root and admin admin with production config
Router.user_registration_url(AppWeb.Host.root_uri(), :new) -> https://yourdomain.co/users/register
Router.console_url(AppWeb.Host.admin_uri(), :index) -> https://admin.yourdomain.co/
# 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