2023-05-10 03:24:05 -04:00
|
|
|
package hscontrol
|
2021-10-30 10:29:53 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
2021-12-22 21:43:53 -05:00
|
|
|
"html/template"
|
2021-10-30 10:29:53 -04:00
|
|
|
"net/http"
|
|
|
|
|
2021-11-13 03:39:04 -05:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-30 10:29:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed gen/openapiv2/headscale/v1/headscale.swagger.json
|
|
|
|
var apiV1JSON []byte
|
|
|
|
|
2022-06-17 10:48:04 -04:00
|
|
|
func SwaggerUI(
|
2022-06-26 05:43:17 -04:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-17 10:48:04 -04:00
|
|
|
) {
|
2021-11-14 14:32:03 -05:00
|
|
|
swaggerTemplate := template.Must(template.New("swagger").Parse(`
|
2021-10-30 10:29:53 -04:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@3/swagger-ui.css">
|
|
|
|
|
|
|
|
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js"></script>
|
|
|
|
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js" charset="UTF-8"></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="swagger-ui"></div>
|
|
|
|
<script>
|
|
|
|
window.addEventListener('load', (event) => {
|
|
|
|
const ui = SwaggerUIBundle({
|
|
|
|
url: "/swagger/v1/openapiv2.json",
|
|
|
|
dom_id: '#swagger-ui',
|
|
|
|
presets: [
|
|
|
|
SwaggerUIBundle.presets.apis,
|
|
|
|
SwaggerUIBundle.SwaggerUIStandalonePreset
|
|
|
|
],
|
|
|
|
plugins: [
|
|
|
|
SwaggerUIBundle.plugins.DownloadUrl
|
|
|
|
],
|
|
|
|
deepLinking: true,
|
|
|
|
// TODO(kradalby): Figure out why this does not work
|
|
|
|
// layout: "StandaloneLayout",
|
|
|
|
})
|
|
|
|
window.ui = ui
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>`))
|
|
|
|
|
|
|
|
var payload bytes.Buffer
|
2021-11-14 14:32:03 -05:00
|
|
|
if err := swaggerTemplate.Execute(&payload, struct{}{}); err != nil {
|
2021-10-30 10:29:53 -04:00
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not render Swagger")
|
2022-06-17 10:48:04 -04:00
|
|
|
|
2022-06-26 05:43:17 -04:00
|
|
|
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 06:21:35 -04:00
|
|
|
_, err := writer.Write([]byte("Could not render Swagger"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-11-14 10:46:09 -05:00
|
|
|
|
2021-10-30 10:29:53 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-26 05:43:17 -04:00
|
|
|
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
2022-06-26 06:21:35 -04:00
|
|
|
_, err := writer.Write(payload.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-10-30 10:29:53 -04:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:48:04 -04:00
|
|
|
func SwaggerAPIv1(
|
2022-06-26 05:43:17 -04:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-17 10:48:04 -04:00
|
|
|
) {
|
2022-08-17 00:27:58 -04:00
|
|
|
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2022-06-26 05:43:17 -04:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
2022-06-26 06:21:35 -04:00
|
|
|
if _, err := writer.Write(apiV1JSON); err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2021-10-30 10:29:53 -04:00
|
|
|
}
|