memEx/lib/memex_web.ex

114 lines
2.6 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb do
2021-03-11 21:12:55 -05:00
@moduledoc """
The entrypoint for defining your web interface, such
2023-04-13 23:29:29 -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:
2022-07-25 19:31:54 -04:00
use MemexWeb, :controller
2023-04-13 23:29:29 -04:00
use MemexWeb, :html
2021-03-11 21:12:55 -05:00
2023-04-13 23:29:29 -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-13 23:29:29 -04:00
below. Instead, define additional modules and import
those modules here.
2021-03-11 21:12:55 -05:00
"""
2023-04-13 23:29:29 -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-13 23:29:29 -04:00
use Phoenix.Router, helpers: false
2021-03-11 21:12:55 -05:00
2023-04-13 23:29:29 -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-13 23:29:29 -04:00
import Phoenix.Controller
import Phoenix.LiveView.Router
2021-03-11 21:12:55 -05:00
end
end
2023-04-13 23:29:29 -04:00
def channel do
2021-03-11 21:12:55 -05:00
quote do
2023-04-13 23:29:29 -04:00
use Phoenix.Channel
end
end
2021-03-11 21:12:55 -05:00
2023-04-13 23:29:29 -04:00
def controller do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: MemexWeb.Layouts]
2021-03-11 21:12:55 -05:00
2023-04-13 23:29:29 -04:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Plug.Conn
import MemexWeb.Gettext
unquote(verified_routes())
2021-03-11 21:12:55 -05:00
end
end
def live_view do
quote do
2023-04-13 23:29:29 -04:00
use Phoenix.LiveView,
layout: {MemexWeb.Layouts, :app}
2021-03-11 21:12:55 -05:00
2023-04-13 23:29:29 -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-13 23:29:29 -04:00
unquote(html_helpers())
2021-03-11 21:12:55 -05:00
end
end
2023-04-13 23:29:29 -04:00
def html do
2022-01-22 13:01:36 -05:00
quote do
2024-02-23 22:12:43 -05:00
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
2022-01-22 13:01:36 -05:00
use Phoenix.Component
2023-04-13 23:29:29 -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-22 13:01:36 -05:00
end
end
2023-04-13 23:29:29 -04:00
defp html_helpers do
2021-03-11 21:12:55 -05:00
quote do
2024-02-23 22:12:43 -05:00
use PhoenixHTMLHelpers
import Phoenix.{Component, HTML, HTML.Form}
2023-04-13 23:29:29 -04:00
import MemexWeb.{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-13 23:29:29 -04:00
def verified_routes do
2021-03-11 21:12:55 -05:00
quote do
2023-04-13 23:29:29 -04:00
use Phoenix.VerifiedRoutes,
endpoint: MemexWeb.Endpoint,
router: MemexWeb.Router,
statics: MemexWeb.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