Phoenix.Sync.Electric (Phoenix.Sync v0.3.4)
View SourceA Plug.Router
and Phoenix.Router
compatible Plug handler that allows
you to mount the full Electric shape api into your application.
Unlike Phoenix.Sync.Router.sync/2
this allows your app to serve
shapes defined by table
parameters, much like the Electric application.
The advantage is that you're free to put your own authentication and authorization Plugs in front of this endpoint, integrating the auth for your shapes API with the rest of your app.
Configuration
Before configuring your router, you must install and configure the
:phoenix_sync
application.
See the documentation for embedding electric for details on embedding Electric into your Elixir application.
Plug Integration
Mount this Plug into your router using Plug.Router.forward/2
.
defmodule MyRouter do
use Plug.Router, copy_opts_to_assign: :config
use Phoenix.Sync.Electric
plug :match
plug :dispatch
forward "/shapes",
to: Phoenix.Sync.Electric,
init_opts: [opts_in_assign: :config]
end
You must configure your Plug.Router
with copy_opts_to_assign
and
pass the key you configure here (in this case :config
) to the
Phoenix.Sync.Electric
plug in it's init_opts
.
In your application, build your Electric confguration using
Phoenix.Sync.plug_opts()
and pass the result to your router as
phoenix_sync
:
# in application.ex
def start(_type, _args) do
children = [
{Bandit, plug: {MyRouter, phoenix_sync: Phoenix.Sync.plug_opts()}, port: 4000}
]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
end
Phoenix Integration
Use Phoenix.Router.forward/2
in your router:
defmodule MyAppWeb.Router do
use Phoenix.Router
pipeline :shapes do
# your authz plugs
end
scope "/shapes" do
pipe_through [:shapes]
forward "/", Phoenix.Sync.Electric
end
end
As for the Plug integration, include the configuration at runtime
within the Application.start/2
callback.
# in application.ex
def start(_type, _args) do
children = [
# ...
{MyAppWeb.Endpoint, phoenix_sync: Phoenix.Sync.plug_opts()}
]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
end