Skip to content

Commit

Permalink
Fix bug of the http module
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Feb 19, 2025
1 parent fc198d6 commit d384dc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion component/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func newProxy(s *Server) *Proxy {

// Router 获取路由器
func (p *Proxy) Router() Router {
return &router{app: p.server.app}
return &router{app: p.server.app, proxy: p}
}

// NewMeshClient 新建微服务客户端
Expand Down
20 changes: 11 additions & 9 deletions component/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Router interface {
}

type router struct {
app *fiber.App
app *fiber.App
proxy *Proxy
}

// Get 添加GET请求处理器
Expand Down Expand Up @@ -96,7 +97,7 @@ func (r *router) Add(methods []string, path string, handler any, middlewares ...
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
})
}
}
Expand All @@ -106,7 +107,7 @@ func (r *router) Add(methods []string, path string, handler any, middlewares ...
r.app.Add(methods, path, h, handlers...)
case Handler:
r.app.Add(methods, path, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
}, handlers...)
}

Expand All @@ -124,15 +125,16 @@ func (r *router) Group(prefix string, middlewares ...any) Router {
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
})
}
}

return &routeGroup{router: r.app.Group(prefix, handlers...)}
return &routeGroup{proxy: r.proxy, router: r.app.Group(prefix, handlers...)}
}

type routeGroup struct {
proxy *Proxy
router fiber.Router
}

Expand Down Expand Up @@ -197,7 +199,7 @@ func (r *routeGroup) Add(methods []string, path string, handler any, middlewares
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
})
}
}
Expand All @@ -207,7 +209,7 @@ func (r *routeGroup) Add(methods []string, path string, handler any, middlewares
r.router.Add(methods, path, h, handlers...)
case Handler:
r.router.Add(methods, path, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
}, handlers...)
}

Expand All @@ -225,10 +227,10 @@ func (r *routeGroup) Group(prefix string, middlewares ...any) Router {
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
return h(&context{Ctx: ctx, proxy: r.proxy})
})
}
}

return &routeGroup{router: r.router.Group(prefix, handlers...)}
return &routeGroup{router: r.router.Group(prefix, handlers...), proxy: r.proxy}
}
2 changes: 1 addition & 1 deletion component/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewServer(opts ...Option) *Server {
switch handler := o.middlewares[i].(type) {
case Handler:
s.app.Use(func(ctx fiber.Ctx) error {
return handler(&context{Ctx: ctx})
return handler(&context{Ctx: ctx, proxy: s.proxy})
})
case fiber.Handler:
s.app.Use(handler)
Expand Down

0 comments on commit d384dc1

Please sign in to comment.