diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/matrix_app_service.ex | 9 | ||||
-rw-r--r-- | lib/matrix_app_service/application.ex | 32 | ||||
-rw-r--r-- | lib/matrix_app_service_web.ex | 74 | ||||
-rw-r--r-- | lib/matrix_app_service_web/channels/user_socket.ex | 35 | ||||
-rw-r--r-- | lib/matrix_app_service_web/endpoint.ex | 45 | ||||
-rw-r--r-- | lib/matrix_app_service_web/router.ex | 11 | ||||
-rw-r--r-- | lib/matrix_app_service_web/telemetry.ex | 46 | ||||
-rw-r--r-- | lib/matrix_app_service_web/views/error_helpers.ex | 16 | ||||
-rw-r--r-- | lib/matrix_app_service_web/views/error_view.ex | 16 |
9 files changed, 284 insertions, 0 deletions
diff --git a/lib/matrix_app_service.ex b/lib/matrix_app_service.ex new file mode 100644 index 0000000..769a9f0 --- /dev/null +++ b/lib/matrix_app_service.ex @@ -0,0 +1,9 @@ +defmodule MatrixAppService do + @moduledoc """ + MatrixAppService keeps the contexts that define your domain + and business logic. + + Contexts are also responsible for managing your data, regardless + if it comes from the database, an external API or others. + """ +end diff --git a/lib/matrix_app_service/application.ex b/lib/matrix_app_service/application.ex new file mode 100644 index 0000000..648cc70 --- /dev/null +++ b/lib/matrix_app_service/application.ex @@ -0,0 +1,32 @@ +defmodule MatrixAppService.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + def start(_type, _args) do + children = [ + # Start the Telemetry supervisor + MatrixAppServiceWeb.Telemetry, + # Start the PubSub system + {Phoenix.PubSub, name: MatrixAppService.PubSub}, + # Start the Endpoint (http/https) + MatrixAppServiceWeb.Endpoint + # Start a worker by calling: MatrixAppService.Worker.start_link(arg) + # {MatrixAppService.Worker, arg} + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: MatrixAppService.Supervisor] + Supervisor.start_link(children, opts) + end + + # Tell Phoenix to update the endpoint configuration + # whenever the application is updated. + def config_change(changed, _new, removed) do + MatrixAppServiceWeb.Endpoint.config_change(changed, removed) + :ok + end +end diff --git a/lib/matrix_app_service_web.ex b/lib/matrix_app_service_web.ex new file mode 100644 index 0000000..e388fee --- /dev/null +++ b/lib/matrix_app_service_web.ex @@ -0,0 +1,74 @@ +defmodule MatrixAppServiceWeb do + @moduledoc """ + The entrypoint for defining your web interface, such + as controllers, views, channels and so on. + + This can be used in your application as: + + use MatrixAppServiceWeb, :controller + use MatrixAppServiceWeb, :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. Instead, define any helper function in modules + and import those modules here. + """ + + def controller do + quote do + use Phoenix.Controller, namespace: MatrixAppServiceWeb + + import Plug.Conn + alias MatrixAppServiceWeb.Router.Helpers, as: Routes + end + end + + def view do + quote do + use Phoenix.View, + root: "lib/matrix_app_service_web/templates", + namespace: MatrixAppServiceWeb + + # Import convenience functions from controllers + import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1] + + # Include shared imports and aliases for views + unquote(view_helpers()) + 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 + end + end + + defp view_helpers do + quote do + # Import basic rendering functionality (render, render_layout, etc) + import Phoenix.View + + import MatrixAppServiceWeb.ErrorHelpers + alias MatrixAppServiceWeb.Router.Helpers, as: Routes + 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 diff --git a/lib/matrix_app_service_web/channels/user_socket.ex b/lib/matrix_app_service_web/channels/user_socket.ex new file mode 100644 index 0000000..ec2e9fb --- /dev/null +++ b/lib/matrix_app_service_web/channels/user_socket.ex @@ -0,0 +1,35 @@ +defmodule MatrixAppServiceWeb.UserSocket do + use Phoenix.Socket + + ## Channels + # channel "room:*", MatrixAppServiceWeb.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. + @impl true + 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: + # + # MatrixAppServiceWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) + # + # Returning `nil` makes this socket anonymous. + @impl true + def id(_socket), do: nil +end diff --git a/lib/matrix_app_service_web/endpoint.ex b/lib/matrix_app_service_web/endpoint.ex new file mode 100644 index 0000000..0ee00fc --- /dev/null +++ b/lib/matrix_app_service_web/endpoint.ex @@ -0,0 +1,45 @@ +defmodule MatrixAppServiceWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :matrix_app_service + + # 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: "_matrix_app_service_key", + signing_salt: "zE7AHynD" + ] + + socket "/socket", MatrixAppServiceWeb.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: :matrix_app_service, + 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.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 MatrixAppServiceWeb.Router +end diff --git a/lib/matrix_app_service_web/router.ex b/lib/matrix_app_service_web/router.ex new file mode 100644 index 0000000..3a24510 --- /dev/null +++ b/lib/matrix_app_service_web/router.ex @@ -0,0 +1,11 @@ +defmodule MatrixAppServiceWeb.Router do + use MatrixAppServiceWeb, :router + + pipeline :api do + plug :accepts, ["json"] + end + + scope "/api", MatrixAppServiceWeb do + pipe_through :api + end +end diff --git a/lib/matrix_app_service_web/telemetry.ex b/lib/matrix_app_service_web/telemetry.ex new file mode 100644 index 0000000..de3ec2d --- /dev/null +++ b/lib/matrix_app_service_web/telemetry.ex @@ -0,0 +1,46 @@ +defmodule MatrixAppServiceWeb.Telemetry do + use Supervisor + import Telemetry.Metrics + + def start_link(arg) do + Supervisor.start_link(__MODULE__, arg, name: __MODULE__) + end + + @impl true + def init(_arg) do + children = [ + {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} + # Add reporters as children of your supervision tree. + # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} + ] + + Supervisor.init(children, strategy: :one_for_one) + end + + def metrics do + [ + # Phoenix Metrics + summary("phoenix.endpoint.stop.duration", + unit: {:native, :millisecond} + ), + summary("phoenix.router_dispatch.stop.duration", + tags: [:route], + unit: {:native, :millisecond} + ), + + # VM Metrics + summary("vm.memory.total", unit: {:byte, :kilobyte}), + summary("vm.total_run_queue_lengths.total"), + summary("vm.total_run_queue_lengths.cpu"), + summary("vm.total_run_queue_lengths.io") + ] + end + + defp periodic_measurements do + [ + # A module, function and arguments to be invoked periodically. + # This function must call :telemetry.execute/3 and a metric must be added above. + # {MatrixAppServiceWeb, :count_users, []} + ] + end +end diff --git a/lib/matrix_app_service_web/views/error_helpers.ex b/lib/matrix_app_service_web/views/error_helpers.ex new file mode 100644 index 0000000..4eff422 --- /dev/null +++ b/lib/matrix_app_service_web/views/error_helpers.ex @@ -0,0 +1,16 @@ +defmodule MatrixAppServiceWeb.ErrorHelpers do + @moduledoc """ + Conveniences for translating and building error messages. + """ + + @doc """ + Translates an error message. + """ + def translate_error({msg, opts}) do + # Because the error messages we show in our forms and APIs + # are defined inside Ecto, we need to translate them dynamically. + Enum.reduce(opts, msg, fn {key, value}, acc -> + String.replace(acc, "%{#{key}}", to_string(value)) + end) + end +end diff --git a/lib/matrix_app_service_web/views/error_view.ex b/lib/matrix_app_service_web/views/error_view.ex new file mode 100644 index 0000000..2358355 --- /dev/null +++ b/lib/matrix_app_service_web/views/error_view.ex @@ -0,0 +1,16 @@ +defmodule MatrixAppServiceWeb.ErrorView do + use MatrixAppServiceWeb, :view + + # If you want to customize a particular status code + # for a certain format, you may uncomment below. + # def render("500.json", _assigns) do + # %{errors: %{detail: "Internal Server Error"}} + # end + + # By default, Phoenix returns the status message from + # the template name. For example, "404.json" becomes + # "Not Found". + def template_not_found(template, _assigns) do + %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}} + end +end |