fix(api/docs): Make redoc load docs.json from public URL

This commit is contained in:
MidoriKurage 2026-03-24 19:19:03 +08:00 committed by kolaente
parent c027d7ef40
commit e8615efe8e
2 changed files with 18 additions and 3 deletions

View File

@ -17,9 +17,13 @@
package v1
import (
"bytes"
_ "embed"
"html/template"
"net/http"
"strings"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
_ "code.vikunja.io/api/pkg/swagger" // To make sure the swag files are properly registered
@ -30,12 +34,13 @@ import (
//go:embed redoc/redoc.html
var redocHTML string
var redocUITemplate = template.Must(template.New("redoc").Parse(redocHTML))
//go:embed redoc/redoc.standalone.js
var redocJS []byte
// DocsJSON serves swagger doc json specs
func DocsJSON(c *echo.Context) error {
doc, err := swag.ReadDoc()
if err != nil {
log.Error(err.Error())
@ -47,7 +52,17 @@ func DocsJSON(c *echo.Context) error {
// RedocUI serves everything needed to provide the redoc ui
func RedocUI(c *echo.Context) error {
return c.HTML(http.StatusOK, redocHTML)
publicURL := config.ServicePublicURL.GetString()
docsURL := strings.TrimRight(publicURL, "/") + "/api/v1/docs.json"
var buf bytes.Buffer
data := map[string]string{"Url": docsURL}
if err := redocUITemplate.Execute(&buf, data); err != nil {
return err
}
return c.HTML(http.StatusOK, buf.String())
}
// RedocJS serves the embedded redoc standalone JavaScript bundle

View File

@ -8,7 +8,7 @@
<style>body{margin:0;padding:0;}</style>
</head>
<body>
<redoc spec-url='/api/v1/docs.json'></redoc>
<redoc spec-url='{{.Url}}'></redoc>
<script src="/api/v1/docs/redoc.standalone.js"></script>
</body>
</html>