zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 44c3be062c1767d03a06dd5e41e45243cc5f4762
parent 6ce1a29ec590adb0feace2462fd8ff018d24324e
Author: srfsh <dev@srf.sh>
Date:   Mon, 18 Jul 2022 23:43:49 +0300

web/router: save raw body in assigns

Diffstat:
Msrc/zenflows/web/router.ex | 21+++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/zenflows/web/router.ex b/src/zenflows/web/router.ex @@ -7,9 +7,9 @@ plug :match plug Plug.RequestId plug Plug.Logger plug Plug.Parsers, - parsers: [:json, Absinthe.Plug.Parser], + parsers: [{:json, json_decoder: Jason}, Absinthe.Plug.Parser], pass: ["*/*"], - json_decoder: Jason + body_reader: {__MODULE__, :read_body, []} plug :gql_context plug :dispatch @@ -48,4 +48,21 @@ defp gql_context(conn, _opts) do Absinthe.Plug.put_options(conn, context: ctx) end + +@doc false +def read_body(conn, opts) do + alias Plug.Conn + + case Conn.read_body(conn, opts) do + {:ok, data, conn} -> + conn = Conn.assign(conn, :raw_body, data) + {:ok, data, conn} + {:more, data, conn} -> + # Since it'll fail due to too large body, we don't + # need to assign. + {:more, data, conn} + {:error, reason} -> + {:error, reason} + end +end end