Skip to content

Commit f1cd921

Browse files
committed
refactor(backend): don't apply AuthMiddleware for all endpoints
e.g. /api/v1/auth does not need AuthMiddleware.
1 parent 0a16e0a commit f1cd921

File tree

3 files changed

+17
-51
lines changed

3 files changed

+17
-51
lines changed

cmd/backend/dependencies.go

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,6 @@ func AuthStorage(redisClient rueidis.Client) auth.Storage {
3535
return auth.NewRedisStorage(redisClient)
3636
}
3737

38-
// AuthMiddleware creates an auth.Middleware that can be injected into gin.
39-
func AuthMiddleware(storage auth.Storage) Middleware {
40-
return Middleware{
41-
Handler: auth.Middleware(storage),
42-
}
43-
}
44-
45-
// MachineMiddleware creates a machine middleware that can be injected into gin.
46-
func MachineMiddleware() Middleware {
47-
return Middleware{
48-
Handler: httputils.MachineMiddleware(),
49-
}
50-
}
51-
52-
// CorsMiddleware creates a cors middleware that can be injected into gin.
53-
func CorsMiddleware(cfg config.Config) Middleware {
54-
return Middleware{
55-
Handler: cors.New(cors.Config{
56-
AllowOrigins: cfg.AllowedOrigins,
57-
AllowMethods: []string{"GET", "POST", "OPTIONS"},
58-
AllowHeaders: []string{"Content-Type", "User-Agent", "Referer"},
59-
AllowCredentials: true,
60-
}),
61-
}
62-
}
63-
6438
func SqlRunner(cfg config.Config) *sqlrunner.SqlRunner {
6539
return sqlrunner.NewSqlRunner(cfg.SqlRunner)
6640
}
@@ -92,24 +66,29 @@ func AuthService(entClient *ent.Client, storage auth.Storage, config config.Conf
9266
}
9367

9468
// GinEngine creates a gin engine.
95-
func GinEngine(services []httpapi.Service, middlewares []Middleware, gqlgenHandler *handler.Server, cfg config.Config) *gin.Engine {
69+
func GinEngine(services []httpapi.Service, authStorage auth.Storage, gqlgenHandler *handler.Server, cfg config.Config) *gin.Engine {
9670
engine := gin.New()
9771

9872
if err := engine.SetTrustedProxies(cfg.TrustProxies); err != nil {
9973
slog.Error("error setting trusted proxies", "error", err)
10074
}
10175

102-
for _, middleware := range middlewares {
103-
engine.Use(middleware.Handler)
104-
}
105-
10676
engine.Use(gin.Recovery())
107-
108-
engine.GET("/", func(ctx *gin.Context) {
77+
engine.Use(httputils.MachineMiddleware())
78+
engine.Use(cors.New(cors.Config{
79+
AllowOrigins: cfg.AllowedOrigins,
80+
AllowMethods: []string{"GET", "POST", "OPTIONS"},
81+
AllowHeaders: []string{"Content-Type", "User-Agent", "Referer"},
82+
AllowCredentials: true,
83+
}))
84+
85+
router := engine.Group("/")
86+
router.Use(auth.Middleware(authStorage))
87+
router.GET("/", func(ctx *gin.Context) {
10988
handler := playground.Handler("GraphQL playground", "/query")
11089
handler.ServeHTTP(ctx.Writer, ctx.Request)
11190
})
112-
engine.POST("/query", func(ctx *gin.Context) {
91+
router.POST("/query", func(ctx *gin.Context) {
11392
gqlgenHandler.ServeHTTP(ctx.Writer, ctx.Request)
11493
})
11594

@@ -174,19 +153,6 @@ func GinLifecycle(lifecycle fx.Lifecycle, engine *gin.Engine, cfg config.Config)
174153
})
175154
}
176155

177-
// Middleware is a middleware that can be injected into gin.
178-
type Middleware struct {
179-
Handler gin.HandlerFunc
180-
}
181-
182-
// AnnotateMiddleware annotates a middleware function to be injected into gin.
183-
func AnnotateMiddleware(f any) any {
184-
return fx.Annotate(
185-
f,
186-
fx.ResultTags(`group:"middlewares"`),
187-
)
188-
}
189-
190156
// AnnotateService annotates a service function to be injected into gin.
191157
func AnnotateService(f any) any {
192158
return fx.Annotate(

cmd/backend/server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ func main() {
1616
fx.Provide(
1717
AuthStorage,
1818
SqlRunner,
19-
AnnotateMiddleware(AuthMiddleware),
20-
AnnotateMiddleware(MachineMiddleware),
21-
AnnotateMiddleware(CorsMiddleware),
2219
AnnotateService(AuthService),
2320
GqlgenHandler,
2421
fx.Annotate(
2522
GinEngine,
26-
fx.ParamTags(`group:"services"`, `group:"middlewares"`),
23+
fx.ParamTags(`group:"services"`),
2724
),
2825
),
2926
fx.Invoke(GinLifecycle),

httpapi/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
Database Playground 大部分的 API 均以 GraphQL 形式提供 (`/query`),但部分為 BFF (Backend for Frontend) 設定的 Stateful Endpoints 則是以 HTTP API 進行設計,並以 `/api` 為開頭。
44

5+
> [!WARNING]
6+
> 注意 HTTP API 不會帶入 AuthMiddleware。如果你的 API 需要鑒權,請手動帶入 `auth.Middleware`
7+
58
- [認證](./auth):相關方法均列於 `/api/auth` 路徑底下。

0 commit comments

Comments
 (0)