Skip to content

Commit 2267d33

Browse files
committed
Add phoenix live view
1 parent 4ae85a0 commit 2267d33

File tree

5 files changed

+132
-14
lines changed

5 files changed

+132
-14
lines changed

Diff for: README.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
[![Package Version](https://img.shields.io/hexpm/v/tictactoe)](https://hex.pm/packages/tictactoe)
44
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/tictactoe/)
55

6-
```sh
7-
gleam add tictactoe@1
8-
```
9-
```gleam
10-
import tictactoe
11-
12-
pub fn main() {
13-
// TODO: An example of the project in use
14-
}
15-
```
16-
17-
Further documentation can be found at <https://hexdocs.pm/tictactoe>.
18-
196
## Development
207

218
```sh
229
gleam run # Run the project
23-
gleam test # Run the tests
10+
11+
14:40:55.944 [info] Running TicTacToe.Endpoint with cowboy 2.12.0 at 127.0.0.1:5001 (http)
12+
13+
14:40:55.946 [info] Access TicTacToe.Endpoint at http://localhost:5001
2414
```
15+
16+
Phoenix integration was based on Wojtek Mach's
17+
[Phoenix LiveView single file example](https://github.com/wojtekmach/mix_install_examples/blob/main/phoenix_live_view.exs).
18+
Thank you! :heart:.

Diff for: gleam.toml

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ version = "1.0.0"
1414

1515
[dependencies]
1616
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
17+
plug_cowboy = ">= 2.5.0 and < 3.0.0"
18+
jason = ">= 1.0.0 and < 2.0.0"
19+
phoenix = ">= 1.7.0 and < 2.0.0"
20+
phoenix_live_view = ">= 0.19.0 and < 1.0.0"
1721

1822
[dev-dependencies]
1923
gleeunit = ">= 1.0.0 and < 2.0.0"

Diff for: manifest.toml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "castore", version = "1.0.10", build_tools = ["mix"], requirements = [], otp_app = "castore", source = "hex", outer_checksum = "1B0B7EA14D889D9EA21202C43A4FA015EB913021CB535E8ED91946F4B77A8848" },
6+
{ name = "cowboy", version = "2.12.0", build_tools = ["make", "rebar3"], requirements = ["cowlib", "ranch"], otp_app = "cowboy", source = "hex", outer_checksum = "8A7ABE6D183372CEB21CAA2709BEC928AB2B72E18A3911AA1771639BEF82651E" },
7+
{ name = "cowboy_telemetry", version = "0.4.0", build_tools = ["rebar3"], requirements = ["cowboy", "telemetry"], otp_app = "cowboy_telemetry", source = "hex", outer_checksum = "7D98BAC1EE4565D31B62D59F8823DFD8356A169E7FCBB83831B8A5397404C9DE" },
8+
{ name = "cowlib", version = "2.13.0", build_tools = ["make", "rebar3"], requirements = [], otp_app = "cowlib", source = "hex", outer_checksum = "E1E1284DC3FC030A64B1AD0D8382AE7E99DA46C3246B815318A4B848873800A4" },
9+
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
10+
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
11+
{ name = "jason", version = "1.4.4", build_tools = ["mix"], requirements = ["decimal"], otp_app = "jason", source = "hex", outer_checksum = "C5EB0CAB91F094599F94D55BC63409236A8EC69A21A67814529E8D5F6CC90B3B" },
12+
{ name = "mime", version = "2.0.6", build_tools = ["mix"], requirements = [], otp_app = "mime", source = "hex", outer_checksum = "C9945363A6B26D747389AAC3643F8E0E09D30499A138AD64FE8FD1D13D9B153E" },
13+
{ name = "phoenix", version = "1.7.17", build_tools = ["mix"], requirements = ["castore", "jason", "phoenix_pubsub", "phoenix_template", "phoenix_view", "plug", "plug_cowboy", "plug_crypto", "telemetry", "websock_adapter"], otp_app = "phoenix", source = "hex", outer_checksum = "50E8AD537F3F7B0EFB1509B2F75B5C918F697BE6A45D48E49A30D3B7C0E464C9" },
14+
{ name = "phoenix_html", version = "4.1.1", build_tools = ["mix"], requirements = [], otp_app = "phoenix_html", source = "hex", outer_checksum = "F2F2DF5A72BC9A2F510B21497FD7D2B86D932EC0598F0210FED4114ADC546C6F" },
15+
{ name = "phoenix_live_view", version = "0.20.17", build_tools = ["mix"], requirements = ["floki", "jason", "phoenix", "phoenix_html", "phoenix_template", "phoenix_view", "plug", "telemetry"], otp_app = "phoenix_live_view", source = "hex", outer_checksum = "A61D741FFB78C85FDBCA0DE084DA6A48F8CEB5261A79165B5A0B59E5F65CE98B" },
16+
{ name = "phoenix_pubsub", version = "2.1.3", build_tools = ["mix"], requirements = [], otp_app = "phoenix_pubsub", source = "hex", outer_checksum = "BBA06BC1DCFD8CB086759F0EDC94A8BA2BC8896D5331A1E2C2902BF8E36EE502" },
17+
{ name = "phoenix_template", version = "1.0.4", build_tools = ["mix"], requirements = ["phoenix_html"], otp_app = "phoenix_template", source = "hex", outer_checksum = "2C0C81F0E5C6753FAF5CCA2F229C9709919ABA34FAB866D3BC05060C9C444206" },
18+
{ name = "plug", version = "1.16.1", build_tools = ["mix"], requirements = ["mime", "plug_crypto", "telemetry"], otp_app = "plug", source = "hex", outer_checksum = "A13FF6B9006B03D7E33874945B2755253841B238C34071ED85B0E86057F8CDDC" },
19+
{ name = "plug_cowboy", version = "2.7.2", build_tools = ["mix"], requirements = ["cowboy", "cowboy_telemetry", "plug"], otp_app = "plug_cowboy", source = "hex", outer_checksum = "245D8A11EE2306094840C000E8816F0CBED69A23FC0AC2BCF8D7835AE019BB2F" },
20+
{ name = "plug_crypto", version = "2.1.0", build_tools = ["mix"], requirements = [], otp_app = "plug_crypto", source = "hex", outer_checksum = "131216A4B030B8F8CE0F26038BC4421AE60E4BB95C5CF5395E1421437824C4FA" },
21+
{ name = "ranch", version = "1.8.0", build_tools = ["make", "rebar3"], requirements = [], otp_app = "ranch", source = "hex", outer_checksum = "49FBCFD3682FAB1F5D109351B61257676DA1A2FDBE295904176D5E521A2DDFE5" },
22+
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
23+
{ name = "websock", version = "0.5.3", build_tools = ["mix"], requirements = [], otp_app = "websock", source = "hex", outer_checksum = "6105453D7FAC22C712AD66FAB1D45ABDF049868F253CF719B625151460B8B453" },
24+
{ name = "websock_adapter", version = "0.5.8", build_tools = ["mix"], requirements = ["bandit", "plug", "plug_cowboy", "websock"], otp_app = "websock_adapter", source = "hex", outer_checksum = "315B9A1865552212B5F35140AD194E67CE31AF45BCEE443D4ECB96B5FD3F3782" },
25+
]
26+
27+
[requirements]
28+
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
29+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
30+
jason = { version = ">= 1.0.0 and < 2.0.0" }
31+
phoenix = { version = ">= 1.7.0 and < 2.0.0" }
32+
phoenix_live_view = { version = ">= 0.19.0 and < 1.0.0" }
33+
plug_cowboy = { version = ">= 2.5.0 and < 3.0.0" }

