diff options
Diffstat (limited to 'lib/matrix_app_service_web')
-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 |
6 files changed, 169 insertions, 0 deletions
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 |