Skip to content

Commit 74053b2

Browse files
committed
[openapi] configurable openapi docs
1 parent 2013153 commit 74053b2

File tree

9 files changed

+101
-29
lines changed

9 files changed

+101
-29
lines changed

internal/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ type Gateway struct {
2424
type HTTP struct {
2525
Listen string `yaml:"listen" envconfig:"HTTP__LISTEN"` // listen address
2626
Proxies []string `yaml:"proxies" envconfig:"HTTP__PROXIES"` // proxies
27+
28+
OpenAPI OpenAPI `yaml:"openapi"`
29+
}
30+
31+
type OpenAPI struct {
32+
Enabled bool `yaml:"enabled" envconfig:"HTTP__OPENAPI__ENABLED"`
33+
Host string `yaml:"host" envconfig:"HTTP__OPENAPI__HOST"`
34+
Path string `yaml:"path" envconfig:"HTTP__OPENAPI__PATH"`
2735
}
2836

2937
type Database struct {

internal/config/module.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/messages"
1010
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/push"
1111
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/sse"
12+
"github.com/android-sms-gateway/server/internal/sms-gateway/openapi"
1213
"github.com/capcom6/go-infra-fx/config"
1314
"github.com/capcom6/go-infra-fx/db"
1415
"github.com/capcom6/go-infra-fx/http"
@@ -97,4 +98,11 @@ var Module = fx.Module(
9798
sse.WithKeepAlivePeriod(time.Duration(cfg.SSE.KeepAlivePeriodSeconds) * time.Second),
9899
)
99100
}),
101+
fx.Provide(func(cfg Config) openapi.Config {
102+
return openapi.Config{
103+
Enabled: cfg.HTTP.OpenAPI.Enabled,
104+
Host: cfg.HTTP.OpenAPI.Host,
105+
Path: cfg.HTTP.OpenAPI.Path,
106+
}
107+
}),
100108
)

internal/sms-gateway/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/settings"
1919
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/sse"
2020
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/webhooks"
21+
"github.com/android-sms-gateway/server/internal/sms-gateway/openapi"
2122
"github.com/capcom6/go-infra-fx/cli"
2223
"github.com/capcom6/go-infra-fx/db"
2324
"github.com/capcom6/go-infra-fx/http"
@@ -36,6 +37,7 @@ var Module = fx.Module(
3637
appdb.Module,
3738
http.Module,
3839
validator.Module,
40+
openapi.Module(),
3941
handlers.Module,
4042
auth.Module,
4143
push.Module,
Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
11
package handlers
22

33
import (
4-
"github.com/android-sms-gateway/server/internal/version"
5-
apidoc "github.com/android-sms-gateway/server/pkg/swagger"
4+
"github.com/android-sms-gateway/server/internal/sms-gateway/openapi"
65
"github.com/gofiber/fiber/v2"
7-
"github.com/gofiber/fiber/v2/middleware/etag"
8-
"github.com/gofiber/swagger"
96
)
107

118
type rootHandler struct {
12-
healthHandler *healthHandler
9+
healthHandler *healthHandler
10+
openapiHandler *openapi.Handler
1311
}
1412

1513
func (h *rootHandler) Register(app *fiber.App) {
1614
app.Use(func(c *fiber.Ctx) error {
17-
if c.Path() == "/api" {
18-
return c.Redirect("/docs/", fiber.StatusMovedPermanently)
15+
if c.Path() == "/api" || c.Path() == "/api/" {
16+
return c.Redirect("/api/docs/", fiber.StatusMovedPermanently)
1917
}
2018

2119
return c.Next()
2220
})
2321

2422
h.healthHandler.Register(app)
25-
h.registerDocs(app)
23+
h.openapiHandler.Register(app.Group("/api/docs"))
2624
}
2725

28-
func (h *rootHandler) registerDocs(router fiber.Router) error {
29-
apidoc.SwaggerInfo.Version = version.AppVersion
30-
router.Use("/docs/*",
31-
etag.New(etag.Config{
32-
Weak: true,
33-
}),
34-
swagger.New(swagger.Config{
35-
Layout: "BaseLayout",
36-
}),
37-
)
38-
39-
return nil
40-
}
41-
42-
func newRootHandler(healthHandler *healthHandler) *rootHandler {
26+
func newRootHandler(healthHandler *healthHandler, openapiHandler *openapi.Handler) *rootHandler {
4327
return &rootHandler{
44-
healthHandler: healthHandler,
28+
healthHandler: healthHandler,
29+
openapiHandler: openapiHandler,
4530
}
4631
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package openapi
2+
3+
type Config struct {
4+
Enabled bool
5+
Host string
6+
Path string
7+
}

pkg/swagger/docs.go renamed to internal/sms-gateway/openapi/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Package swagger Code generated by swaggo/swag. DO NOT EDIT
2-
package swagger
1+
// Package openapi Code generated by swaggo/swag. DO NOT EDIT
2+
package openapi
33

44
import "github.com/swaggo/swag"
55

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package openapi
2+
3+
import (
4+
"go.uber.org/fx"
5+
"go.uber.org/zap"
6+
)
7+
8+
func Module() fx.Option {
9+
return fx.Module(
10+
"openapi",
11+
fx.Decorate(func(log *zap.Logger) *zap.Logger {
12+
return log.Named("openapi")
13+
}),
14+
fx.Provide(New),
15+
)
16+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package openapi
2+
3+
import (
4+
"github.com/android-sms-gateway/server/internal/version"
5+
"github.com/gofiber/fiber/v2"
6+
"github.com/gofiber/fiber/v2/middleware/etag"
7+
"github.com/gofiber/swagger"
8+
)
9+
10+
//go:generate swag init --parseDependency --tags=User,System --outputTypes go -d ../../../ -g ./cmd/sms-gateway/main.go -o ../../../internal/sms-gateway/openapi
11+
12+
type Handler struct {
13+
config Config
14+
}
15+
16+
func New(config Config) *Handler {
17+
return &Handler{
18+
config: config,
19+
}
20+
}
21+
22+
func (s *Handler) Register(router fiber.Router) {
23+
if !s.config.Enabled {
24+
return
25+
}
26+
27+
SwaggerInfo.Version = version.AppVersion
28+
SwaggerInfo.Host = s.config.Host
29+
SwaggerInfo.BasePath = s.config.Path
30+
31+
router.Use("*",
32+
// Pre-middleware: set host/scheme dynamically
33+
func(c *fiber.Ctx) error {
34+
if SwaggerInfo.Host == "" {
35+
SwaggerInfo.Host = c.Hostname()
36+
SwaggerInfo.BasePath = "/api"
37+
}
38+
39+
scheme := "http"
40+
if c.Secure() {
41+
scheme = "https"
42+
}
43+
SwaggerInfo.Schemes = []string{scheme}
44+
return c.Next()
45+
},
46+
etag.New(etag.Config{Weak: true}),
47+
swagger.New(swagger.Config{Layout: "BaseLayout"}),
48+
)
49+
}

pkg/swagger/swagger.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)