Skip to content

Commit

Permalink
Refactoring plug configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst committed Nov 1, 2017
1 parent 850695a commit a7c9329
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 127 deletions.
25 changes: 0 additions & 25 deletions lib/triplex/param_plug.ex

This file was deleted.

48 changes: 0 additions & 48 deletions lib/triplex/plug_config.ex

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ defmodule Triplex.EnsurePlug do
callback: &TenantHelper.callback/2
failure_callback: &TenantHelper.failure_callback/2
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Triplex.PlugConfig
alias Triplex.EnsurePlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(EnsurePlugConfig, opts)

@doc false
def call(conn, config), do: ensure_tenant(conn, config)
Expand Down
17 changes: 17 additions & 0 deletions lib/triplex/plugs/ensure_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Triplex.EnsurePlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.EnsurePlug`.
Here are the config keys allowed:
- `assign`: the name of the assign where we must save the tenant.
- `callback`: function that might be called when the plug succeeded. It
must return a connection.
- `failure_callback`: function that might be called when the plug failed.
It must return a connection.
"""

defstruct [:callback, :failure_callback, assign: :current_tenant]
end


32 changes: 32 additions & 0 deletions lib/triplex/plugs/param_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Triplex.ParamPlug do
@moduledoc """
This is a basic plug that loads the current tenant assign from a given
param.
To plug it on your router, you can use:
plug Triplex.ParamPlug,
param: :subdomain,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.ParamPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Triplex.ParamPlugConfig

@doc false
def init(opts), do: struct(ParamPlugConfig, opts)

@doc false
def call(conn, config),
do: put_tenant(conn, get_param(conn, config.param), config)

defp get_param(conn, %ParamPlugConfig{param: key}),
do: get_param(conn, key)
defp get_param(conn, key) when is_atom(key),
do: get_param(conn, Atom.to_string(key))
defp get_param(conn, key),
do: conn.params[key]
end

16 changes: 16 additions & 0 deletions lib/triplex/plugs/param_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Triplex.ParamPlugConfig do
@moduledoc """
This is a struct that holds all configuration for `Triplex.ParamPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `param`: the param name to load the tenant from.
"""

defstruct [:tenant_handler, assign: :current_tenant, param: "tenant"]
end


13 changes: 8 additions & 5 deletions lib/triplex/plug.ex → lib/triplex/plugs/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ defmodule Triplex.Plug do
- `Triplex.SessionPlug` - loads the tenant from a session param
- `Triplex.SubdomainPlug` - loads the tenant from the url subdomain
- `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not
You can also check `Triplex.PlugConfig` for the config options you have for
each plug.
"""

import Plug.Conn
Expand All @@ -21,7 +18,11 @@ defmodule Triplex.Plug do
Puts the given `tenant` as an assign on the given `conn`, but only if the
tenant is not reserved.
See `Triplex.PlugConfig` to the allowed `config` flags.
The `config` map/struct must have:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
"""
def put_tenant(conn, tenant, config) do
if conn.assigns[config.assign] do
Expand All @@ -40,7 +41,9 @@ defmodule Triplex.Plug do
@doc """
Ensure the tenant is loaded, and if not, halts the `conn`.
See `Triplex.PlugConfig` to the allowed `config` flags.
The `config` map/struct must have:
- `assign`: the name of the assign where we must save the tenant.
"""
def ensure_tenant(conn, config) do
if loaded_tenant = conn.assigns[config.assign] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule Triplex.SessionPlug do
session: :subdomain,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.SessionPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
import Plug.Conn
alias Triplex.PlugConfig
alias Triplex.SessionPlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(SessionPlugConfig, opts)

@doc false
def call(conn, config),
Expand Down
18 changes: 18 additions & 0 deletions lib/triplex/plugs/session_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Triplex.SessionPlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.SessionPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `session`: the session param name to load the tenant from.
"""

defstruct [
:tenant_handler,
assign: :current_tenant,
session: :tenant
]
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ defmodule Triplex.SubdomainPlug do
endpoint: MyApp.Endpoint,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Plug.Conn
alias Triplex.PlugConfig
alias Triplex.SubdomainPlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(SubdomainPlugConfig, opts)

