<!DOCTYPE html>
<html>

<head>
    <link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.30.0/swagger-ui.css">
    <title>OpenAPI power by Index.py</title>
</head>

<body>
    <div id="swagger-ui"></div>
    <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.48.0/swagger-ui-bundle.js"></script>
    <script>
        const loadSwagger = (spec) => {
            SwaggerUIBundle({
                spec,
                dom_id: '#swagger-ui',
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIBundle.SwaggerUIStandalonePreset
                ],
                layout: "BaseLayout",
                deepLinking: true,
                showExtensions: true,
                showCommonExtensions: true,
                persistAuthorization: true
            })
        }

        (async () => {
            const res = await fetch("./json");
            const json = await res.json();
            loadSwagger(json);

            if (res.headers.get("reload") === "false") return;

            // Auto reload
            let version = res.headers.get("hash");
            const evtSource = new EventSource("./heartbeat");
            evtSource.onmessage = function (event) {
                if (event.lastEventId === version) {
                    return
                }
                version = event.lastEventId;
                loadSwagger(JSON.parse(event.data))
            }
        })();

    </script>
</body>

</html>
