Skip to content

Instantly share code, notes, and snippets.

@Maikon
Created November 10, 2022 13:20
Show Gist options
  • Save Maikon/40e1012cf0dd906ba2c793553595eb27 to your computer and use it in GitHub Desktop.
Save Maikon/40e1012cf0dd906ba2c793553595eb27 to your computer and use it in GitHub Desktop.
Phoenix whitelabel app (subdomain plug)
defmodule FrescoWeb.Plugs.Subdomain do
@behaviour Plug # see this for more on behaviours: https://elixir-lang.org/getting-started/typespecs-and-behaviours.html#behaviours
import Plug.Conn, only: [put_private: 3]
def init(_opts) do
%{ root_host: FrescoWeb.Endpoint.config(:url)[:host] }
end
def call(%Plug.Conn{host: host} = conn, %{root_host: root_host} = _opts) do
case extract_subdomain(host, root_host) do
subdomain when byte_size(subdomain) > 0 ->
put_private(conn, :subdomain, subdomain)
_ ->
conn
end
end
defp extract_subdomain(host, root_host) do
String.replace(host, ~r/.?#{root_host}/, "")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment