Skip to content

Commit 56525d9

Browse files
authored
Merge pull request #424 from letscolife/add-phoenix-live-view
Identify Phoenix LiveView metrics
2 parents f6e55bb + 55ee81c commit 56525d9

File tree

24 files changed

+277
-55
lines changed

24 files changed

+277
-55
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import "phoenix_html";
2+
import { Socket } from "phoenix";
3+
import { LiveSocket } from "phoenix_live_view";
4+
5+
let csrfToken = document
6+
.querySelector("meta[name='csrf-token']")
7+
.getAttribute("content");
8+
let liveSocket = new LiveSocket("/live", Socket, {
9+
params: { _csrf_token: csrfToken },
10+
});
11+
12+
liveSocket.connect();
13+
window.liveSocket = liveSocket;

examples/apps/phx_example/config/config.exs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ import Config
22

33
config :phx_example, PhxExampleWeb.Endpoint,
44
url: [host: "localhost"],
5-
render_errors: [view: PhxExampleWeb.ErrorView, accepts: ~w(html json), layout: false],
5+
render_errors: [formats: [html: PhxExampleWeb.ErrorHTML], layout: false],
66
http: [port: 4004],
7-
server: true
7+
server: true,
8+
adapter: Phoenix.Endpoint.Cowboy2Adapter,
9+
pubsub_server: PhxExample.PubSub,
10+
live_view: [signing_salt: "dB7qn7EQ"],
11+
secret_key_base: "A+gtEDayUNx4ZyfHvUKETwRC4RjxK0FDlrLjuRhaBnr3Ll3ynfu5RlSSGe5E7zbW"
12+
13+
config :logger, level: :warning
814

915
config :phoenix, :json_library, Jason

examples/apps/phx_example/lib/phx_example/application.ex

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule PhxExample.Application do
55

66
def start(_type, _args) do
77
children = [
8+
{Phoenix.PubSub, name: PhxExample.PubSub},
89
PhxExampleWeb.Endpoint
910
]
1011

examples/apps/phx_example/lib/phx_example_web.ex

+82-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,102 @@
11
defmodule PhxExampleWeb do
2-
def controller do
2+
@moduledoc """
3+
The entrypoint for defining your web interface, such
4+
as controllers, components, channels, and so on.
5+
6+
This can be used in your application as:
7+
8+
use PhxExampleWeb, :controller
9+
use PhxExampleWeb, :html
10+
11+
The definitions below will be executed for every controller,
12+
component, etc, so keep them short and clean, focused
13+
on imports, uses and aliases.
14+
15+
Do NOT define functions inside the quoted expressions
16+
below. Instead, define additional modules and import
17+
those modules here.
18+
"""
19+
20+
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
21+
22+
def router do
323
quote do
4-
use Phoenix.Controller, namespace: PhxExampleWeb
24+
use Phoenix.Router, helpers: false
525

626
import Plug.Conn
7-
alias PhxExampleWeb.Router.Helpers, as: Routes
27+
import Phoenix.Controller
28+
import Phoenix.LiveView.Router
829
end
930
end
1031

11-
def view do
32+
def channel do
1233
quote do
13-
use Phoenix.View,
14-
root: "lib/phx_example_web/templates",
15-
namespace: PhxExampleWeb
34+
use Phoenix.Channel
1635
end
1736
end
1837

19-
def router do
38+
def controller do
2039
quote do
21-
use Phoenix.Router
40+
use Phoenix.Controller,
41+
formats: [:html, :json],
42+
layouts: [html: PhxExampleWeb.Layouts]
2243

2344
import Plug.Conn
24-
import Phoenix.Controller
45+
46+
unquote(verified_routes())
47+
end
48+
end
49+
50+
def live_view do
51+
quote do
52+
use Phoenix.LiveView,
53+
layout: {PhxExampleWeb.Layouts, :app}
54+
55+
unquote(html_helpers())
56+
end
57+
end
58+
59+
def live_component do
60+
quote do
61+
use Phoenix.LiveComponent
62+
63+
unquote(html_helpers())
64+
end
65+
end
66+
67+
def html do
68+
quote do
69+
use Phoenix.Component
70+
71+
import Phoenix.Controller,
72+
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
73+
74+
unquote(html_helpers())
75+
end
76+
end
77+
78+
defp html_helpers do
79+
quote do
80+
import Phoenix.HTML
81+
82+
alias Phoenix.LiveView.JS
83+
84+
unquote(verified_routes())
85+
end
86+
end
87+
88+
def verified_routes do
89+
quote do
90+
use Phoenix.VerifiedRoutes,
91+
endpoint: PhxExampleWeb.Endpoint,
92+
router: PhxExampleWeb.Router,
93+
statics: PhxExampleWeb.static_paths()
2594
end
2695
end
2796

