cannery/lib/cannery_web.ex

116 lines
2.7 KiB
Elixir
Raw Normal View History

2021-03-11 21:12:55 -05:00
defmodule CanneryWeb do
@moduledoc """
The entrypoint for defining your web interface, such
2023-04-14 23:34:11 -04:00
as controllers, components, channels, and so on.
2021-03-11 21:12:55 -05:00
This can be used in your application as:
use CanneryWeb, :controller
2023-04-14 23:34:11 -04:00
use CanneryWeb, :html
2021-03-11 21:12:55 -05:00
2023-04-14 23:34:11 -04:00
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
2021-03-11 21:12:55 -05:00
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
2023-04-14 23:34:11 -04:00
below. Instead, define additional modules and import
those modules here.
2021-03-11 21:12:55 -05:00
"""
2023-04-14 23:34:11 -04:00
def static_paths, do: ~w(css js fonts images favicon.ico robots.txt)
def router do
2021-03-11 21:12:55 -05:00
quote do
2023-04-14 23:34:11 -04:00
use Phoenix.Router, helpers: false
2021-03-11 21:12:55 -05:00
2023-04-14 23:34:11 -04:00
# Import common connection and controller functions to use in pipelines
2021-03-11 21:12:55 -05:00
import Plug.Conn
2023-04-14 23:34:11 -04:00
import Phoenix.Controller
import Phoenix.LiveView.Router
2021-03-11 21:12:55 -05:00
end
end
2023-04-14 23:34:11 -04:00
def channel do
2021-03-11 21:12:55 -05:00
quote do
2023-04-14 23:34:11 -04:00
use Phoenix.Channel
end
end
2021-03-11 21:12:55 -05:00
2023-04-14 23:34:11 -04:00
def controller do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: CanneryWeb.Layouts]
2021-03-11 21:12:55 -05:00
2023-04-14 23:34:11 -04:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Plug.Conn
import CanneryWeb.Gettext
unquote(verified_routes())
2021-03-11 21:12:55 -05:00
end
end
def live_view do
quote do
2023-04-14 23:34:11 -04:00
use Phoenix.LiveView,
layout: {CanneryWeb.Layouts, :app}
2022-01-21 20:36:25 -05:00
2023-04-14 23:34:11 -04:00
unquote(html_helpers())
2021-03-11 21:12:55 -05:00
end
end
def live_component do
quote do
use Phoenix.LiveComponent
2023-04-14 23:34:11 -04:00
unquote(html_helpers())
2021-03-11 21:12:55 -05:00
end
end
2023-04-14 23:34:11 -04:00
def html do
2022-01-21 20:36:25 -05:00
quote do
use Phoenix.Component
2023-04-14 23:34:11 -04:00
# Import convenience functions from controllers
import Phoenix.Controller,
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
# Include general helpers for rendering HTML
unquote(html_helpers())
2022-01-21 20:36:25 -05:00
end
end
2023-04-14 23:34:11 -04:00
defp html_helpers do
2021-03-11 21:12:55 -05:00
quote do
2022-03-28 23:05:12 -04:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
2023-04-14 23:34:11 -04:00
use Phoenix.HTML
2021-03-11 21:12:55 -05:00
2022-03-28 23:05:12 -04:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
2023-04-14 23:34:11 -04:00
import Phoenix.Component
import CanneryWeb.{ErrorHelpers, Gettext, CoreComponents, HTMLHelpers}
# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
# Routes generation with the ~p sigil
unquote(verified_routes())
2021-03-11 21:12:55 -05:00
end
end
2023-04-14 23:34:11 -04:00
def verified_routes do
2021-03-11 21:12:55 -05:00
quote do
2023-04-14 23:34:11 -04:00
use Phoenix.VerifiedRoutes,
endpoint: CanneryWeb.Endpoint,
router: CanneryWeb.Router,
statics: CanneryWeb.static_paths()
2021-03-11 21:12:55 -05:00
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