Skip to content

Commit

Permalink
[very wip] mounting shape plug into phoenix app
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetised committed Feb 6, 2025
1 parent 07ac30d commit 233e8c9
Show file tree
Hide file tree
Showing 21 changed files with 597 additions and 54 deletions.
34 changes: 28 additions & 6 deletions examples/phoenix-liveview/config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import Config

# Configure your database
config :electric_phoenix_example, Electric.PhoenixExample.Repo,
connection_opts = [
username: "postgres",
password: "password",
hostname: "localhost",
database: "electric",
port: 54321,
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
port: 54321
]

# Configure your database
config :electric_phoenix_example,
Electric.PhoenixExample.Repo,
Keyword.merge(connection_opts,
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
)

# For development, we disable any cache and enable
# debugging and code reloading.
Expand Down Expand Up @@ -86,3 +92,19 @@ config :phoenix_live_view,
config :electric_phoenix, Electric.Client,
base_url: System.get_env("ELECTRIC_URL", "http://localhost:3000/"),
params: System.get_env("ELECTRIC_CLIENT_PARAMS", "{}") |> :json.decode()

config :sentry,
environment_name: config_env(),
client: Electric.Telemetry.SentryReqHTTPClient

config :electric,
# connection_opts: Electric.Utils.obfuscate_password(connection_opts),
connection_opts: connection_opts,
# enable the http api so that the client tests against a real endpoint can
# run against our embedded electric instance.
enable_http_api: false,
allow_shape_deletion?: false,
# use a non-default replication stream id so we can run the client
# tests at the same time as an active electric instance
replication_stream_id: "liveview_example",
storage_dir: Path.join(System.tmp_dir!(), "electric/liveview_example#{System.monotonic_time()}")
19 changes: 19 additions & 0 deletions examples/phoenix-liveview/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,23 @@ if config_env() == :prod do
base_url:
System.get_env("ELECTRIC_URL") || raise("ELECTRIC_URL environment variable not set"),
params: System.get_env("ELECTRIC_CLIENT_PARAMS", "{}") |> :json.decode()

config :sentry,
environment_name: config_env(),
client: Electric.Telemetry.SentryReqHTTPClient

connection_opts = Electric.Config.parse_postgresql_uri!(database_url)

config :electric,
connection_opts: Electric.Utils.obfuscate_password(connection_opts),
# enable the http api so that the client tests against a real endpoint can
# run against our embedded electric instance.
enable_http_api: false,
service_port: port,
allow_shape_deletion?: false,
# use a non-default replication stream id so we can run the client
# tests at the same time as an active electric instance
replication_stream_id: "liveview_example",
storage_dir:
Path.join(System.tmp_dir!(), "electric/liveview_example#{System.monotonic_time()}")
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Electric.PhoenixExample.Application do

@impl true
def start(_type, _args) do
electric_config = Electric.Application.api_config()

children = [
Electric.PhoenixExampleWeb.Telemetry,
Electric.PhoenixExample.Repo,
Expand All @@ -16,7 +18,7 @@ defmodule Electric.PhoenixExample.Application do
# Start a worker by calling: Electric.PhoenixExample.Worker.start_link(arg)
# {Electric.PhoenixExample.Worker, arg},
# Start to serve requests, typically the last entry
Electric.PhoenixExampleWeb.Endpoint
{Electric.PhoenixExampleWeb.Endpoint, electric: electric_config}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ defmodule Electric.PhoenixExampleWeb.Router do
use Electric.PhoenixExampleWeb, :router

pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, html: {Electric.PhoenixExampleWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug(:accepts, ["html"])
plug(:fetch_session)
plug(:fetch_live_flash)
plug(:put_root_layout, html: {Electric.PhoenixExampleWeb.Layouts, :root})
plug(:protect_from_forgery)
plug(:put_secure_browser_headers)
end

pipeline :api do
plug :accepts, ["json"]
plug(:accepts, ["json"])
end

scope "/", Electric.PhoenixExampleWeb do
pipe_through :browser
pipe_through(:browser)

live "/", TodoLive.Index, :index
live("/", TodoLive.Index, :index)
end

scope "/shapes" do
pipe_through(:api)

get("/", Electric.Phoenix.Plug.Shapes, [])
end

# Other scopes may use custom stacks.
Expand Down
4 changes: 3 additions & 1 deletion examples/phoenix-liveview/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ defmodule Electric.PhoenixExample.MixProject do
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.1.1"},
{:bandit, "~> 1.5"},
{:electric_phoenix, "~> 0.2.0"},
# {:electric_phoenix, "~> 0.2.0"},
{:electric_phoenix, env: :dev, path: "../../packages/electric-phoenix/"},
# {:electric, path: "../../packages/sync-service"},
{:electric_client, ">= 0.2.6-pre-1", override: true}
]
end
Expand Down
Loading

0 comments on commit 233e8c9

Please sign in to comment.