diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/application.ex | 24 | ||||
-rw-r--r-- | lib/pleroma/repo.ex | 11 | ||||
-rw-r--r-- | lib/pleroma/web/channels/user_socket.ex | 37 | ||||
-rw-r--r-- | lib/pleroma/web/endpoint.ex | 52 | ||||
-rw-r--r-- | lib/pleroma/web/gettext.ex | 24 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 11 | ||||
-rw-r--r-- | lib/pleroma/web/views/error_helpers.ex | 29 | ||||
-rw-r--r-- | lib/pleroma/web/views/error_view.ex | 17 | ||||
-rw-r--r-- | lib/pleroma/web/web.ex | 63 |
9 files changed, 268 insertions, 0 deletions
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex new file mode 100644 index 000000000..e88a85196 --- /dev/null +++ b/lib/pleroma/application.ex @@ -0,0 +1,24 @@ +defmodule Pleroma.Application do + use Application + + # See http://elixir-lang.org/docs/stable/elixir/Application.html + # for more information on OTP Applications + def start(_type, _args) do + import Supervisor.Spec + + # Define workers and child supervisors to be supervised + children = [ + # Start the Ecto repository + supervisor(Pleroma.Repo, []), + # Start the endpoint when the application starts + supervisor(Pleroma.Web.Endpoint, []), + # Start your own worker by calling: Pleroma.Worker.start_link(arg1, arg2, arg3) + # worker(Pleroma.Worker, [arg1, arg2, arg3]), + ] + + # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: Pleroma.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/lib/pleroma/repo.ex b/lib/pleroma/repo.ex new file mode 100644 index 000000000..7cecd7b38 --- /dev/null +++ b/lib/pleroma/repo.ex @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo do + use Ecto.Repo, otp_app: :pleroma + + @doc """ + Dynamically loads the repository url from the + DATABASE_URL environment variable. + """ + def init(_, opts) do + {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))} + end +end diff --git a/lib/pleroma/web/channels/user_socket.ex b/lib/pleroma/web/channels/user_socket.ex new file mode 100644 index 000000000..7aa8e556e --- /dev/null +++ b/lib/pleroma/web/channels/user_socket.ex @@ -0,0 +1,37 @@ +defmodule Pleroma.Web.UserSocket do + use Phoenix.Socket + + ## Channels + # channel "room:*", Pleroma.Web.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: + # + # Pleroma.Web.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) + # + # Returning `nil` makes this socket anonymous. + def id(_socket), do: nil +end diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex new file mode 100644 index 000000000..a79b0dca9 --- /dev/null +++ b/lib/pleroma/web/endpoint.ex @@ -0,0 +1,52 @@ +defmodule Pleroma.Web.Endpoint do + use Phoenix.Endpoint, otp_app: :pleroma + + socket "/socket", Pleroma.Web.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: "/", from: :pleroma, 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 + 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: "_pleroma_key", + signing_salt: "CqaoopA2" + + plug Pleroma.Web.Router + + @doc """ + Dynamically loads configuration from the system environment + on startup. + + It receives the endpoint configuration from the config files + and must return the updated configuration. + """ + def load_from_system_env(config) do + port = System.get_env("PORT") || raise "expected the PORT environment variable to be set" + {:ok, Keyword.put(config, :http, [:inet6, port: port])} + end +end diff --git a/lib/pleroma/web/gettext.ex b/lib/pleroma/web/gettext.ex new file mode 100644 index 000000000..501545581 --- /dev/null +++ b/lib/pleroma/web/gettext.ex @@ -0,0 +1,24 @@ +defmodule Pleroma.Web.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 Pleroma.Web.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: :pleroma +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex new file mode 100644 index 000000000..e0f1ac12f --- /dev/null +++ b/lib/pleroma/web/router.ex @@ -0,0 +1,11 @@ +defmodule Pleroma.Web.Router do + use Pleroma.Web, :router + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/api", Pleroma.Web do + pipe_through :api + end +end diff --git a/lib/pleroma/web/views/error_helpers.ex b/lib/pleroma/web/views/error_helpers.ex new file mode 100644 index 000000000..3981b270d --- /dev/null +++ b/lib/pleroma/web/views/error_helpers.ex @@ -0,0 +1,29 @@ +defmodule Pleroma.Web.ErrorHelpers do + @moduledoc """ + Conveniences for translating and building error messages. + """ + + @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(Pleroma.Web.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(Pleroma.Web.Gettext, "errors", msg, opts) + end + end +end diff --git a/lib/pleroma/web/views/error_view.ex b/lib/pleroma/web/views/error_view.ex new file mode 100644 index 000000000..6c589d3a1 --- /dev/null +++ b/lib/pleroma/web/views/error_view.ex @@ -0,0 +1,17 @@ +defmodule Pleroma.Web.ErrorView do + use Pleroma.Web, :view + + def render("404.json", _assigns) do + %{errors: %{detail: "Page not found"}} + end + + def render("500.json", _assigns) do + %{errors: %{detail: "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.json", assigns + end +end diff --git a/lib/pleroma/web/web.ex b/lib/pleroma/web/web.ex new file mode 100644 index 000000000..f392af2ca --- /dev/null +++ b/lib/pleroma/web/web.ex @@ -0,0 +1,63 @@ +defmodule Pleroma.Web do + @moduledoc """ + A module that keeps using definitions for controllers, + views and so on. + + This can be used in your application as: + + use Pleroma.Web, :controller + use Pleroma.Web, :view + + The definitions below will be executed for every view, + controller, etc, so keep them short and clean, focused + on imports, uses and aliases. + + Do NOT define functions inside the quoted expressions + below. + """ + + def controller do + quote do + use Phoenix.Controller, namespace: Pleroma.Web + import Plug.Conn + import Pleroma.Web.Router.Helpers + import Pleroma.Web.Gettext + end + end + + def view do + quote do + use Phoenix.View, root: "lib/pleroma/web/templates", + namespace: Pleroma.Web + + # Import convenience functions from controllers + import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1] + + import Pleroma.Web.Router.Helpers + import Pleroma.Web.ErrorHelpers + import Pleroma.Web.Gettext + end + end + + def router do + quote do + use Phoenix.Router + import Plug.Conn + import Phoenix.Controller + end + end + + def channel do + quote do + use Phoenix.Channel + import Pleroma.Web.Gettext + end + end + + @doc """ + When used, dispatch to the appropriate controller/view/etc. + """ + defmacro __using__(which) when is_atom(which) do + apply(__MODULE__, which, []) + end +end |