Skip to content

Commit 1f7f22e

Browse files
committed
[handlers] minor fixes
1 parent 1f4c739 commit 1f7f22e

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

internal/config/module.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"strings"
45
"time"
56

67
"github.com/android-sms-gateway/server/internal/sms-gateway/handlers"
@@ -53,7 +54,7 @@ var Module = fx.Module(
5354
}),
5455
fx.Provide(func(cfg Config) push.Config {
5556
mode := push.ModeFCM
56-
if cfg.Gateway.Mode == "private" {
57+
if cfg.Gateway.Mode == GatewayModePrivate {
5758
mode = push.ModeUpstream
5859
}
5960

@@ -78,15 +79,25 @@ var Module = fx.Module(
7879
}
7980
}),
8081
fx.Provide(func(cfg Config) handlers.Config {
81-
if cfg.HTTP.API.Host == "" {
82+
// Default and normalize API path/host
83+
if cfg.HTTP.API.Path == "" {
8284
cfg.HTTP.API.Path = "/api"
8385
}
86+
// Ensure leading slash and trim trailing slash (except root)
87+
if !strings.HasPrefix(cfg.HTTP.API.Path, "/") {
88+
cfg.HTTP.API.Path = "/" + cfg.HTTP.API.Path
89+
}
90+
if cfg.HTTP.API.Path != "/" && strings.HasSuffix(cfg.HTTP.API.Path, "/") {
91+
cfg.HTTP.API.Path = strings.TrimRight(cfg.HTTP.API.Path, "/")
92+
}
93+
// Guard against misconfigured scheme in host (accept "host[:port]" only)
94+
cfg.HTTP.API.Host = strings.TrimPrefix(strings.TrimPrefix(cfg.HTTP.API.Host, "https://"), "http://")
8495

8596
return handlers.Config{
8697
PublicHost: cfg.HTTP.API.Host,
8798
PublicPath: cfg.HTTP.API.Path,
8899
UpstreamEnabled: cfg.Gateway.Mode == GatewayModePublic,
89-
OpanAPIEnabled: cfg.HTTP.OpenAPI.Enabled,
100+
OpenAPIEnabled: cfg.HTTP.OpenAPI.Enabled,
90101
}
91102
}),
92103
fx.Provide(func(cfg Config) messages.Config {

internal/sms-gateway/handlers/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ type Config struct {
55
PublicPath string
66

77
UpstreamEnabled bool
8-
OpanAPIEnabled bool
8+
OpenAPIEnabled bool
99
}

internal/sms-gateway/handlers/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (h *rootHandler) Register(app *fiber.App) {
2121
}
2222

2323
func (h *rootHandler) registerOpenAPI(router fiber.Router) {
24-
if !h.config.OpanAPIEnabled {
24+
if !h.config.OpenAPIEnabled {
2525
return
2626
}
2727

internal/sms-gateway/openapi/openapi.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ func (s *Handler) Register(router fiber.Router, publicHost, publicPath string) {
2626
func(c *fiber.Ctx) error {
2727
if SwaggerInfo.Host == "" {
2828
SwaggerInfo.Host = c.Hostname()
29-
// SwaggerInfo.BasePath = "/api"
3029
}
3130

32-
scheme := "http"
33-
if c.Secure() {
34-
scheme = "https"
35-
}
36-
SwaggerInfo.Schemes = []string{scheme}
31+
SwaggerInfo.Schemes = []string{c.Protocol()}
3732
return c.Next()
3833
},
3934
etag.New(etag.Config{Weak: true}),

0 commit comments

Comments
 (0)