238 lines
6 KiB
Elixir
238 lines
6 KiB
Elixir
Application.put_env(:Sfe, :ecto_repos, [Sfe.Repo])
|
|
Application.put_env(:Sfe, :generators, binary_id: true)
|
|
Application.put_env(:Sfe, Sfe.Endpoint, live_reload: [
|
|
|
|
patterns: [
|
|
# ~r{priv/static/.*(js|css|png|jpeg|jpg|gif)$},
|
|
# ~r{lib/my_app_web/views/.*(ex)$},
|
|
# ~r{lib/my_app_web/templates/.*(eex)$}
|
|
~r{main.ex},
|
|
]
|
|
])
|
|
|
|
# application
|
|
defmodule Sfe do
|
|
defmodule Application do
|
|
use Elixir.Application
|
|
|
|
require Logger
|
|
|
|
def start(_type, _args) do
|
|
Logger.debug("Sfe starting...")
|
|
|
|
# config
|
|
counter = :counters.new(1, [])
|
|
dbg(counter)
|
|
|
|
Elixir.Application.put_env(:sfe, Sfe.Endpoint,
|
|
http: [ip: {127, 0, 0, 1}, port: 5001],
|
|
pubsub_server: Sfe.PubSub,
|
|
adapter: Bandit.PhoenixAdapter,
|
|
debug_errors: true,
|
|
code_reloader: true,
|
|
server: true,
|
|
live_view: [signing_salt: "aaaaaaaa"],
|
|
secret_key_base: String.duplicate("a", 64)
|
|
)
|
|
|
|
Elixir.Application.put_env(:sfe, Sfe.HomeLive, counter_ref: counter)
|
|
|
|
children = [
|
|
Sfe.Endpoint,
|
|
{Phoenix.PubSub, name: Sfe.PubSub}
|
|
]
|
|
|
|
opts = [strategy: :one_for_one, name: Sfe.Supervisor]
|
|
Supervisor.start_link(children, opts)
|
|
end
|
|
end
|
|
|
|
defmodule ErrorView do
|
|
def render(template, _), do: Phoenix.Controller.status_message_from_template(template)
|
|
end
|
|
|
|
defmodule Router do
|
|
use Phoenix.Router
|
|
import Phoenix.LiveView.Router
|
|
|
|
pipeline :browser do
|
|
plug(:accepts, ["html"])
|
|
plug(:fetch_session)
|
|
plug(:fetch_live_flash)
|
|
# plug(:put_root_layout, {LyricsWeb.Layouts, :root})
|
|
plug(:protect_from_forgery)
|
|
plug(:put_secure_browser_headers)
|
|
# plug(:fetch_current_user)
|
|
end
|
|
|
|
scope "/", Sfe do
|
|
pipe_through(:browser)
|
|
|
|
live("/", HomeLive, :index)
|
|
end
|
|
end
|
|
|
|
defmodule Endpoint do
|
|
use Phoenix.Endpoint, otp_app: :sfe
|
|
|
|
plug(Plug.RequestId)
|
|
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
|
|
|
|
@session_options [
|
|
store: :cookie,
|
|
key: "_lyrics_key",
|
|
signing_salt: "1MoCZ/LL",
|
|
same_site: "Lax"
|
|
]
|
|
|
|
# plug Plug.Static,
|
|
# at: "/",
|
|
# from: :lyrics,
|
|
# gzip: false,
|
|
# only: LyricsWeb.static_paths()
|
|
|
|
IO.inspect(code_reloading?)
|
|
dbg(code_reloading?)
|
|
if code_reloading? do
|
|
socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket)
|
|
plug(Phoenix.LiveReloader)
|
|
dbg(Phoenix.LiveReloader)
|
|
plug(Phoenix.CodeReloader)
|
|
# plug Phoenix.Ecto.CheckRepoStatus, otp_app: :lyrics
|
|
end
|
|
|
|
require Logger
|
|
Logger.warning("#{code_reloading?}")
|
|
|
|
plug(Plug.Session, @session_options)
|
|
|
|
socket("/live", Phoenix.LiveView.Socket,
|
|
websocket: [connect_info: [session: @session_options]]
|
|
)
|
|
|
|
plug(Plug.Parsers,
|
|
parsers: [:urlencoded, :multipart, :json],
|
|
pass: ["*/*"],
|
|
json_decoder: Phoenix.json_library()
|
|
)
|
|
|
|
plug(Sfe.Router)
|
|
end
|
|
end
|
|
|
|
defmodule Sfe.HomeLive do
|
|
use Phoenix.LiveView, layout: {__MODULE__, :live}
|
|
|
|
@topic "counter"
|
|
|
|
defp counter_ref(), do: Application.get_env(:sfe, __MODULE__)[:counter_ref]
|
|
defp counter_value(), do: :counters.get(counter_ref(), 1)
|
|
|
|
def mount(_params, _session, socket) do
|
|
Sfe.Endpoint.subscribe(@topic)
|
|
{:ok, assign(socket, :count, counter_value())}
|
|
end
|
|
|
|
defp phx_vsn, do: Application.spec(:phoenix, :vsn)
|
|
defp lv_vsn, do: Application.spec(:phoenix_live_view, :vsn)
|
|
|
|
def render("live.html", assigns) do
|
|
~H"""
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<meta name="csrf-token" content={Phoenix.Controller.get_csrf_token()} />
|
|
</head>
|
|
<body>
|
|
<script src={"https://cdn.jsdelivr.net/npm/phoenix@#{phx_vsn()}/priv/static/phoenix.min.js"}></script>
|
|
<script src={"https://cdn.jsdelivr.net/npm/phoenix_live_view@#{lv_vsn()}/priv/static/phoenix_live_view.min.js"}></script>
|
|
<script>
|
|
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
|
|
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket, {params: {_csrf_token: csrfToken}})
|
|
liveSocket.connect()
|
|
</script>
|
|
<style>
|
|
* { font-size: 1.1em; }
|
|
</style>
|
|
<%= @inner_content %>
|
|
</body>
|
|
"""
|
|
end
|
|
|
|
def render(assigns) do
|
|
~H"""
|
|
Multiplayer Counter
|
|
<%= @count %>
|
|
<button phx-click="inc">+</button>
|
|
<button phx-click="dec">-</button>
|
|
"""
|
|
end
|
|
|
|
def handle_event("inc", _params, socket) do
|
|
dbg(:counters.add(counter_ref(), 1, 1))
|
|
counter_value(socket)
|
|
end
|
|
|
|
def handle_event("dec", _params, socket) do
|
|
dbg(:counters.sub(counter_ref(), 1, 1))
|
|
counter_value(socket)
|
|
end
|
|
|
|
def handle_info(%{topic: "counter", event: "counter", payload: cval}, socket) do
|
|
{:noreply, assign(socket, :count, cval)}
|
|
end
|
|
|
|
defp counter_value(socket) do
|
|
Sfe.Endpoint.broadcast_from(self(), @topic, "counter", counter_value())
|
|
{:noreply, assign(socket, :count, counter_value())}
|
|
end
|
|
end
|
|
|
|
defmodule UserGroup do
|
|
use Ecto.Schema
|
|
schema "users_groups" do
|
|
field(:user_id)
|
|
field(:group_id)
|
|
timestamps()
|
|
end
|
|
end
|
|
|
|
defmodule User do
|
|
use Ecto.Schema
|
|
import Ecto.Changeset
|
|
|
|
schema "users" do
|
|
field(:email)
|
|
field(:avatar)
|
|
many_to_many(:groups, Group, join_through: UserGroup)
|
|
timestamps()
|
|
end
|
|
|
|
def create(struct \\ %__MODULE__{}, params) do
|
|
struct
|
|
|> cast(params, @required)
|
|
|> validate_required([:avatar, :email, :name])
|
|
|> validate_format(:avatar, :url)
|
|
|> validate_url_is_an_image(:avatar)
|
|
|> validate_format(:email, ~r/@/)
|
|
|> validate_length(:email, {:gt, 3}) # I don't know how to use this off the top of my head
|
|
|> validate_length(:name, 2)
|
|
# and more validations for name and email and whatever
|
|
end
|
|
|
|
def create_from_referral(struct \\ %__MODULE__{}, params) do
|
|
struct
|
|
|> create()
|
|
|> cast(params, [:referrer])
|
|
|> validate_referrer(:referrer)
|
|
end
|
|
end
|
|
|
|
defmodule Group do
|
|
use Ecto.Schema
|
|
schema "groups" do
|
|
field(:name)
|
|
many_to_many(:users, User, join_through: UserGroup)
|
|
timestamps()
|
|
end
|
|
end
|