97+
@doc """
98+
When used, dispatch to the appropriate controller/view/etc.
99+
"""
28100
defmacro __using__(which) when is_atom(which) do
29101
apply(__MODULE__, which, [])
30102
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule PhxExampleWeb.Layouts do
2+
use PhxExampleWeb, :html
3+
4+
embed_templates "layouts/*"
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<main>
2+
<div>
3+
<%= @inner_content %>
4+
</div>
5+
</main>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<meta name="csrf-token" content={get_csrf_token()} />
7+
<.live_title suffix=" · Phoenix Framework">
8+
<%= assigns[:page_title] || "PhxExample" %>
9+
</.live_title>
10+
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
11+
</script>
12+
</head>
13+
<body>
14+
<%= @inner_content %>
15+
</body>
16+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule PhxExampleWeb.ErrorHTML do
2+
use PhxExampleWeb, :html
3+
4+
embed_templates "error_html/*"
5+
6+
def render(template, _assigns) do
7+
Phoenix.Controller.status_message_from_template(template)
8+
end
9+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Oops, Internal Server Error"

examples/apps/phx_example/lib/phx_example_web/controllers/page_controller.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule PhxExampleWeb.PageController do
22
use PhxExampleWeb, :controller
33

44
def index(conn, _params) do
5-
render(conn, "index.html")
5+
render(conn, :index)
66
end
77

88
def error(_, _) do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule PhxExampleWeb.PageHTML do
2+
use PhxExampleWeb, :html
3+
4+
embed_templates "page_html/*"
5+
end
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
defmodule PhxExampleWeb.Endpoint do
22
use Phoenix.Endpoint, otp_app: :phx_example
33

4+
@session_options [
5+
store: :cookie,
6+
key: "_phx_example_key",
7+
signing_salt: "F6n7gjjvL6I61gUB",
8+
same_site: "Lax"
9+
]
10+
11+
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
12+
13+
plug Plug.Static,
14+
at: "/",
15+
from: :phx_example,
16+
gzip: false,
17+
only: PhxExampleWeb.static_paths()
18+
19+
plug Plug.RequestId
20+
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
21+
22+
plug Plug.Parsers,
23+
parsers: [:urlencoded, :multipart, :json],
24+
pass: ["*/*"],
25+
json_decoder: Phoenix.json_library()
26+
27+
plug Plug.MethodOverride
28+
plug Plug.Head
29+
plug Plug.Session, @session_options
430
plug PhxExampleWeb.Router
531
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule PhxExampleWeb.ErrorLive do
2+
use PhxExampleWeb, :live_view
3+
4+
@impl true
5+
def render(assigns) do
6+
~H"""
7+
<div>
8+
<h1><%= @some_variable %></h1>
9+
</div>
10+
"""
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule PhxExampleWeb.HomeLive do
2+
use PhxExampleWeb, :live_view
3+
4+
@impl true
5+
def render(assigns) do
6+
~H"""
7+
<div>
8+
<h1>Home</h1>
9+
<p>Some content</p>
10+
</div>
11+
"""
12+
end
13+
end

examples/apps/phx_example/lib/phx_example_web/router.ex

+8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ defmodule PhxExampleWeb.Router do
33

44
pipeline :browser do
55
plug :accepts, ["html"]
6+
plug :fetch_session
7+
plug :fetch_live_flash
8+
plug :put_root_layout, html: {PhxExampleWeb.Layouts, :root}
9+
plug :protect_from_forgery
10+
plug :put_secure_browser_headers
611
end
712

813
scope "/phx", PhxExampleWeb do
914
pipe_through :browser
1015

16+
live "/home", HomeLive, :index
17+
live "/live_error", ErrorLive, :index
18+
1119
get "/error", PageController, :error
1220
get "/:foo", PageController, :index
1321
end

examples/apps/phx_example/lib/phx_example_web/templates/layout/app.html.eex

-11
This file was deleted.

examples/apps/phx_example/lib/phx_example_web/views/error_view.ex

-11
This file was deleted.

examples/apps/phx_example/lib/phx_example_web/views/layout_view.ex

-3
This file was deleted.

examples/apps/phx_example/lib/phx_example_web/views/page_view.ex

-3
This file was deleted.

examples/apps/phx_example/mix.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule PhxExample.MixProject do
1010
deps_path: "../../deps",
1111
lockfile: "../../mix.lock",
1212
elixir: "~> 1.7",
13-
compilers: [:phoenix] ++ Mix.compilers(),
1413
start_permanent: Mix.env() == :prod,
1514
deps: deps()
1615
]
@@ -28,7 +27,9 @@ defmodule PhxExample.MixProject do
2827
{:new_relic_agent, path: "../../../"},
2928
{:test_support, in_umbrella: true},
3029
{:phoenix, "~> 1.5"},
31-
{:phoenix_html, "~> 2.11"},
30+
{:phoenix_html, "~> 3.3"},
31+
{:phoenix_view, "~> 2.0"},
32+
{:phoenix_live_view, "~> 0.20"},
3233
{:jason, "~> 1.0"},
3334
{:plug_cowboy, "~> 2.0"}
3435
]

0 commit comments

Comments
 (0)