router.ex (2070B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.Web.Router do 19 @moduledoc "Plug entrypoint/router." 20 21 use Plug.Router 22 23 alias Zenflows.Web.MW 24 25 plug :match 26 plug Plug.RequestId 27 plug Plug.Logger 28 plug Plug.Parsers, 29 parsers: [{:json, json_decoder: Jason}, Absinthe.Plug.Parser], 30 pass: ["*/*"], 31 body_reader: {__MODULE__, :read_body, []} 32 plug MW.GQLContext 33 plug :dispatch 34 35 @init_opts [schema: Zenflows.GQL.Schema] 36 37 forward "/api/file", 38 to: Zenflows.Web.File 39 40 forward "/api", 41 to: Absinthe.Plug, 42 init_opts: @init_opts 43 44 forward "/play", 45 to: Absinthe.Plug.GraphiQL, 46 init_opts: [{:interface, :advanced} | @init_opts] 47 48 @sdl Absinthe.Schema.to_sdl(Zenflows.GQL.Schema) 49 get "/schema" do 50 Plug.Conn.send_resp(conn, 200, @sdl) 51 end 52 53 match _ do 54 conn 55 |> put_resp_content_type("text/html") 56 |> send_resp(404, """ 57 <a href="/play">go to the playground</a><br/> 58 <a href="/api">the api location</a> 59 """) 60 end 61 62 @doc false 63 def read_body(conn, opts) do 64 alias Plug.Conn 65 66 case Conn.read_body(conn, opts) do 67 {:ok, data, conn} -> 68 conn = Conn.assign(conn, :raw_body, data) 69 {:ok, data, conn} 70 {:more, data, conn} -> 71 # Since it'll fail due to too large body, we don't 72 # need to assign. 73 {:more, data, conn} 74 {:error, reason} -> 75 {:error, reason} 76 end 77 end 78 end