aboutsummaryrefslogtreecommitdiff
path: root/lib/waev_web
diff options
context:
space:
mode:
Diffstat (limited to 'lib/waev_web')
-rw-r--r--lib/waev_web/channels/user_socket.ex33
-rw-r--r--lib/waev_web/controllers/page_controller.ex7
-rw-r--r--lib/waev_web/endpoint.ex47
-rw-r--r--lib/waev_web/gettext.ex24
-rw-r--r--lib/waev_web/router.ex26
-rw-r--r--lib/waev_web/templates/layout/app.html.eex31
-rw-r--r--lib/waev_web/templates/page/index.html.eex35
-rw-r--r--lib/waev_web/views/error_helpers.ex44
-rw-r--r--lib/waev_web/views/error_view.ex16
-rw-r--r--lib/waev_web/views/layout_view.ex3
-rw-r--r--lib/waev_web/views/page_view.ex3
11 files changed, 269 insertions, 0 deletions
diff --git a/lib/waev_web/channels/user_socket.ex b/lib/waev_web/channels/user_socket.ex
new file mode 100644
index 0000000..b4a6ff6
--- /dev/null
+++ b/lib/waev_web/channels/user_socket.ex
@@ -0,0 +1,33 @@
+defmodule WaevWeb.UserSocket do
+ use Phoenix.Socket
+
+ ## Channels
+ # channel "room:*", WaevWeb.RoomChannel
+
+ # Socket params are passed from the client and can
+ # be used to verify and authenticate a user. After
+ # verification, you can put default assigns into
+ # the socket that will be set for all channels, ie
+ #
+ # {:ok, assign(socket, :user_id, verified_user_id)}
+ #
+ # To deny connection, return `:error`.
+ #
+ # See `Phoenix.Token` documentation for examples in
+ # performing token verification on connect.
+ def connect(_params, socket, _connect_info) do
+ {:ok, socket}
+ end
+
+ # Socket id's are topics that allow you to identify all sockets for a given user:
+ #
+ # def id(socket), do: "user_socket:#{socket.assigns.user_id}"
+ #
+ # Would allow you to broadcast a "disconnect" event and terminate
+ # all active sockets and channels for a given user:
+ #
+ # WaevWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
+ #
+ # Returning `nil` makes this socket anonymous.
+ def id(_socket), do: nil
+end
diff --git a/lib/waev_web/controllers/page_controller.ex b/lib/waev_web/controllers/page_controller.ex
new file mode 100644
index 0000000..3bb2336
--- /dev/null
+++ b/lib/waev_web/controllers/page_controller.ex
@@ -0,0 +1,7 @@
+defmodule WaevWeb.PageController do
+ use WaevWeb, :controller
+
+ def index(conn, _params) do
+ render(conn, "index.html")
+ end
+end
diff --git a/lib/waev_web/endpoint.ex b/lib/waev_web/endpoint.ex
new file mode 100644
index 0000000..ad20615
--- /dev/null
+++ b/lib/waev_web/endpoint.ex
@@ -0,0 +1,47 @@
+defmodule WaevWeb.Endpoint do
+ use Phoenix.Endpoint, otp_app: :waev
+
+ # The session will be stored in the cookie and signed,
+ # this means its contents can be read but not tampered with.
+ # Set :encryption_salt if you would also like to encrypt it.
+ @session_options [
+ store: :cookie,
+ key: "_waev_key",
+ signing_salt: "oHHex3Ho"
+ ]
+
+ socket "/socket", WaevWeb.UserSocket,
+ websocket: true,
+ longpoll: false
+
+ # Serve at "/" the static files from "priv/static" directory.
+ #
+ # You should set gzip to true if you are running phx.digest
+ # when deploying your static files in production.
+ plug Plug.Static,
+ at: "/",
+ from: :waev,
+ gzip: false,
+ only: ~w(css fonts images js favicon.ico robots.txt)
+
+ # Code reloading can be explicitly enabled under the
+ # :code_reloader configuration of your endpoint.
+ if code_reloading? do
+ socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
+ plug Phoenix.LiveReloader
+ plug Phoenix.CodeReloader
+ end
+
+ plug Plug.RequestId
+ plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
+
+ plug Plug.Parsers,
+ parsers: [:urlencoded, :multipart, :json],
+ pass: ["*/*"],
+ json_decoder: Phoenix.json_library()
+
+ plug Plug.MethodOverride
+ plug Plug.Head
+ plug Plug.Session, @session_options
+ plug WaevWeb.Router
+end
diff --git a/lib/waev_web/gettext.ex b/lib/waev_web/gettext.ex
new file mode 100644
index 0000000..8893769
--- /dev/null
+++ b/lib/waev_web/gettext.ex
@@ -0,0 +1,24 @@
+defmodule WaevWeb.Gettext do
+ @moduledoc """
+ A module providing Internationalization with a gettext-based API.
+
+ By using [Gettext](https://hexdocs.pm/gettext),
+ your module gains a set of macros for translations, for example:
+
+ import WaevWeb.Gettext
+
+ # Simple translation
+ gettext("Here is the string to translate")
+
+ # Plural translation
+ ngettext("Here is the string to translate",
+ "Here are the strings to translate",
+ 3)
+
+ # Domain-based translation
+ dgettext("errors", "Here is the error message to translate")
+
+ See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
+ """
+ use Gettext, otp_app: :waev
+end
diff --git a/lib/waev_web/router.ex b/lib/waev_web/router.ex
new file mode 100644
index 0000000..e98f87a
--- /dev/null
+++ b/lib/waev_web/router.ex
@@ -0,0 +1,26 @@
+defmodule WaevWeb.Router do
+ use WaevWeb, :router
+
+ pipeline :browser do
+ plug :accepts, ["html"]
+ plug :fetch_session
+ plug :fetch_flash
+ plug :protect_from_forgery
+ plug :put_secure_browser_headers
+ end
+
+ pipeline :api do
+ plug :accepts, ["json"]
+ end
+
+ scope "/", WaevWeb do
+ pipe_through :browser
+
+ get "/", PageController, :index
+ end
+
+ # Other scopes may use custom stacks.
+ # scope "/api", WaevWeb do
+ # pipe_through :api
+ # end
+end
diff --git a/lib/waev_web/templates/layout/app.html.eex b/lib/waev_web/templates/layout/app.html.eex
new file mode 100644
index 0000000..3eb7e3a
--- /dev/null
+++ b/lib/waev_web/templates/layout/app.html.eex
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8"/>
+ <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+ <title>Waev ยท Phoenix Framework</title>
+ <link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
+ <%= csrf_meta_tag() %>
+ </head>
+ <body>
+ <header>
+ <section class="container">
+ <nav role="navigation">
+ <ul>
+ <li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
+ </ul>
+ </nav>
+ <a href="https://phoenixframework.org/" class="phx-logo">
+ <img src="<%= Routes.static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
+ </a>
+ </section>
+ </header>
+ <main role="main" class="container">
+ <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
+ <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
+ <%= render @view_module, @view_template, assigns %>
+ </main>
+ <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
+ </body>
+</html>
diff --git a/lib/waev_web/templates/page/index.html.eex b/lib/waev_web/templates/page/index.html.eex
new file mode 100644
index 0000000..8cbd9d8
--- /dev/null
+++ b/lib/waev_web/templates/page/index.html.eex
@@ -0,0 +1,35 @@
+<section class="phx-hero">
+ <h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
+ <p>A productive web framework that<br/>does not compromise speed or maintainability.</p>
+</section>
+
+<section class="row">
+ <article class="column">
+ <h2>Resources</h2>
+ <ul>
+ <li>
+ <a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
+ </li>
+ <li>
+ <a href="https://github.com/phoenixframework/phoenix">Source</a>
+ </li>
+ <li>
+ <a href="https://github.com/phoenixframework/phoenix/blob/v1.4/CHANGELOG.md">v1.4 Changelog</a>
+ </li>
+ </ul>
+ </article>
+ <article class="column">
+ <h2>Help</h2>
+ <ul>
+ <li>
+ <a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
+ </li>
+ <li>
+ <a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
+ </li>
+ <li>
+ <a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
+ </li>
+ </ul>
+ </article>
+</section>
diff --git a/lib/waev_web/views/error_helpers.ex b/lib/waev_web/views/error_helpers.ex
new file mode 100644
index 0000000..09c5bc1
--- /dev/null
+++ b/lib/waev_web/views/error_helpers.ex
@@ -0,0 +1,44 @@
+defmodule WaevWeb.ErrorHelpers do
+ @moduledoc """
+ Conveniences for translating and building error messages.
+ """
+
+ use Phoenix.HTML
+
+ @doc """
+ Generates tag for inlined form input errors.
+ """
+ def error_tag(form, field) do
+ Enum.map(Keyword.get_values(form.errors, field), fn error ->
+ content_tag(:span, translate_error(error), class: "help-block")
+ end)
+ end
+
+ @doc """
+ Translates an error message using gettext.
+ """
+ def translate_error({msg, opts}) do
+ # When using gettext, we typically pass the strings we want
+ # to translate as a static argument:
+ #
+ # # Translate "is invalid" in the "errors" domain
+ # dgettext("errors", "is invalid")
+ #
+ # # Translate the number of files with plural rules
+ # dngettext("errors", "1 file", "%{count} files", count)
+ #
+ # Because the error messages we show in our forms and APIs
+ # are defined inside Ecto, we need to translate them dynamically.
+ # This requires us to call the Gettext module passing our gettext
+ # backend as first argument.
+ #
+ # Note we use the "errors" domain, which means translations
+ # should be written to the errors.po file. The :count option is
+ # set by Ecto and indicates we should also apply plural rules.
+ if count = opts[:count] do
+ Gettext.dngettext(WaevWeb.Gettext, "errors", msg, msg, count, opts)
+ else
+ Gettext.dgettext(WaevWeb.Gettext, "errors", msg, opts)
+ end
+ end
+end
diff --git a/lib/waev_web/views/error_view.ex b/lib/waev_web/views/error_view.ex
new file mode 100644
index 0000000..b594014
--- /dev/null
+++ b/lib/waev_web/views/error_view.ex
@@ -0,0 +1,16 @@
+defmodule WaevWeb.ErrorView do
+ use WaevWeb, :view
+
+ # If you want to customize a particular status code
+ # for a certain format, you may uncomment below.
+ # def render("500.html", _assigns) do
+ # "Internal Server Error"
+ # end
+
+ # By default, Phoenix returns the status message from
+ # the template name. For example, "404.html" becomes
+ # "Not Found".
+ def template_not_found(template, _assigns) do
+ Phoenix.Controller.status_message_from_template(template)
+ end
+end
diff --git a/lib/waev_web/views/layout_view.ex b/lib/waev_web/views/layout_view.ex
new file mode 100644
index 0000000..e5af1b7
--- /dev/null
+++ b/lib/waev_web/views/layout_view.ex
@@ -0,0 +1,3 @@
+defmodule WaevWeb.LayoutView do
+ use WaevWeb, :view
+end
diff --git a/lib/waev_web/views/page_view.ex b/lib/waev_web/views/page_view.ex
new file mode 100644
index 0000000..e82080d
--- /dev/null
+++ b/lib/waev_web/views/page_view.ex
@@ -0,0 +1,3 @@
+defmodule WaevWeb.PageView do
+ use WaevWeb, :view
+end