bonfire-app/lib/web/endpoint_template.ex

105 lines
2.8 KiB
Elixir
Raw Normal View History

2022-04-13 13:55:20 +00:00
defmodule Bonfire.Web.EndpointTemplate do
alias Bonfire.Common.Config
2022-06-13 21:06:17 +00:00
defmacro __using__(_) do
quote do
2022-09-12 04:34:14 +00:00
# make sure this comes before the Phoenix endpoint
use Bonfire.ErrorReporting
2022-06-13 21:06:17 +00:00
import Bonfire.Common.Extend
alias Bonfire.Web.EndpointTemplate
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
use_if_enabled(Absinthe.Phoenix.Endpoint)
2022-06-13 21:06:17 +00:00
if Application.compile_env(:bonfire, :sql_sandbox) do
plug(Phoenix.Ecto.SQL.Sandbox)
end
2022-09-12 04:34:14 +00:00
socket("/live", Phoenix.LiveView.Socket,
websocket: [
connect_info: [
:user_agent,
session: EndpointTemplate.session_options()
2022-09-12 04:34:14 +00:00
]
]
)
2022-06-13 21:06:17 +00:00
if module_enabled?(Bonfire.API.GraphQL.UserSocket) do
2022-09-12 04:34:14 +00:00
socket("/api/socket", Bonfire.API.GraphQL.UserSocket,
2022-06-13 21:06:17 +00:00
websocket: true,
longpoll: false
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
end
# 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.
2022-09-12 04:34:14 +00:00
plug(Plug.Static,
2022-06-13 21:06:17 +00:00
at: "/",
from: :bonfire,
gzip: true,
2022-09-12 04:34:14 +00:00
only:
2022-09-24 23:40:07 +00:00
~w(public assets css fonts images js favicon.ico pwa pwabuilder-sw.js robots.txt cache_manifest.json source.tar.gz index.html)
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.Static,
2022-06-13 21:06:17 +00:00
at: "/data/uploads/",
from: "data/uploads",
gzip: true
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.Static,
2022-06-13 21:06:17 +00:00
at: "/",
from: :livebook,
gzip: true,
only: ~w(images js)
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.Static,
2022-06-13 21:06:17 +00:00
at: "/livebook/",
from: :livebook,
gzip: true,
only: ~w(css images js favicon.ico robots.txt cache_manifest.json)
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-10-02 08:29:20 +00:00
# TODO: serve priv/static from any extensions that have one as well?
2022-09-12 04:34:14 +00:00
plug(Phoenix.LiveDashboard.RequestLogger,
2022-06-13 21:06:17 +00:00
param_key: "request_logger",
cookie_key: "request_logger"
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.RequestId)
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.Parsers,
2022-06-13 21:06:17 +00:00
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
2022-09-12 04:34:14 +00:00
)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Bonfire.ErrorReporting)
2022-06-13 21:06:17 +00:00
2022-09-12 04:34:14 +00:00
plug(Plug.MethodOverride)
plug(Plug.Head)
plug(Plug.Session, EndpointTemplate.session_options())
2022-04-13 13:55:20 +00:00
end
end
def session_options do
# TODO: check that this is changeable at runtime
# 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.
[
store: :cookie,
key: "_bonfire_key",
signing_salt: Config.get!(:signing_salt),
encryption_salt: Config.get!(:encryption_salt),
# 60 days by default
max_age: Config.get(:session_time_to_remember, 60 * 60 * 24 * 60)
]
end
2022-04-13 13:55:20 +00:00
end