cannery/lib/cannery_web.ex

114 lines
2.6 KiB
Elixir
Raw Permalink 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
2024-02-23 21:03:57 -05:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
2022-01-21 20:36:25 -05:00
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
2024-02-23 21:03:57 -05:00
use PhoenixHTMLHelpers
import Phoenix.{Component, HTML, HTML.Form}
2023-04-14 23:34:11 -04:00
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