Skip to content

Commit

Permalink
optimize http components
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Nov 11, 2024
1 parent d9cc711 commit 439e4a9
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 116 deletions.
17 changes: 10 additions & 7 deletions component/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type Resp struct {
Code int `json:"code"` // 响应码
Data any `json:"data,omitempty"` // 响应数据
Code int `json:"code"` // 响应码
Message string `json:"message"` // 响应消息
Data any `json:"data,omitempty"` // 响应数据
}

type Context interface {
Expand Down Expand Up @@ -47,20 +48,22 @@ func (c *context) Proxy() *Proxy {
func (c *context) Failure(rst any) error {
switch v := rst.(type) {
case error:
return c.JSON(&Resp{Code: codes.Convert(v).Code()})
code := codes.Convert(v)

return c.JSON(&Resp{Code: code.Code(), Message: code.Message()})
case *codes.Code:
return c.JSON(&Resp{Code: v.Code()})
return c.JSON(&Resp{Code: v.Code(), Message: v.Message()})
default:
return c.JSON(&Resp{Code: codes.Unknown.Code()})
return c.JSON(&Resp{Code: codes.Unknown.Code(), Message: codes.Unknown.Message()})
}
}

// Success 成功响应
func (c *context) Success(data ...any) error {
if len(data) > 0 {
return c.JSON(&Resp{Code: codes.OK.Code(), Data: data[0]})
return c.JSON(&Resp{Code: codes.OK.Code(), Message: codes.OK.Message(), Data: data[0]})
} else {
return c.JSON(&Resp{Code: codes.OK.Code()})
return c.JSON(&Resp{Code: codes.OK.Code(), Message: codes.OK.Message()})
}
}

Expand Down
15 changes: 8 additions & 7 deletions component/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ import (
)

type Proxy struct {
http *Http
server *Server
}

func newProxy(h *Http) *Proxy {
return &Proxy{http: h}
func newProxy(s *Server) *Proxy {
return &Proxy{server: s}
}

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

// NewMeshClient 新建微服务客户端
// target参数可分为两种模式:
// target参数可分为三种模式:
// 服务直连模式: direct://127.0.0.1:8011
// 服务直连模式: direct://711baf8d-8a06-11ef-b7df-f4f19e1f0070
// 服务发现模式: discovery://service_name
func (p *Proxy) NewMeshClient(target string) (transport.Client, error) {
if p.http.opts.transporter == nil {
if p.server.opts.transporter == nil {
return nil, errors.ErrMissTransporter
}

return p.http.opts.transporter.NewClient(target)
return p.server.opts.transporter.NewClient(target)
}
162 changes: 99 additions & 63 deletions component/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,195 +4,231 @@ import (
"github.com/gofiber/fiber/v3"
)

type Handler func(ctx Context) error
type Handler = func(ctx Context) error

type Router interface {
// Get 添加GET请求处理器
Get(path string, handler Handler, middlewares ...Handler) Router
// Post 添加GET请求处理器
Post(path string, handler Handler, middlewares ...Handler) Router
Get(path string, handler any, middlewares ...any) Router
// Post 添加POST请求处理器
Post(path string, handler any, middlewares ...any) Router
// Head 添加HEAD请求处理器
Head(path string, handler Handler, middlewares ...Handler) Router
Head(path string, handler any, middlewares ...any) Router
// Put 添加PUT请求处理器
Put(path string, handler Handler, middlewares ...Handler) Router
Put(path string, handler any, middlewares ...any) Router
// Delete 添加DELETE请求处理器
Delete(path string, handler Handler, middlewares ...Handler) Router
Delete(path string, handler any, middlewares ...any) Router
// Connect 添加CONNECT请求处理器
Connect(path string, handler Handler, middlewares ...Handler) Router
Connect(path string, handler any, middlewares ...any) Router
// Options 添加OPTIONS请求处理器
Options(path string, handler Handler, middlewares ...Handler) Router
Options(path string, handler any, middlewares ...any) Router
// Trace 添加TRACE请求处理器
Trace(path string, handler Handler, middlewares ...Handler) Router
Trace(path string, handler any, middlewares ...any) Router
// Patch 添加PATCH请求处理器
Patch(path string, handler Handler, middlewares ...Handler) Router
Patch(path string, handler any, middlewares ...any) Router
// Add 添加路由处理器
Add(methods []string, path string, handler Handler, middlewares ...Handler) Router
Add(methods []string, path string, handler any, middlewares ...any) Router
// Group 路由组
Group(prefix string, handlers ...Handler) Router
Group(prefix string, handlers ...any) Router
}

type router struct {
app *fiber.App
}

// Get 添加GET请求处理器
func (r *router) Get(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Get(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodGet}, path, handler, middlewares...)
}

// Post 添加POST请求处理器
func (r *router) Post(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Post(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPost}, path, handler, middlewares...)
}

// Head 添加HEAD请求处理器
func (r *router) Head(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Head(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodHead}, path, handler, middlewares...)
}

// Put 添加PUT请求处理器
func (r *router) Put(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Put(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPut}, path, handler, middlewares...)
}

// Delete 添加DELETE请求处理器
func (r *router) Delete(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Delete(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodDelete}, path, handler, middlewares...)
}

// Connect 添加CONNECT请求处理器
func (r *router) Connect(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Connect(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodConnect}, path, handler, middlewares...)
}

