Skip to content

Commit d384dc1

Browse files
committed
Fix bug of the http module
1 parent fc198d6 commit d384dc1

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

component/http/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func newProxy(s *Server) *Proxy {
1515

1616
// Router 获取路由器
1717
func (p *Proxy) Router() Router {
18-
return &router{app: p.server.app}
18+
return &router{app: p.server.app, proxy: p}
1919
}
2020

2121
// NewMeshClient 新建微服务客户端

component/http/router.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type Router interface {
3232
}
3333

3434
type router struct {
35-
app *fiber.App
35+
app *fiber.App
36+
proxy *Proxy
3637
}
3738

3839
// Get 添加GET请求处理器
@@ -96,7 +97,7 @@ func (r *router) Add(methods []string, path string, handler any, middlewares ...
9697
handlers = append(handlers, h)
9798
case Handler:
9899
handlers = append(handlers, func(ctx fiber.Ctx) error {
99-
return h(&context{Ctx: ctx})
100+
return h(&context{Ctx: ctx, proxy: r.proxy})
100101
})
101102
}
102103
}
@@ -106,7 +107,7 @@ func (r *router) Add(methods []string, path string, handler any, middlewares ...
106107
r.app.Add(methods, path, h, handlers...)
107108
case Handler:
108109
r.app.Add(methods, path, func(ctx fiber.Ctx) error {
109-
return h(&context{Ctx: ctx})
110+
return h(&context{Ctx: ctx, proxy: r.proxy})
110111
}, handlers...)
111112
}
112113

@@ -124,15 +125,16 @@ func (r *router) Group(prefix string, middlewares ...any) Router {
124125
handlers = append(handlers, h)
125126
case Handler:
126127
handlers = append(handlers, func(ctx fiber.Ctx) error {
127-
return h(&context{Ctx: ctx})
128+
return h(&context{Ctx: ctx, proxy: r.proxy})
128129
})
129130
}
130131
}
131132

132-
return &routeGroup{router: r.app.Group(prefix, handlers...)}
133+
return &routeGroup{proxy: r.proxy, router: r.app.Group(prefix, handlers...)}
133134
}
134135

135136
type routeGroup struct {
137+
proxy *Proxy
136138
router fiber.Router
137139
}
138140

@@ -197,7 +199,7 @@ func (r *routeGroup) Add(methods []string, path string, handler any, middlewares
197199
handlers = append(handlers, h)
198200
case Handler:
199201
handlers = append(handlers, func(ctx fiber.Ctx) error {
200-
return h(&context{Ctx: ctx})
202+
return h(&context{Ctx: ctx, proxy: r.proxy})
201203
})
202204
}
203205
}
@@ -207,7 +209,7 @@ func (r *routeGroup) Add(methods []string, path string, handler any, middlewares
207209
r.router.Add(methods, path, h, handlers...)
208210
case Handler:
209211
r.router.Add(methods, path, func(ctx fiber.Ctx) error {
210-
return h(&context{Ctx: ctx})
212+
return h(&context{Ctx: ctx, proxy: r.proxy})
211213
}, handlers...)
212214
}
213215

@@ -225,10 +227,10 @@ func (r *routeGroup) Group(prefix string, middlewares ...any) Router {
225227
handlers = append(handlers, h)
226228
case Handler:
227229
handlers = append(handlers, func(ctx fiber.Ctx) error {
228-
return h(&context{Ctx: ctx})
230+
return h(&context{Ctx: ctx, proxy: r.proxy})
229231
})
230232
}
231233
}
232234

233-
return &routeGroup{router: r.router.Group(prefix, handlers...)}
235+
return &routeGroup{router: r.router.Group(prefix, handlers...), proxy: r.proxy}
234236
}

component/http/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewServer(opts ...Option) *Server {
6161
switch handler := o.middlewares[i].(type) {
6262
case Handler:
6363
s.app.Use(func(ctx fiber.Ctx) error {
64-
return handler(&context{Ctx: ctx})
64+
return handler(&context{Ctx: ctx, proxy: s.proxy})
6565
})
6666
case fiber.Handler:
6767
s.app.Use(handler)

0 commit comments

Comments
 (0)