mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-23 17:05:02 +08:00
change route path name
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
zap "go.uber.org/zap"
|
||||
)
|
||||
|
||||
// import packages: context. errcode. middleware. zap. gin. metadata.
|
||||
// import packages: context. errcode. middleware. zap. gin.
|
||||
|
||||
type UserExampleServiceLogicer interface {
|
||||
Create(ctx context.Context, req *CreateUserExampleRequest) (*CreateUserExampleReply, error)
|
||||
@@ -59,7 +59,13 @@ func WithUserExampleServiceLogger(zapLog *zap.Logger) UserExampleServiceOption {
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterUserExampleServiceRouter(prePath string, iRouter gin.IRouter, iLogic UserExampleServiceLogicer, opts ...UserExampleServiceOption) {
|
||||
func RegisterUserExampleServiceRouter(
|
||||
iRouter gin.IRouter,
|
||||
groupPathMiddlewares map[string][]gin.HandlerFunc,
|
||||
singlePathMiddlewares map[string][]gin.HandlerFunc,
|
||||
iLogic UserExampleServiceLogicer,
|
||||
opts ...UserExampleServiceOption) {
|
||||
|
||||
o := &userExampleServiceOptions{}
|
||||
o.apply(opts...)
|
||||
|
||||
@@ -71,48 +77,59 @@ func RegisterUserExampleServiceRouter(prePath string, iRouter gin.IRouter, iLogi
|
||||
}
|
||||
|
||||
r := &userExampleServiceRouter{
|
||||
prePath: prePath,
|
||||
iRouter: iRouter,
|
||||
iLogic: iLogic,
|
||||
iResponse: o.responser,
|
||||
zapLog: o.zapLog,
|
||||
iRouter: iRouter,
|
||||
groupPathMiddlewares: groupPathMiddlewares,
|
||||
singlePathMiddlewares: singlePathMiddlewares,
|
||||
iLogic: iLogic,
|
||||
iResponse: o.responser,
|
||||
zapLog: o.zapLog,
|
||||
}
|
||||
r.register()
|
||||
}
|
||||
|
||||
type userExampleServiceRouter struct {
|
||||
iRouter gin.IRouter
|
||||
iLogic UserExampleServiceLogicer
|
||||
iResponse errcode.Responser
|
||||
zapLog *zap.Logger
|
||||
prePath string
|
||||
iRouter gin.IRouter
|
||||
groupPathMiddlewares map[string][]gin.HandlerFunc
|
||||
singlePathMiddlewares map[string][]gin.HandlerFunc
|
||||
iLogic UserExampleServiceLogicer
|
||||
iResponse errcode.Responser
|
||||
zapLog *zap.Logger
|
||||
}
|
||||
|
||||
func (r *userExampleServiceRouter) register() {
|
||||
r.iRouter.Handle("POST", r.cutPrePath("/api/v1/userExample"), r.Create_0)
|
||||
r.iRouter.Handle("DELETE", r.cutPrePath("/api/v1/userExample/:id"), r.DeleteByID_0)
|
||||
r.iRouter.Handle("PUT", r.cutPrePath("/api/v1/userExample/:id"), r.UpdateByID_0)
|
||||
r.iRouter.Handle("GET", r.cutPrePath("/api/v1/userExample/:id"), r.GetByID_0)
|
||||
r.iRouter.Handle("POST", r.cutPrePath("/api/v1/userExamples/ids"), r.ListByIDs_0)
|
||||
r.iRouter.Handle("POST", r.cutPrePath("/api/v1/userExamples"), r.List_0)
|
||||
r.iRouter.Handle("POST", "/api/v1/userExample", r.withMiddleware("/api/v1/userExample", r.Create_0)...)
|
||||
r.iRouter.Handle("DELETE", "/api/v1/userExample/:id", r.withMiddleware("/api/v1/userExample/:id", r.DeleteByID_0)...)
|
||||
r.iRouter.Handle("PUT", "/api/v1/userExample/:id", r.withMiddleware("/api/v1/userExample/:id", r.UpdateByID_0)...)
|
||||
r.iRouter.Handle("GET", "/api/v1/userExample/:id", r.withMiddleware("/api/v1/userExample/:id", r.GetByID_0)...)
|
||||
r.iRouter.Handle("POST", "/api/v1/userExample/list/ids", r.withMiddleware("/api/v1/userExample/list/ids", r.ListByIDs_0)...)
|
||||
r.iRouter.Handle("POST", "/api/v1/userExample/list", r.withMiddleware("/api/v1/userExample/list", r.List_0)...)
|
||||
|
||||
}
|
||||
|
||||
func (r *userExampleServiceRouter) cutPrePath(path string) string {
|
||||
if r.prePath == "" || r.prePath == "/" {
|
||||
return path
|
||||
}
|
||||
size := len(r.prePath)
|
||||
if len(path) <= size {
|
||||
return path
|
||||
}
|
||||
if r.prePath == path[:size] {
|
||||
cutPath := path[size:]
|
||||
if cutPath[0] != '/' {
|
||||
cutPath = "/" + cutPath
|
||||
func (r *userExampleServiceRouter) withMiddleware(path string, fn gin.HandlerFunc) []gin.HandlerFunc {
|
||||
handlerFns := []gin.HandlerFunc{}
|
||||
|
||||
// determine if a route group is hit or miss, left prefix rule
|
||||
for groupPath, fns := range r.groupPathMiddlewares {
|
||||
if groupPath == "" || groupPath == "/" {
|
||||
handlerFns = append(handlerFns, fns...)
|
||||
continue
|
||||
}
|
||||
size := len(groupPath)
|
||||
if len(path) < size {
|
||||
continue
|
||||
}
|
||||
if groupPath == path[:size] {
|
||||
handlerFns = append(handlerFns, fns...)
|
||||
}
|
||||
return cutPath
|
||||
}
|
||||
return path
|
||||
|
||||
// determine if a single route has been hit
|
||||
if fns, ok := r.singlePathMiddlewares[path]; ok {
|
||||
handlerFns = append(handlerFns, fns...)
|
||||
}
|
||||
|
||||
return append(handlerFns, fn)
|
||||
}
|
||||
|
||||
func (r *userExampleServiceRouter) Create_0(c *gin.Context) {
|
||||
|
Reference in New Issue
Block a user