1defmodule CometWeb do
2 @moduledoc """
3 The entrypoint for defining your web interface, such
4 as controllers, components, channels, and so on.
5
6 This can be used in your application as:
7
8 use CometWeb, :controller
9 use CometWeb, :html
10
11 The definitions below will be executed for every controller,
12 component, etc, so keep them short and clean, focused
13 on imports, uses and aliases.
14
15 Do NOT define functions inside the quoted expressions
16 below. Instead, define additional modules and import
17 those modules here.
18 """
19
20 def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
21
22 def router do
23 quote do
24 use Phoenix.Router, helpers: false
25
26 # Import common connection and controller functions to use in pipelines
27 import Plug.Conn
28 import Phoenix.Controller
29 import Phoenix.LiveView.Router
30 end
31 end
32
33 def channel do
34 quote do
35 use Phoenix.Channel
36 end
37 end
38
39 def controller do
40 quote do
41 use Phoenix.Controller, formats: [:html, :json]
42
43 use Gettext, backend: CometWeb.Gettext
44
45 import Plug.Conn
46
47 unquote(verified_routes())
48 end
49 end
50
51 def live_view do
52 quote do
53 use Phoenix.LiveView
54
55 unquote(html_helpers())
56 end
57 end
58
59 def live_component do
60 quote do
61 use Phoenix.LiveComponent
62
63 unquote(html_helpers())
64 end
65 end
66
67 def html do
68 quote do
69 use Phoenix.Component
70
71 # Import convenience functions from controllers
72 import Phoenix.Controller,
73 only: [get_csrf_token: 0, view_module: 1, view_template: 1]
74
75 # Include general helpers for rendering HTML
76 unquote(html_helpers())
77 end
78 end
79
80 defp html_helpers do
81 quote do
82 # Translation
83 use Gettext, backend: CometWeb.Gettext
84
85 # HTML escaping functionality
86 import Phoenix.HTML
87 # Core UI components
88 import CometWeb.CoreComponents
89
90 # Common modules used in templates
91 alias Phoenix.LiveView.JS
92 alias CometWeb.Layouts
93
94 # Routes generation with the ~p sigil
95 unquote(verified_routes())
96 end
97 end
98
99 def verified_routes do
100 quote do
101 use Phoenix.VerifiedRoutes,
102 endpoint: CometWeb.Endpoint,
103 router: CometWeb.Router,
104 statics: CometWeb.static_paths()
105 end
106 end
107
108 @doc """
109 When used, dispatch to the appropriate controller/live_view/etc.
110 """
111 defmacro __using__(which) when is_atom(which) do
112 apply(__MODULE__, which, [])
113 end
114end