Skip to content

Commit cc47e8e

Browse files
committed
Add transform function support to sync and sync_render
Fixes #53
1 parent 33a9af0 commit cc47e8e

28 files changed

+966
-71
lines changed

apps/plug_sync/.formatter.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Used by "mix format"
2+
[
3+
import_deps: [:plug, :ecto, :phoenix_sync],
4+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
5+
]

apps/plug_sync/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# If the VM crashes, it generates a dump, let's ignore it too.
14+
erl_crash.dump
15+
16+
# Also ignore archive artifacts (built via "mix archive.build").
17+
*.ez
18+
19+
# Ignore package tarball (built via "mix hex.build").
20+
plug_sync-*.tar
21+
22+
# Temporary files, for example, from tests.
23+
/tmp/

apps/plug_sync/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PlugSync
2+
3+
An test Plug app for easy validation of Phoenix.Sync features within a real app.
4+
5+
Use `PHOENIX_SYNC_MODE` to set the interaction mode, either `"http"` or `"embedded"`.
6+
7+
In `http` mode, you should run an Electric server on port 3000.

apps/plug_sync/config/config.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Config
2+
3+
import_config "#{config_env()}.exs"

apps/plug_sync/config/dev.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Config
2+
3+
config :plug_sync, ecto_repos: [PlugSync.Repo], start_server: false
4+
5+
case System.get_env("PHOENIX_SYNC_MODE", "embedded") do
6+
"http" ->
7+
IO.puts("Starting in HTTP mode")
8+
9+
config :phoenix_sync,
10+
mode: :http,
11+
env: config_env(),
12+
url: "http://localhost:3000"
13+
14+
config :plug_sync, PlugSync.Repo,
15+
username: "postgres",
16+
password: "password",
17+
hostname: "localhost",
18+
database: "electric",
19+
port: 54321
20+
21+
_ ->
22+
IO.puts("Starting in embedded mode")
23+
24+
config :phoenix_sync,
25+
mode: :embedded,
26+
env: config_env(),
27+
repo: PlugSync.Repo
28+
29+
config :plug_sync, PlugSync.Repo,
30+
username: "postgres",
31+
password: "password",
32+
hostname: "localhost",
33+
database: "plug_sync",
34+
port: 55555
35+
end

apps/plug_sync/config/prod.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import Config

apps/plug_sync/config/test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Config
2+
3+
config :phoenix_sync, mode: :sandbox, env: config_env()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Mix.Tasks.PlugSync.Server do
2+
use Mix.Task
3+
4+
@shortdoc "Starts a PlugSync server"
5+
6+
def run(_args) do
7+
Application.put_env(:plug_sync, :server, true, persistent: true)
8+
Mix.Tasks.Run.run(run_args())
9+
end
10+
11+
defp run_args do
12+
if iex_running?(), do: [], else: ["--no-halt"]
13+
end
14+
15+
defp iex_running? do
16+
Code.ensure_loaded?(IEx) and IEx.started?()
17+
end
18+
end

apps/plug_sync/lib/plug_sync.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule PlugSync do
2+
@moduledoc """
3+
Documentation for `PlugSync`.
4+
"""
5+
6+
@doc """
7+
Hello world.
8+
9+
## Examples
10+
11+
iex> PlugSync.hello()
12+
:world
13+
14+
"""
15+
def hello do
16+
:world
17+
end
18+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule PlugSync.Application do
2+
# See https://hexdocs.pm/elixir/Application.html
3+
# for more information on OTP Applications
4+
@moduledoc false
5+
6+
use Application
7+
8+
@impl true
9+
def start(_type, _args) do
10+
server =
11+
if Application.get_env(:plug_sync, :server, false) do
12+
[
13+
{Bandit, plug: {PlugSync.Router, phoenix_sync: Phoenix.Sync.plug_opts()}, port: 4444}
14+
]
15+
else
16+
[]
17+
end
18+
19+
children =
20+
[
21+
PlugSync.Repo
22+
] ++ server
23+
24+
# See https://hexdocs.pm/elixir/Supervisor.html
25+
# for other strategies and supported options
26+
opts = [strategy: :one_for_one, name: PlugSync.Supervisor]
27+
Supervisor.start_link(children, opts)
28+
end
29+
end

0 commit comments

Comments
 (0)