Diff for: src/tictactoe.gleam

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import gleam/io
22

33
pub fn main() {
4+
start_phoenix()
45
io.println("Hello from tictactoe!")
56
}
7+
8+
@external(erlang, "Elixir.TicTacToe.Endpoint", "start_endpoint")
9+
fn start_phoenix() -> phoenix

Diff for: src/web.ex

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
defmodule TicTacToe.ErrorView do
2+
def render(template, _), do: Phoenix.Controller.status_message_from_template(template)
3+
end
4+
5+
defmodule TicTacToe.LayoutLive do
6+
use Phoenix.LiveView
7+
8+
defp phx_vsn, do: Application.spec(:phoenix, :vsn)
9+
defp lv_vsn, do: Application.spec(:phoenix_live_view, :vsn)
10+
11+
def render("live.html", assigns) do
12+
~H"""
13+
<script src={"https://cdn.jsdelivr.net/npm/phoenix@#{phx_vsn()}/priv/static/phoenix.min.js"}></script>
14+
<script src={"https://cdn.jsdelivr.net/npm/phoenix_live_view@#{lv_vsn()}/priv/static/phoenix_live_view.min.js"}></script>
15+
<script>
16+
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket)
17+
liveSocket.connect()
18+
</script>
19+
<style>
20+
* { font-size: 1.1em; }
21+
</style>
22+
<%= @inner_content %>
23+
"""
24+
end
25+
end
26+
27+
defmodule TicTacToe.CounterLive do
28+
use Phoenix.LiveView, layout: {TicTacToe.LayoutLive, :live}
29+
30+
def mount(_params, _session, socket) do
31+
{:ok, assign(socket, :count, 0)}
32+
end
33+
34+
def render(assigns) do
35+
~H"""
36+
<%= @count %>
37+
<button phx-click="inc">+</button>
38+
<button phx-click="dec">-</button>
39+
"""
40+
end
41+
42+
def handle_event("inc", _params, socket) do
43+
{:noreply, assign(socket, :count, socket.assigns.count + 1)}
44+
end
45+
46+
def handle_event("dec", _params, socket) do
47+
{:noreply, assign(socket, :count, socket.assigns.count - 1)}
48+
end
49+
end
50+
51+
defmodule TicTacToe.Router do
52+
use Phoenix.Router
53+
import Phoenix.LiveView.Router
54+
55+
pipeline :browser do
56+
plug(:accepts, ["html"])
57+
end
58+
59+
scope "/", TicTacToe do
60+
pipe_through(:browser)
61+
62+
# live("/", LayoutLive, :index)
63+
live("/", CounterLive, :index)
64+
end
65+
end
66+
67+
defmodule TicTacToe.Endpoint do
68+
use Phoenix.Endpoint, otp_app: :sample
69+
socket("/live", Phoenix.LiveView.Socket)
70+
plug(TicTacToe.Router)
71+
72+
def start_endpoint() do
73+
Application.put_env(:sample, TicTacToe.Endpoint,
74+
http: [ip: {127, 0, 0, 1}, port: 5001],
75+
server: true,
76+
live_view: [signing_salt: "aaaaaaaa"],
77+
secret_key_base: String.duplicate("a", 64)
78+
)
79+
80+
{:ok, _} = Supervisor.start_link([TicTacToe.Endpoint], strategy: :one_for_one)
81+
Process.sleep(:infinity)
82+
end
83+
end

0 commit comments

Comments
 (0)