Skip to content

Commit

Permalink
fix: use relative path for openapi handler (#2820)
Browse files Browse the repository at this point in the history
* fix: use relative path for openapi handler

* discard error
  • Loading branch information
gartnera authored Sep 3, 2024
1 parent 74f1ab5 commit 0004bb6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
24 changes: 6 additions & 18 deletions docs/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@ package openapi

import (
"embed"
"html/template"
"net/http"

"github.com/gorilla/mux"
)

const (
apiFile = "openapi.swagger.yaml"
templateFile = "template.tpl"
apiFile = "openapi.swagger.yaml"
htmlFile = "openapi.html"
)

//go:embed openapi.swagger.yaml
var staticFS embed.FS

//go:embed template.tpl
var templateFS embed.FS
//go:embed openapi.html
var html []byte

func RegisterOpenAPIService(router *mux.Router) {
router.Handle("/"+apiFile, http.FileServer(http.FS(staticFS)))
router.HandleFunc("/", openAPIHandler())
}

func openAPIHandler() http.HandlerFunc {
tmpl, err := template.ParseFS(templateFS, templateFile)
if err != nil {
panic(err)
}

return func(w http.ResponseWriter, _ *http.Request) {
err := tmpl.Execute(w, struct {
URL string
}{
"/" + apiFile,
})
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write(html)
}
}
2 changes: 1 addition & 1 deletion docs/openapi/template.tpl → docs/openapi/openapi.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<script>
window.onload = function() {
window.ui = SwaggerUIBundle({
url: {{ .URL }},
url: "./openapi.swagger.yaml",
dom_id: "#swagger-ui",
deepLinking: true,
layout: "BaseLayout",
Expand Down

0 comments on commit 0004bb6

Please sign in to comment.