// Options 添加OPTIONS请求处理器
func (r *router) Options(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Options(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodOptions}, path, handler, middlewares...)
}

// Trace 添加TRACE请求处理器
func (r *router) Trace(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Trace(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodTrace}, path, handler, middlewares...)
}

// Patch 添加PATCH请求处理器
func (r *router) Patch(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Patch(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPatch}, path, handler, middlewares...)
}

// All 添加任意请求处理器
func (r *router) All(path string, handler Handler, middlewares ...Handler) Router {
func (r *router) All(path string, handler any, middlewares ...any) Router {
return r.Add(fiber.DefaultMethods, path, handler, middlewares...)
}

// Add 添加路由处理器
func (r *router) Add(methods []string, path string, handler Handler, middlewares ...Handler) Router {
func (r *router) Add(methods []string, path string, handler any, middlewares ...any) Router {
handlers := make([]fiber.Handler, 0, len(middlewares))
for i := range middlewares {
middleware := middlewares[i]
handlers = append(handlers, func(ctx fiber.Ctx) error {
return middleware(&context{Ctx: ctx})
})

switch h := middleware.(type) {
case fiber.Handler:
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
})
}
}

switch h := handler.(type) {
case fiber.Handler:
r.app.Add(methods, path, h, handlers...)
case Handler:
r.app.Add(methods, path, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
}, handlers...)
}

r.app.Add(methods, path, func(ctx fiber.Ctx) error {
return handler(&context{Ctx: ctx})
}, handlers...)
return r
}

// Group 路由组
func (r *router) Group(prefix string, handlers ...Handler) Router {
list := make([]fiber.Handler, 0, len(handlers))
for i := range handlers {
handler := handlers[i]
list = append(list, func(ctx fiber.Ctx) error {
return handler(&context{Ctx: ctx})
})
func (r *router) Group(prefix string, middlewares ...any) Router {
handlers := make([]fiber.Handler, 0, len(middlewares))
for i := range middlewares {
middleware := middlewares[i]

switch h := middleware.(type) {
case fiber.Handler:
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
})
}
}

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

type routeGroup struct {
router fiber.Router
}

// Get 添加GET请求处理器
func (r *routeGroup) Get(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Get(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodGet}, path, handler, middlewares...)
}

// Post 添加GET请求处理器
func (r *routeGroup) Post(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Post(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPost}, path, handler, middlewares...)
}

// Head 添加HEAD请求处理器
func (r *routeGroup) Head(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Head(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodHead}, path, handler, middlewares...)
}

// Put 添加PUT请求处理器
func (r *routeGroup) Put(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Put(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPut}, path, handler, middlewares...)
}

// Delete 添加DELETE请求处理器
func (r *routeGroup) Delete(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Delete(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodDelete}, path, handler, middlewares...)
}

// Connect 添加CONNECT请求处理器
func (r *routeGroup) Connect(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Connect(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodConnect}, path, handler, middlewares...)
}

// Options 添加OPTIONS请求处理器
func (r *routeGroup) Options(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Options(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodOptions}, path, handler, middlewares...)
}

// Trace 添加TRACE请求处理器
func (r *routeGroup) Trace(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Trace(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodTrace}, path, handler, middlewares...)
}

// Patch 添加PATCH请求处理器
func (r *routeGroup) Patch(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Patch(path string, handler any, middlewares ...any) Router {
return r.Add([]string{fiber.MethodPatch}, path, handler, middlewares...)
}

// All 添加任意请求处理器
func (r *routeGroup) All(path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) All(path string, handler any, middlewares ...any) Router {
return r.Add(fiber.DefaultMethods, path, handler, middlewares...)
}

// Add 添加路由处理器
func (r *routeGroup) Add(methods []string, path string, handler Handler, middlewares ...Handler) Router {
func (r *routeGroup) Add(methods []string, path string, handler any, middlewares ...any) Router {
handlers := make([]fiber.Handler, 0, len(middlewares))
for i := range middlewares {
middleware := middlewares[i]
handlers = append(handlers, func(ctx fiber.Ctx) error {
return middleware(&context{Ctx: ctx})
})

switch h := middleware.(type) {
case fiber.Handler:
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
})
}
}

switch h := handler.(type) {
case fiber.Handler:
r.router.Add(methods, path, h, handlers...)
case Handler:
r.router.Add(methods, path, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
}, handlers...)
}

r.router.Add(methods, path, func(ctx fiber.Ctx) error {
return handler(&context{Ctx: ctx})
}, handlers...)
return r
}

// Group 路由组
func (r *routeGroup) Group(prefix string, handlers ...Handler) Router {
list := make([]fiber.Handler, 0, len(handlers))
for i := range handlers {
handler := handlers[i]
list = append(list, func(ctx fiber.Ctx) error {
return handler(&context{Ctx: ctx})
})
func (r *routeGroup) Group(prefix string, middlewares ...any) Router {
handlers := make([]fiber.Handler, 0, len(middlewares))
for i := range middlewares {
middleware := middlewares[i]

switch h := middleware.(type) {
case fiber.Handler:
handlers = append(handlers, h)
case Handler:
handlers = append(handlers, func(ctx fiber.Ctx) error {
return h(&context{Ctx: ctx})
})
}
}

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

0 comments on commit 439e4a9

Please sign in to comment.