Serve Swagger spec per request so it works behind a proxy prefix.
The baked-in host and basePath pointed "Try it out" at the app's own listen address, so requests from the browser never reached the server. doc.json is now built from the incoming request, using X-Forwarded-Prefix to restore the path prefix nginx strips before proxying. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ba53b5b5c2
commit
18012fcec0
2 files changed
+54
-1
No files matched your search
@@ -2,6 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"online-back/docs"
|
"online-back/docs"
|
||||||
"online-back/internal/config"
|
"online-back/internal/config"
|
||||||
@@ -9,9 +12,11 @@ import (
|
|||||||
"online-back/pkg/client/postgresql"
|
"online-back/pkg/client/postgresql"
|
||||||
"online-back/pkg/logging"
|
"online-back/pkg/logging"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
swaggerFiles "github.com/swaggo/files"
|
swaggerFiles "github.com/swaggo/files"
|
||||||
ginSwagger "github.com/swaggo/gin-swagger"
|
ginSwagger "github.com/swaggo/gin-swagger"
|
||||||
|
"github.com/swaggo/swag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @title Service APIs
|
// @title Service APIs
|
||||||
@@ -57,7 +62,7 @@ func main() {
|
|||||||
pool := mustConnectPostgresql(cfg, logger)
|
pool := mustConnectPostgresql(cfg, logger)
|
||||||
router := handlermanager.New(cfg, pool, logger)
|
router := handlermanager.New(cfg, pool, logger)
|
||||||
|
|
||||||
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
router.GET("/swagger/*any", swaggerHandler())
|
||||||
|
|
||||||
addr := cfg.Listen.BindIP + ":" + cfg.Listen.Port
|
addr := cfg.Listen.BindIP + ":" + cfg.Listen.Port
|
||||||
logger.Infof("starting server on %s", addr)
|
logger.Infof("starting server on %s", addr)
|
||||||
@@ -66,6 +71,51 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// swaggerHandler serves the Swagger UI, but answers doc.json itself so the
|
||||||
|
// spec can describe the address the browser actually used. Behind a reverse
|
||||||
|
// proxy that strips a path prefix, the baked-in host and basePath would point
|
||||||
|
// "Try it out" at the app's own listen address instead of the public URL.
|
||||||
|
func swaggerHandler() gin.HandlerFunc {
|
||||||
|
ui := ginSwagger.WrapHandler(swaggerFiles.Handler)
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if strings.HasSuffix(c.Param("any"), "/doc.json") {
|
||||||
|
serveSwaggerSpec(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ui(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveSwaggerSpec(c *gin.Context) {
|
||||||
|
doc, err := swag.ReadDoc()
|
||||||
|
if err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var spec map[string]any
|
||||||
|
if err := json.Unmarshal([]byte(doc), &spec); err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scheme := "http"
|
||||||
|
if proto := c.GetHeader("X-Forwarded-Proto"); proto != "" {
|
||||||
|
scheme = proto
|
||||||
|
} else if c.Request.TLS != nil {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
|
basePath, _ := spec["basePath"].(string)
|
||||||
|
prefix := strings.TrimSuffix(c.GetHeader("X-Forwarded-Prefix"), "/")
|
||||||
|
|
||||||
|
spec["schemes"] = []string{scheme}
|
||||||
|
spec["host"] = c.Request.Host
|
||||||
|
spec["basePath"] = prefix + basePath
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, spec)
|
||||||
|
}
|
||||||
|
|
||||||
func mustConnectPostgresql(cfg *config.Config, logger *logging.Logger) *pgxpool.Pool {
|
func mustConnectPostgresql(cfg *config.Config, logger *logging.Logger) *pgxpool.Pool {
|
||||||
pool, err := postgresql.NewClient(context.Background(), 3, cfg.Storage)
|
pool, err := postgresql.NewClient(context.Background(), 3, cfg.Storage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -68,6 +68,9 @@ server {
|
|||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
# The prefix is stripped above, so tell the app what it was: Swagger
|
||||||
|
# needs it to build URLs the browser can actually reach.
|
||||||
|
proxy_set_header X-Forwarded-Prefix /online-backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
location = /favicon.ico { access_log off; log_not_found off; }
|
location = /favicon.ico { access_log off; log_not_found off; }
|
||||||
|
|||||||
Reference in new issue
Block a user