@doc false
def call(conn, config),
do: put_tenant(conn, get_subdomain(conn, config), config)

defp get_subdomain(_conn, %PlugConfig{endpoint: nil}) do
defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do
nil
end
defp get_subdomain(%Conn{host: host}, %PlugConfig{endpoint: endpoint}) do
defp get_subdomain(%Conn{host: host},
%SubdomainPlugConfig{endpoint: endpoint}) do
root_host = endpoint.config(:url)[:host]
if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do
nil
Expand Down
19 changes: 19 additions & 0 deletions lib/triplex/plugs/subdomain_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Triplex.SubdomainPlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.SubdomainPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `endpoint`: the Phoenix.Endpoint to get the host name to dicover the
subdomain.
"""

defstruct [
:endpoint,
:tenant_handler,
assign: :current_tenant,
]
end
27 changes: 0 additions & 27 deletions test/triplex/plug_config_test.exs

This file was deleted.

File renamed without changes.
File renamed without changes.
23 changes: 12 additions & 11 deletions test/triplex/plug_test.exs → test/triplex/plugs/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ defmodule Triplex.PlugTest do
import Plug.Conn
import Plug.Test
alias Triplex.Plug
alias Triplex.PlugConfig
alias Triplex.ParamPlugConfig
alias Triplex.EnsurePlugConfig

test "put_tenant/3 must set the tenant to the default assign" do
conn =
:get
|> conn("/")
|> Plug.put_tenant("power", PlugConfig.new())
|> Plug.put_tenant("power", %ParamPlugConfig{})
assert conn.assigns[:current_tenant] == "power"
end

Expand All @@ -19,15 +20,15 @@ defmodule Triplex.PlugTest do
conn =
:get
|> conn("/")
|> Plug.put_tenant("oi", PlugConfig.new(tenant_handler: handler))
|> Plug.put_tenant("oi", %ParamPlugConfig{tenant_handler: handler})
assert conn.assigns[:current_tenant] == "olá"
end

test "put_tenant/3 must set the tenant on the given assign" do
conn =
:get
|> conn("/")
|> Plug.put_tenant("power", PlugConfig.new(assign: :tenant))
|> Plug.put_tenant("power", %ParamPlugConfig{assign: :tenant})
assert conn.assigns[:tenant] == "power"
end

Expand All @@ -36,24 +37,24 @@ defmodule Triplex.PlugTest do
:get
|> conn("/")
|> assign(:current_tenant, "already_set")
|> Plug.put_tenant("power", PlugConfig.new())
|> Plug.put_tenant("power", %ParamPlugConfig{})
assert conn.assigns[:current_tenant] == "already_set"
end

test "put_tenant/3 must not set the tenant if it is reserved" do
conn =
:get
|> conn("/")
|> Plug.put_tenant("www", PlugConfig.new())
|> Plug.put_tenant("www", %ParamPlugConfig{})
assert conn.assigns[:current_tenant] == nil
end

test "ensure_tenant/3 must halts the conn" do
conn =
:get
|> conn("/")
|> Plug.put_tenant("power", PlugConfig.new())
|> Plug.ensure_tenant(PlugConfig.new())
|> Plug.put_tenant("power", %ParamPlugConfig{})
|> Plug.ensure_tenant(%ParamPlugConfig{})

assert conn.halted == false
end
Expand All @@ -63,8 +64,8 @@ defmodule Triplex.PlugTest do
conn =
:get
|> conn("/")
|> Plug.put_tenant("power", PlugConfig.new())
|> Plug.ensure_tenant(PlugConfig.new(callback: callback))
|> Plug.put_tenant("power", %ParamPlugConfig{})
|> Plug.ensure_tenant(%EnsurePlugConfig{callback: callback})

assert conn.assigns[:test] == "blag"
end
Expand All @@ -74,7 +75,7 @@ defmodule Triplex.PlugTest do
conn =
:get
|> conn("/")
|> Plug.ensure_tenant(PlugConfig.new(failure_callback: callback))
|> Plug.ensure_tenant(%EnsurePlugConfig{failure_callback: callback})

assert conn.assigns[:test] == "blog"
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a7c9329

Please # to comment.