hello_web.ex 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import HelloWeb.Gettext
  26. unquote(verified_routes())
  27. end
  28. end
  29. def component do
  30. quote do
  31. use Phoenix.Component
  32. import HelloWeb.Gettext
  33. # Routes generation with the ~p sigil
  34. unquote(verified_routes())
  35. end
  36. end
  37. def html do
  38. quote do
  39. use Phoenix.Component
  40. # Import convenience functions from controllers
  41. import Phoenix.Controller,
  42. only: [get_csrf_token: 0, view_module: 1, view_template: 1]
  43. # Include general helpers for rendering HTML
  44. unquote(html_helpers())
  45. end
  46. end
  47. defp html_helpers do
  48. quote do
  49. # Use all HTML functionality (forms, tags, etc)
  50. use Phoenix.HTML
  51. # Core UI Components and translation
  52. import HelloWeb.Gettext
  53. # Routes generation with the ~p sigil
  54. unquote(verified_routes())
  55. end
  56. end
  57. def verified_routes do
  58. quote do
  59. use Phoenix.VerifiedRoutes,
  60. endpoint: HelloWeb.Endpoint,
  61. router: HelloWeb.Router,
  62. statics: HelloWeb.static_paths()
  63. end
  64. end
  65. def router do
  66. quote do
  67. use Phoenix.Router, helpers: false
  68. # Import common connection and controller functions to use in pipelines
  69. import Plug.Conn
  70. import Phoenix.Controller
  71. end
  72. end
  73. def channel do
  74. quote do
  75. use Phoenix.Channel
  76. end
  77. end
  78. @doc """
  79. When used, dispatch to the appropriate controller/view/etc.
  80. """
  81. defmacro __using__(which) when is_atom(which) do
  82. apply(__MODULE__, which, [])
  83. end
  84. end