hello_web.ex 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. defmodule HelloWeb do
  2. @moduledoc """
  3. The entrypoint for defining your web interface, such
  4. as controllers, views, channels and so on.
  5. This can be used in your application as:
  6. use HelloWeb, :controller
  7. use HelloWeb, :html
  8. The definitions below will be executed for every controller,
  9. component, etc, so keep them short and clean, focused
  10. on imports, uses and aliases.
  11. Do NOT define functions inside the quoted expressions
  12. below. Instead, define additional modules and import
  13. those modules here.
  14. """
  15. def static_paths,
  16. do: ~w(assets favicon.svg apple-touch-icon.png robots.txt font-files mask-icon.svg)
  17. def controller do
  18. quote do
  19. use Phoenix.Controller,
  20. namespace: HelloWeb,
  21. formats: [:html, :json],
  22. layouts: [html: HelloWeb.Layouts],
  23. log: false
  24. import Plug.Conn
  25. unquote(verified_routes())
  26. end
  27. end
  28. def component do
  29. quote do
  30. use Phoenix.Component
  31. # Routes generation with the ~p sigil
  32. unquote(verified_routes())
  33. end
  34. end
  35. def html do
  36. quote do
  37. use Phoenix.Component
  38. # Import convenience functions from controllers
  39. import Phoenix.Controller,
  40. only: [get_csrf_token: 0, view_module: 1, view_template: 1]
  41. # Include general helpers for rendering HTML
  42. unquote(html_helpers())
  43. end
  44. end
  45. defp html_helpers do
  46. quote do
  47. # Use all HTML functionality (forms, tags, etc)
  48. import Phoenix.HTML
  49. # Routes generation with the ~p sigil
  50. unquote(verified_routes())
  51. end
  52. end
  53. def verified_routes do
  54. quote do
  55. use Phoenix.VerifiedRoutes,
  56. endpoint: HelloWeb.Endpoint,
  57. router: HelloWeb.Router,
  58. statics: HelloWeb.static_paths()
  59. end
  60. end
  61. def router do
  62. quote do
  63. use Phoenix.Router, helpers: false
  64. # Import common connection and controller functions to use in pipelines
  65. import Plug.Conn
  66. import Phoenix.Controller
  67. end
  68. end
  69. def channel do
  70. quote do
  71. use Phoenix.Channel
  72. end
  73. end
  74. @doc """
  75. When used, dispatch to the appropriate controller/view/etc.
  76. """
  77. defmacro __using__(which) when is_atom(which) do
  78. apply(__MODULE__, which, [])
  79. end
  80. end