defmodule Redoc.Plug.RedocUI do @moduledoc """ A Plug for rendering Redoc UI. ## Usage If you're using Phoenix Framework, add Redoc UI by add this plug to the router: ```elixir scope "/api" do ... get "/redoc", Redoc.Plug.RedocUI, spec_url: "/spec/openapi" end ``` ## Options * `spec_url` - The openapi path to fetch. Support both `yaml` and `json`. * `redoc_version` - Specify a Redoc version, default to `latest`. """ @behaviour Plug import Plug.Conn @index_html """ ReDoc """ @impl true def init(opts) do spec_url = Keyword.fetch!(opts, :spec_url) redoc_version = Keyword.get(opts, :redoc_version, "latest") [spec_url: spec_url, redoc_version: redoc_version] end @impl true def call(conn, opts) do conn |> put_resp_content_type("text/html") |> send_resp(200, EEx.eval_string(@index_html, opts)) end end