View Source CozyProxy (cozy_proxy v0.3.1)
Proxy requests to other plugs.
Usage
A CozyProxy
instance is an isolated supervision tree and you can include it in
application's supervisor:
# lib/demo/application.ex
def start(_type, _args) do
children = [
# ...
{CozyProxy, Application.fetch_env!(:demo, CozyProxy)}
]
opts = [strategy: :one_for_one, name: Demo.Supervisor]
Supervisor.start_link(children, opts)
end
Above code requires a piece of configuration:
config :demo, CozyProxy,
server: true,
backends: [
%{
plug: HealthCheckPlug,
path: "/health-check"
},
%{
plug: DemoWebAPI.Endpoint,
path: "/api"
},
%{
plug: DemoAdminWeb.Endpoint,
path: "/admin"
},
%{
plug: DemoWeb.Endpoint,
path: "/"
}
]
When using CozyProxy
, it's better to configure Phoenix endpoints to not start servers,
in order to avoid Phoenix endpoints bypassing CozyProxy
:
config :demo, DemoWeb.Endpoint, server: false
config :demo, DemoWebAPI.Endpoint, server: false
config :demo, DemoAdminWeb.Endpoint, server: false
Options
:server
- start the web server or not. Default tofalse
. It is aware of Phoenix startup arguments, if the application is started withmix phx.server
oriex -S mix phx.server
, this option will set totrue
.:backends
- the list of backends. Default to[]
. See next section for more details.:adapter
- the adapter for web server,Plug.Cowboy
andBandit
are available. Default toPlug.Cowboy
.- All other options will be put into an keyword list and passed as the options of
the adapter:
- For
Plug.Cowboy
, checkout Plug.Cowboy.child_spec/1. - For
Bandit
, checkout Bandit.options/0.
- For
about :backends
A valid :backends
option is a list of maps, and the keys of maps are:
:plug
:- required
typespec:
module() | {module(), keyword()}
- examples:
HealthCheckPlug
{HealthCheckPlug, []}
- ...
:method
:- optional
- typespec:
String.t()
- examples:
"GET"
"POST"
- ...
:host
:- optional
- typespec:
String.t()
- examples:
"example.com"
- ...
:path
:- optional
- typespec:
String.t()
- examples:
"/admin"
"/api"
- ...
The order of backends matters
If you configure the backends like this:
config :demo, CozyProxy,
backends: [
%{
plug: DemoUserWeb.Endpoint,
path: "/"
},
%{
plug: DemoUserAPI.Endpoint,
path: "/api"
},
%{
plug: DemoAdminWeb.Endpoint,
path: "/admin"
},
%{
plug: HealthCheck,
path: "/health"
}
]
The first backend will always match, which may not what you expected.
If you want all backends to have a chance to match, you should configure them like this:
config :demo, CozyProxy,
backends: [
%{
plug: HealthCheck,
path: "/health"
},
%{
plug: DemoUserAPI.Endpoint,
path: "/api"
},
%{
plug: DemoAdminWeb.Endpoint,
path: "/admin"
},
%{
plug: DemoUserWeb.Endpoint,
path: "/"
}
]
Examples
# Example options for dev environment
[
backends: [
# ...
]
adapter: Plug.Cowboy,
scheme: :http,
ip: {127, 0, 0, 1},
port: 4000,
transport_options: [num_acceptors: 2]
]
# Example options for prod environment
[
server: true,
backends: [
# ...
]
adapter: Plug.Cowboy,
scheme: :http,
ip: {0, 0, 0, 0},
port: 4000,
]
Summary
Functions
Returns a specification to start this module under a supervisor.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.