2022-03-21 17:43:24 +13:00
|
|
|
defmodule AshHqWeb.Router do
|
|
|
|
use AshHqWeb, :router
|
|
|
|
|
2022-08-06 14:08:23 +12:00
|
|
|
import AshHqWeb.UserAuth
|
2022-10-23 00:51:22 +13:00
|
|
|
import AshAdmin.Router
|
2022-08-06 14:08:23 +12:00
|
|
|
|
2022-03-21 17:43:24 +13:00
|
|
|
pipeline :browser do
|
|
|
|
plug :accepts, ["html"]
|
|
|
|
plug :fetch_session
|
|
|
|
plug :fetch_live_flash
|
|
|
|
plug :put_root_layout, {AshHqWeb.LayoutView, :root}
|
|
|
|
plug :protect_from_forgery
|
2022-03-26 10:17:01 +13:00
|
|
|
plug AshHqWeb.SessionPlug
|
2022-11-01 20:20:09 +13:00
|
|
|
plug :assign_user_agent
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|
|
|
|
|
2022-08-07 11:22:58 +12:00
|
|
|
pipeline :dead_view_authentication do
|
|
|
|
plug :fetch_current_user
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|
|
|
|
|
2022-08-07 11:22:58 +12:00
|
|
|
pipeline :api do
|
|
|
|
plug :accepts, ["json"]
|
2022-08-06 14:08:23 +12:00
|
|
|
end
|
|
|
|
|
2022-08-29 00:42:24 +12:00
|
|
|
pipeline :admin_basic_auth do
|
|
|
|
plug :basic_auth
|
|
|
|
end
|
|
|
|
|
2022-03-21 17:43:24 +13:00
|
|
|
scope "/", AshHqWeb do
|
|
|
|
pipe_through :browser
|
|
|
|
|
2022-08-07 11:22:58 +12:00
|
|
|
live_session :main,
|
2022-09-09 16:31:50 +12:00
|
|
|
on_mount: [{AshHqWeb.InitAssigns, :default}, {AshHqWeb.LiveUserAuth, :live_user}],
|
2022-11-01 20:20:09 +13:00
|
|
|
root_layout: {AshHqWeb.LayoutView, :root} do
|
2022-03-28 10:26:35 +13:00
|
|
|
live "/", AppViewLive, :home
|
2022-11-01 20:20:09 +13:00
|
|
|
live "/blog", AppViewLive, :blog
|
|
|
|
live "/blog/:slug", AppViewLive, :blog
|
2022-03-28 10:26:35 +13:00
|
|
|
live "/docs/", AppViewLive, :docs_dsl
|
2022-04-05 18:20:36 +12:00
|
|
|
live "/docs/guides/:library/:version/*guide", AppViewLive, :docs_dsl
|
2022-03-29 08:47:43 +13:00
|
|
|
live "/docs/dsl/:library", AppViewLive, :docs_dsl
|
2022-03-28 10:26:35 +13:00
|
|
|
live "/docs/dsl/:library/:version", AppViewLive, :docs_dsl
|
|
|
|
live "/docs/dsl/:library/:version/:extension", AppViewLive, :docs_dsl
|
2022-03-30 18:07:17 +13:00
|
|
|
live "/docs/dsl/:library/:version/:extension/*dsl_path", AppViewLive, :docs_dsl
|
2022-03-30 17:40:17 +13:00
|
|
|
live "/docs/module/:library/:version/:module", AppViewLive, :docs_dsl
|
2022-09-30 05:56:07 +13:00
|
|
|
live "/docs/mix_task/:library/:version/:mix_task", AppViewLive, :docs_dsl
|
2022-08-31 10:18:34 +12:00
|
|
|
|
|
|
|
get "/unsubscribe", MailingListController, :unsubscribe
|
2022-03-26 10:17:01 +13:00
|
|
|
end
|
2022-08-16 13:23:59 +12:00
|
|
|
|
|
|
|
live_session :unauthenticated_only,
|
2022-09-09 16:31:50 +12:00
|
|
|
on_mount: [
|
|
|
|
{AshHqWeb.InitAssigns, :default},
|
|
|
|
{AshHqWeb.LiveUserAuth, :live_user_not_allowed}
|
|
|
|
],
|
2022-11-01 20:20:09 +13:00
|
|
|
root_layout: {AshHqWeb.LayoutView, :root} do
|
2022-08-16 13:23:59 +12:00
|
|
|
live "/users/log_in", AppViewLive, :log_in
|
|
|
|
live "/users/register", AppViewLive, :register
|
|
|
|
live "/users/reset_password", AppViewLive, :reset_password
|
|
|
|
live "/users/reset_password/:token", AppViewLive, :reset_password
|
|
|
|
end
|
|
|
|
|
|
|
|
live_session :authenticated_only,
|
2022-09-09 16:31:50 +12:00
|
|
|
on_mount: [{AshHqWeb.InitAssigns, :default}, {AshHqWeb.LiveUserAuth, :live_user_required}],
|
2022-11-01 20:20:09 +13:00
|
|
|
root_layout: {AshHqWeb.LayoutView, :root} do
|
2022-08-16 13:23:59 +12:00
|
|
|
live "/users/settings", AppViewLive, :user_settings
|
|
|
|
end
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|
|
|
|
|
2022-11-01 20:20:09 +13:00
|
|
|
get "/rss", AshHqWeb.RssController, :rss
|
|
|
|
|
2022-09-16 10:35:33 +12:00
|
|
|
## Api routes
|
|
|
|
scope "/" do
|
|
|
|
forward "/gql", Absinthe.Plug, schema: AshHqWeb.Schema
|
|
|
|
|
|
|
|
forward "/playground",
|
|
|
|
Absinthe.Plug.GraphiQL,
|
|
|
|
schema: AshHqWeb.Schema,
|
|
|
|
interface: :playground
|
|
|
|
end
|
|
|
|
|
2022-08-06 14:08:23 +12:00
|
|
|
## Authentication routes
|
|
|
|
|
|
|
|
scope "/", AshHqWeb do
|
2022-08-07 11:22:58 +12:00
|
|
|
pipe_through [
|
|
|
|
:browser,
|
|
|
|
:dead_view_authentication,
|
|
|
|
:redirect_if_user_is_authenticated,
|
|
|
|
:put_session_layout
|
|
|
|
]
|
2022-08-06 14:08:23 +12:00
|
|
|
|
2022-08-16 13:23:59 +12:00
|
|
|
get "/users/new_session", UserSessionController, :log_in
|
|
|
|
post "/users/new_session", UserSessionController, :log_in
|
2022-08-06 14:08:23 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
scope "/", AshHqWeb do
|
2022-08-07 11:22:58 +12:00
|
|
|
pipe_through [:browser, :dead_view_authentication, :require_authenticated_user]
|
2022-08-06 14:08:23 +12:00
|
|
|
|
|
|
|
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
|
|
|
|
end
|
|
|
|
|
|
|
|
scope "/", AshHqWeb do
|
2022-08-07 11:22:58 +12:00
|
|
|
pipe_through [:browser, :dead_view_authentication]
|
2022-08-06 14:08:23 +12:00
|
|
|
|
2022-10-11 14:21:55 +13:00
|
|
|
post "/users/log_out", UserSessionController, :delete
|
2022-08-06 14:08:23 +12:00
|
|
|
post "/users/confirm", UserConfirmationController, :create
|
|
|
|
get "/users/confirm/:token", UserConfirmationController, :confirm
|
|
|
|
end
|
|
|
|
|
2022-03-21 17:43:24 +13:00
|
|
|
# Enables LiveDashboard only for development
|
|
|
|
#
|
|
|
|
# If you want to use the LiveDashboard in production, you should put
|
|
|
|
# it behind authentication and allow only admins to access it.
|
|
|
|
# If your application does not have an admins-only section yet,
|
|
|
|
# you can use Plug.BasicAuth to set up some basic authentication
|
|
|
|
# as long as you are also using SSL (which you should anyway).
|
2022-08-29 00:42:24 +12:00
|
|
|
import Phoenix.LiveDashboard.Router
|
2022-03-21 17:43:24 +13:00
|
|
|
|
2022-08-29 00:42:24 +12:00
|
|
|
scope "/" do
|
|
|
|
if Mix.env() in [:dev, :test] do
|
2022-08-07 11:22:58 +12:00
|
|
|
pipe_through [:browser, :dead_view_authentication]
|
2022-08-29 00:42:24 +12:00
|
|
|
else
|
|
|
|
pipe_through [:browser, :admin_basic_auth]
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|
2022-08-29 00:42:24 +12:00
|
|
|
|
2022-10-23 00:51:22 +13:00
|
|
|
ash_admin("/admin")
|
|
|
|
|
2022-08-29 00:42:24 +12:00
|
|
|
live_dashboard "/dashboard",
|
|
|
|
metrics: AshHqWeb.Telemetry,
|
|
|
|
ecto_repos: [AshHq.Repo],
|
|
|
|
ecto_psql_extras_options: [long_running_queries: [threshold: "200 milliseconds"]]
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
# Enables the Swoosh mailbox preview in development.
|
|
|
|
#
|
|
|
|
# Note that preview only shows emails that were sent by the same
|
|
|
|
# node running the Phoenix server.
|
|
|
|
if Mix.env() == :dev do
|
|
|
|
scope "/dev" do
|
2022-08-07 11:22:58 +12:00
|
|
|
pipe_through [:browser, :dead_view_authentication]
|
2022-03-21 17:43:24 +13:00
|
|
|
|
|
|
|
forward "/mailbox", Plug.Swoosh.MailboxPreview
|
|
|
|
end
|
|
|
|
end
|
2022-08-29 00:42:24 +12:00
|
|
|
|
|
|
|
defp basic_auth(conn, _opts) do
|
|
|
|
username = System.fetch_env!("ADMIN_AUTH_USERNAME")
|
|
|
|
password = System.fetch_env!("ADMIN_AUTH_PASSWORD")
|
|
|
|
Plug.BasicAuth.basic_auth(conn, username: username, password: password)
|
|
|
|
end
|
2022-11-01 20:20:09 +13:00
|
|
|
|
|
|
|
def assign_user_agent(conn, _opts) do
|
|
|
|
ua = get_req_header(conn, "user-agent")
|
|
|
|
|
|
|
|
case ua do
|
|
|
|
[ua] ->
|
|
|
|
assign(conn, :user_agent, ua)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
assign(conn, :user_agent, "")
|
|
|
|
end
|
|
|
|
end
|
2022-03-21 17:43:24 +13:00
|
|
|
end
|