commit 6ce1a29ec590adb0feace2462fd8ff018d24324e
parent 5be29a24e906445654e0f9421e2b6931887c5ac1
Author: srfsh <dev@srf.sh>
Date: Mon, 18 Jul 2022 21:57:20 +0300
web: restructure a bit
This might come in handy when we add Plug middlewares under, maybe,
Zenflows.Web.MW namepsace.
Diffstat:
3 files changed, 52 insertions(+), 52 deletions(-)
diff --git a/src/zenflows/application.ex b/src/zenflows/application.ex
@@ -8,7 +8,7 @@ def start(_type, _args) do
children = [
Zenflows.DB.Repo,
- {Plug.Cowboy, scheme: :http, plug: Zenflows.Web, options: [port: 8000]},
+ {Plug.Cowboy, scheme: :http, plug: Zenflows.Web.Router, options: [port: 8000]},
]
opts = [strategy: :one_for_one, name: Zenflows.Supervisor]
diff --git a/src/zenflows/web.ex b/src/zenflows/web.ex
@@ -1,51 +0,0 @@
-defmodule Zenflows.Web do
-@moduledoc "Plug entrypoint."
-
-use Plug.Router
-
-plug :match
-plug Plug.RequestId
-plug Plug.Logger
-plug Plug.Parsers,
- parsers: [:json, Absinthe.Plug.Parser],
- pass: ["*/*"],
- json_decoder: Jason
-plug :gql_context
-plug :dispatch
-
-forward "/api",
- to: Absinthe.Plug,
- schema: Zenflows.GQL.Schema
-
-forward "/play",
- to: Absinthe.Plug.GraphiQL,
- schema: Zenflows.GQL.Schema,
- interface: :advanced
-
-match _ do
- conn
- |> put_resp_content_type("text/html")
- |> send_resp(404, """
- <a href="/play">go to the playground</a><br/>
- <a href="/api">the api location</a>
- """)
-end
-
-# Set the absinthe context with the data fetched from various headers.
-defp gql_context(conn, _opts) do
- ctx =
- case get_req_header(conn, "zenflows-admin") do
- [key | _] ->
- %{gql_admin: key}
-
- _ ->
- with [user | _] <- get_req_header(conn, "zenflows-user"),
- [sign | _] <- get_req_header(conn, "zenflows-sign") do
- %{gql_user: user, gql_sign: sign}
- else
- _ -> %{}
- end
- end
- Absinthe.Plug.put_options(conn, context: ctx)
-end
-end
diff --git a/src/zenflows/web/router.ex b/src/zenflows/web/router.ex
@@ -0,0 +1,51 @@
+defmodule Zenflows.Web.Router do
+@moduledoc "Plug entrypoint/router."
+
+use Plug.Router
+
+plug :match
+plug Plug.RequestId
+plug Plug.Logger
+plug Plug.Parsers,
+ parsers: [:json, Absinthe.Plug.Parser],
+ pass: ["*/*"],
+ json_decoder: Jason
+plug :gql_context
+plug :dispatch
+
+forward "/api",
+ to: Absinthe.Plug,
+ schema: Zenflows.GQL.Schema
+
+forward "/play",
+ to: Absinthe.Plug.GraphiQL,
+ schema: Zenflows.GQL.Schema,
+ interface: :advanced
+
+match _ do
+ conn
+ |> put_resp_content_type("text/html")
+ |> send_resp(404, """
+ <a href="/play">go to the playground</a><br/>
+ <a href="/api">the api location</a>
+ """)
+end
+
+# Set the absinthe context with the data fetched from various headers.
+defp gql_context(conn, _opts) do
+ ctx =
+ case get_req_header(conn, "zenflows-admin") do
+ [key | _] ->
+ %{gql_admin: key}
+ _ ->
+ with [user | _] <- get_req_header(conn, "zenflows-user"),
+ [sign | _] <- get_req_header(conn, "zenflows-sign") do
+ %{gql_user: user, gql_sign: sign}
+ else _ ->
+ %{}
+ end
+ end
+
+ Absinthe.Plug.put_options(conn, context: ctx)
+end
+end