commit bfaf0d08a8e1d94ab494fb42df046c427f7c8ee0
parent e1559558a57bfe37187ece311996ef4856bb08bd
Author: srfsh <dev@srf.sh>
Date: Mon, 18 Jul 2022 14:58:24 +0300
web: set absinthe context with data fetched from http headers
Diffstat:
1 file changed, 19 insertions(+), 0 deletions(-)
diff --git a/src/zenflows/web.ex b/src/zenflows/web.ex
@@ -10,6 +10,7 @@ plug Plug.Parsers,
parsers: [:json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Jason
+plug :gql_context
plug :dispatch
forward "/api",
@@ -29,4 +30,22 @@ match _ do
<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