diff options
author | href <href@random.sh> | 2018-01-31 20:36:42 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-01-31 20:36:42 +0100 |
commit | fcb2a082346054108f3f16cffe968450cf961976 (patch) | |
tree | 8e7d2eb9eeebf854e62e08c29b147f34dfeb1881 /lib/lsg_web |
import
Diffstat (limited to 'lib/lsg_web')
-rw-r--r-- | lib/lsg_web/channels/user_socket.ex | 37 | ||||
-rw-r--r-- | lib/lsg_web/controllers/page_controller.ex | 20 | ||||
-rw-r--r-- | lib/lsg_web/endpoint.ex | 57 | ||||
-rw-r--r-- | lib/lsg_web/gettext.ex | 24 | ||||
-rw-r--r-- | lib/lsg_web/router.ex | 26 | ||||
-rw-r--r-- | lib/lsg_web/templates/layout/app.html.eex | 35 | ||||
-rw-r--r-- | lib/lsg_web/templates/page/index.html.eex | 36 | ||||
-rw-r--r-- | lib/lsg_web/templates/page/widget.html.eex | 11 | ||||
-rw-r--r-- | lib/lsg_web/views/error_helpers.ex | 40 | ||||
-rw-r--r-- | lib/lsg_web/views/error_view.ex | 17 | ||||
-rw-r--r-- | lib/lsg_web/views/layout_view.ex | 3 | ||||
-rw-r--r-- | lib/lsg_web/views/page_view.ex | 3 |
12 files changed, 309 insertions, 0 deletions
diff --git a/lib/lsg_web/channels/user_socket.ex b/lib/lsg_web/channels/user_socket.ex new file mode 100644 index 0000000..6fedf18 --- /dev/null +++ b/lib/lsg_web/channels/user_socket.ex @@ -0,0 +1,37 @@ +defmodule LSGWeb.UserSocket do + use Phoenix.Socket + + ## Channels + # channel "room:*", LSGWeb.RoomChannel + + ## Transports + transport :websocket, Phoenix.Transports.WebSocket + # transport :longpoll, Phoenix.Transports.LongPoll + + # 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) 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: + # + # LSGWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) + # + # Returning `nil` makes this socket anonymous. + def id(_socket), do: nil +end diff --git a/lib/lsg_web/controllers/page_controller.ex b/lib/lsg_web/controllers/page_controller.ex new file mode 100644 index 0000000..3d4e444 --- /dev/null +++ b/lib/lsg_web/controllers/page_controller.ex @@ -0,0 +1,20 @@ +defmodule LSGWeb.PageController do + use LSGWeb, :controller + + def index(conn, _params) do + render conn, "index.html" + end + + def icecast(conn, _params) do + conn + |> json(LSG.IcecastAgent.get) + end + + def widget(conn, options) do + icecast = LSG.IcecastAgent.get + conn + |> put_layout(false) + |> render("widget.html", icecast: icecast) + end + +end diff --git a/lib/lsg_web/endpoint.ex b/lib/lsg_web/endpoint.ex new file mode 100644 index 0000000..e05e3f5 --- /dev/null +++ b/lib/lsg_web/endpoint.ex @@ -0,0 +1,57 @@ +defmodule LSGWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :lsg + + socket "/socket", LSGWeb.UserSocket + + # Serve at "/" the static files from "priv/static" directory. + # + # You should set gzip to true if you are running phoenix.digest + # when deploying your static files in production. + plug Plug.Static, + at: "/assets", from: :lsg, 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.Logger + + plug Plug.Parsers, + parsers: [:urlencoded, :multipart, :json], + pass: ["*/*"], + json_decoder: Poison + + plug Plug.MethodOverride + plug Plug.Head + + # 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. + plug Plug.Session, + store: :cookie, + key: "_lsg_key", + signing_salt: "+p7K3wrj" + + plug LSGWeb.Router + + @doc """ + Callback invoked for dynamically configuring the endpoint. + + It receives the endpoint configuration and checks if + configuration should be loaded from the system environment. + """ + def init(_key, config) do + if config[:load_from_system_env] do + port = System.get_env("PORT") || raise "expected the PORT environment variable to be set" + {:ok, Keyword.put(config, :http, [:inet6, port: port])} + else + {:ok, config} + end + end +end diff --git a/lib/lsg_web/gettext.ex b/lib/lsg_web/gettext.ex new file mode 100644 index 0000000..f38a57d --- /dev/null +++ b/lib/lsg_web/gettext.ex @@ -0,0 +1,24 @@ +defmodule LSGWeb.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 LSGWeb.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: :lsg +end diff --git a/lib/lsg_web/router.ex b/lib/lsg_web/router.ex new file mode 100644 index 0000000..773a4c2 --- /dev/null +++ b/lib/lsg_web/router.ex @@ -0,0 +1,26 @@ +defmodule LSGWeb.Router do + use LSGWeb, :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 "/", LSGWeb do + pipe_through :browser # Use the default browser stack + + get "/embed/widget", PageController, :widget + end + + scope "/api", LSGWeb do + pipe_through :api + get "/icecast.json", PageController, :icecast + end +end diff --git a/lib/lsg_web/templates/layout/app.html.eex b/lib/lsg_web/templates/layout/app.html.eex new file mode 100644 index 0000000..0d91f12 --- /dev/null +++ b/lib/lsg_web/templates/layout/app.html.eex @@ -0,0 +1,35 @@ +<!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"> + <meta name="description" content=""> + <meta name="author" content=""> + + <title>Hello LSG!</title> + <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>"> + </head> + + <body> + <div class="container"> + <header class="header"> + <nav role="navigation"> + <ul class="nav nav-pills pull-right"> + <li><a href="http://www.phoenixframework.org/docs">Get Started</a></li> + </ul> + </nav> + <span class="logo"></span> + </header> + + <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p> + <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p> + + <main role="main"> + <%= render @view_module, @view_template, assigns %> + </main> + + </div> <!-- /container --> + <script src="<%= static_path(@conn, "/js/app.js") %>"></script> + </body> +</html> diff --git a/lib/lsg_web/templates/page/index.html.eex b/lib/lsg_web/templates/page/index.html.eex new file mode 100644 index 0000000..0988ea5 --- /dev/null +++ b/lib/lsg_web/templates/page/index.html.eex @@ -0,0 +1,36 @@ +<div class="jumbotron"> + <h2><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h2> + <p class="lead">A productive web framework that<br />does not compromise speed and maintainability.</p> +</div> + +<div class="row marketing"> + <div class="col-lg-6"> + <h4>Resources</h4> + <ul> + <li> + <a href="http://phoenixframework.org/docs/overview">Guides</a> + </li> + <li> + <a href="https://hexdocs.pm/phoenix">Docs</a> + </li> + <li> + <a href="https://github.com/phoenixframework/phoenix">Source</a> + </li> + </ul> + </div> + + <div class="col-lg-6"> + <h4>Help</h4> + <ul> + <li> + <a href="http://groups.google.com/group/phoenix-talk">Mailing list</a> + </li> + <li> + <a href="http://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on freenode IRC</a> + </li> + <li> + <a href="https://twitter.com/elixirphoenix">@elixirphoenix</a> + </li> + </ul> + </div> +</div> diff --git a/lib/lsg_web/templates/page/widget.html.eex b/lib/lsg_web/templates/page/widget.html.eex new file mode 100644 index 0000000..efa382f --- /dev/null +++ b/lib/lsg_web/templates/page/widget.html.eex @@ -0,0 +1,11 @@ +<!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"> +<link rel="stylesheet" href="<%= static_path(@conn, "/assets/css/widget.css") %>"> +<script src="<%= static_path(@conn, "/assets/js/widget.js") %>"></script> +</head> +<body></body> +</html> diff --git a/lib/lsg_web/views/error_helpers.ex b/lib/lsg_web/views/error_helpers.ex new file mode 100644 index 0000000..47906f2 --- /dev/null +++ b/lib/lsg_web/views/error_helpers.ex @@ -0,0 +1,40 @@ +defmodule LSGWeb.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 + # Because error messages were defined within Ecto, we must + # call the Gettext module passing our Gettext backend. We + # also use the "errors" domain as translations are placed + # in the errors.po file. + # Ecto will pass the :count keyword if the error message is + # meant to be pluralized. + # On your own code and templates, depending on whether you + # need the message to be pluralized or not, this could be + # written simply as: + # + # dngettext "errors", "1 file", "%{count} files", count + # dgettext "errors", "is invalid" + # + if count = opts[:count] do + Gettext.dngettext(LSGWeb.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(LSGWeb.Gettext, "errors", msg, opts) + end + end +end diff --git a/lib/lsg_web/views/error_view.ex b/lib/lsg_web/views/error_view.ex new file mode 100644 index 0000000..1a7a92d --- /dev/null +++ b/lib/lsg_web/views/error_view.ex @@ -0,0 +1,17 @@ +defmodule LSGWeb.ErrorView do + use LSGWeb, :view + + def render("404.html", _assigns) do + "Page not found" + end + + def render("500.html", _assigns) do + "Internal server error" + end + + # In case no render clause matches or no + # template is found, let's render it as 500 + def template_not_found(_template, assigns) do + render "500.html", assigns + end +end diff --git a/lib/lsg_web/views/layout_view.ex b/lib/lsg_web/views/layout_view.ex new file mode 100644 index 0000000..39216ed --- /dev/null +++ b/lib/lsg_web/views/layout_view.ex @@ -0,0 +1,3 @@ +defmodule LSGWeb.LayoutView do + use LSGWeb, :view +end diff --git a/lib/lsg_web/views/page_view.ex b/lib/lsg_web/views/page_view.ex new file mode 100644 index 0000000..90c384c --- /dev/null +++ b/lib/lsg_web/views/page_view.ex @@ -0,0 +1,3 @@ +defmodule LSGWeb.PageView do + use LSGWeb, :view +end |