-
-
Save LostKobrakai/f228370ed4d10be51e05862271be85ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Application.put_env(:sample, SamplePhoenixWeb.Endpoint, | |
| http: [ip: {127, 0, 0, 1}, port: 5001], | |
| server: true, | |
| secret_key_base: String.duplicate("a", 64), | |
| debug_errors: true | |
| ) | |
| Mix.install([ | |
| {:plug_cowboy, "~> 2.5"}, | |
| {:jason, "~> 1.0"}, | |
| {:phoenix, "~> 1.7.0"} | |
| ]) | |
| defmodule SamplePhoenixWeb.Convention do | |
| import Phoenix.Controller | |
| def custom_default_views(conn, formats) do | |
| case conn |> controller_module() |> Module.split() |> List.pop_at(-1) do | |
| {"Controller", namespace} -> | |
| Enum.reduce(formats, conn, fn format, conn -> | |
| view_module = Module.concat(namespace ++ [String.upcase("#{format}")]) | |
| put_view(conn, [{format, view_module}]) | |
| end) | |
| _ -> | |
| conn | |
| end | |
| end | |
| end | |
| defmodule SamplePhoenixWeb.Foo.Controller do | |
| # Could all be moved to `use SamplePhoenixWeb, :controller` | |
| use Phoenix.Controller, put_default_views: false | |
| import SamplePhoenixWeb.Convention | |
| plug(:custom_default_views, [:json]) | |
| def index(conn, _) do | |
| render(conn, :index, msg: "Hello") | |
| end | |
| end | |
| defmodule SamplePhoenixWeb.Foo.JSON do | |
| def index(assigns) do | |
| %{data: %{message: assigns.msg}} | |
| end | |
| end | |
| defmodule SamplePhoenixWeb.Router do | |
| use Phoenix.Router | |
| pipeline :browser do | |
| plug(:accepts, ["json"]) | |
| end | |
| scope "/", SamplePhoenixWeb do | |
| pipe_through(:browser) | |
| get("/", Foo.Controller, :index) | |
| end | |
| end | |
| defmodule SamplePhoenixWeb.Endpoint do | |
| use Phoenix.Endpoint, otp_app: :sample | |
| plug(SamplePhoenixWeb.Router) | |
| end | |
| {:ok, _} = Supervisor.start_link([SamplePhoenixWeb.Endpoint], strategy: :one_for_one) | |
| Process.sleep(:infinity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment