Skip to content

Commit 80232d9

Browse files
committed
[handlers] rewrite location
1 parent 1f7f22e commit 80232d9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

internal/config/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var Module = fx.Module(
8080
}),
8181
fx.Provide(func(cfg Config) handlers.Config {
8282
// Default and normalize API path/host
83-
if cfg.HTTP.API.Path == "" {
83+
if cfg.HTTP.API.Host == "" {
8484
cfg.HTTP.API.Path = "/api"
8585
}
8686
// Ensure leading slash and trim trailing slash (except root)

internal/sms-gateway/handlers/root.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package handlers
22

33
import (
4+
"fmt"
45
"path"
6+
"strings"
57

68
"github.com/android-sms-gateway/server/internal/sms-gateway/openapi"
79
"github.com/gofiber/fiber/v2"
@@ -15,6 +17,21 @@ type rootHandler struct {
1517
}
1618

1719
func (h *rootHandler) Register(app *fiber.App) {
20+
if h.config.PublicPath != "/api" {
21+
app.Use(func(c *fiber.Ctx) error {
22+
err := c.Next()
23+
24+
location := c.GetRespHeader(fiber.HeaderLocation)
25+
if after, ok := strings.CutPrefix(location, "/api"); ok {
26+
c.Set(fiber.HeaderLocation, path.Join(h.config.PublicPath, after))
27+
}
28+
29+
fmt.Printf("Request: %s %s\n", c.Method(), location)
30+
31+
return err
32+
})
33+
}
34+
1835
h.healthHandler.Register(app)
1936

2037
h.registerOpenAPI(app)
@@ -27,7 +44,7 @@ func (h *rootHandler) registerOpenAPI(router fiber.Router) {
2744

2845
router.Use(func(c *fiber.Ctx) error {
2946
if c.Path() == "/api" || c.Path() == "/api/" {
30-
return c.Redirect(path.Join(h.config.PublicPath, "docs"), fiber.StatusMovedPermanently)
47+
return c.Redirect("/api/docs", fiber.StatusMovedPermanently)
3148
}
3249

3350
return c.Next()

0 commit comments

Comments
 (0)