defmodule OpenApiSpex.Plug.SwaggerUI do @moduledoc """ Module plug that serves SwaggerUI. The full path to the API spec must be given as a plug option. The API spec should be served at the given path, see `OpenApiSpex.Plug.RenderSpec` ## Example scope "/" do pipe_through :browser # Use the default browser stack get "/", MyAppWeb.PageController, :index get "/swaggerui", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi" end # Other scopes may use custom stacks. scope "/api" do pipe_through :api resources "/users", MyAppWeb.UserController, only: [:index, :create, :show] get "/openapi", OpenApiSpex.Plug.RenderSpec, :show end """ @behaviour Plug @html """ Swagger UI
""" @doc """ Initializes the plug. Required params: `[:path]` ## Example use OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi" """ @impl Plug def init(opts) when is_list(opts) do opts |> Enum.into(%{}) |> Map.put_new(:display_operation_id, false) end @impl Plug def call(conn, %{path: path, display_operation_id: display_operation_id}) do csrf_token = Plug.CSRFProtection.get_csrf_token() html = render(path, csrf_token, display_operation_id) conn |> Plug.Conn.put_resp_content_type("text/html") |> Plug.Conn.send_resp(200, html) end require EEx EEx.function_from_string(:defp, :render, @html, [:path, :csrf_token, :display_operation_id]) end