Created
November 10, 2022 13:20
-
-
Save Maikon/40e1012cf0dd906ba2c793553595eb27 to your computer and use it in GitHub Desktop.
Phoenix whitelabel app (subdomain plug)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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