Files
online-backend/app.go
GuwandykA 18012fcec0 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>
2026-08-08 11:42:49 +05:00

126 lines
3.3 KiB
Go
Executable File

package main
import (
"context"
"encoding/json"
"net/http"
"strings"
"online-back/docs"
"online-back/internal/config"
handlermanager "online-back/internal/handlers/manager"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"
)
// @title Service APIs
// @version 1.0
// @description Online payment system admin API.
// @host localhost:5005
// @BasePath /api/admin
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @schemes http
// @tag.name auth
// @tag.description Authentication
// @tag.name province
// @tag.description Provinces
// @tag.name branches
// @tag.description Branches
// @tag.name payment-card
// @tag.description Payment cards
// @tag.name reason-card
// @tag.description Reason cards
// @tag.name loan-type
// @tag.description Loan types
// @tag.name loan-document
// @tag.description Loan required documents
// @tag.name loans-repayment
// @tag.description Loan repayments
// @tag.name card-orders
// @tag.description Card orders
// @tag.name activity-logs
// @tag.description User activity audit trail
func main() {
cfg := config.GetConfig()
logger := logging.GetLogger()
docs.SwaggerInfo.Title = "Service APIs"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Description = "Online payment system admin API."
docs.SwaggerInfo.Host = "localhost:" + cfg.Listen.Port
docs.SwaggerInfo.BasePath = "/api/admin"
docs.SwaggerInfo.Schemes = []string{"http"}
pool := mustConnectPostgresql(cfg, logger)
router := handlermanager.New(cfg, pool, logger)
router.GET("/swagger/*any", swaggerHandler())
addr := cfg.Listen.BindIP + ":" + cfg.Listen.Port
logger.Infof("starting server on %s", addr)
if err := router.Run(addr); err != nil {
logger.Fatal(err)
}
}
// 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 {
pool, err := postgresql.NewClient(context.Background(), 3, cfg.Storage)
if err != nil {
logger.Fatal(err)
}
return pool
}