mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-10-05 08:46:57 +08:00
307 lines
7.8 KiB
Go
307 lines
7.8 KiB
Go
// Code generated by https://github.com/zhufuyi/sponge, DO NOT EDIT.
|
|
|
|
package v1
|
|
|
|
import (
|
|
context "context"
|
|
gin "github.com/gin-gonic/gin"
|
|
errcode "github.com/zhufuyi/sponge/pkg/errcode"
|
|
middleware "github.com/zhufuyi/sponge/pkg/gin/middleware"
|
|
zap "go.uber.org/zap"
|
|
strings "strings"
|
|
)
|
|
|
|
// import packages: strings. context. errcode. middleware. zap. gin.
|
|
|
|
type UserExampleLogicer interface {
|
|
Create(ctx context.Context, req *CreateUserExampleRequest) (*CreateUserExampleReply, error)
|
|
DeleteByID(ctx context.Context, req *DeleteUserExampleByIDRequest) (*DeleteUserExampleByIDReply, error)
|
|
UpdateByID(ctx context.Context, req *UpdateUserExampleByIDRequest) (*UpdateUserExampleByIDReply, error)
|
|
GetByID(ctx context.Context, req *GetUserExampleByIDRequest) (*GetUserExampleByIDReply, error)
|
|
List(ctx context.Context, req *ListUserExampleRequest) (*ListUserExampleReply, error)
|
|
}
|
|
|
|
type UserExampleOption func(*userExampleOptions)
|
|
|
|
type userExampleOptions struct {
|
|
isFromRPC bool
|
|
responser errcode.Responser
|
|
zapLog *zap.Logger
|
|
httpErrors []*errcode.Error
|
|
rpcStatus []*errcode.RPCStatus
|
|
wrapCtxFn func(c *gin.Context) context.Context
|
|
}
|
|
|
|
func (o *userExampleOptions) apply(opts ...UserExampleOption) {
|
|
for _, opt := range opts {
|
|
opt(o)
|
|
}
|
|
}
|
|
|
|
func WithUserExampleHTTPResponse() UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.isFromRPC = false
|
|
}
|
|
}
|
|
|
|
func WithUserExampleRPCResponse() UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.isFromRPC = true
|
|
}
|
|
}
|
|
|
|
func WithUserExampleResponser(responser errcode.Responser) UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.responser = responser
|
|
}
|
|
}
|
|
|
|
func WithUserExampleLogger(zapLog *zap.Logger) UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.zapLog = zapLog
|
|
}
|
|
}
|
|
|
|
func WithUserExampleErrorToHTTPCode(e ...*errcode.Error) UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.httpErrors = e
|
|
}
|
|
}
|
|
|
|
func WithUserExampleRPCStatusToHTTPCode(s ...*errcode.RPCStatus) UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.rpcStatus = s
|
|
}
|
|
}
|
|
|
|
func WithUserExampleWrapCtx(wrapCtxFn func(c *gin.Context) context.Context) UserExampleOption {
|
|
return func(o *userExampleOptions) {
|
|
o.wrapCtxFn = wrapCtxFn
|
|
}
|
|
}
|
|
|
|
func RegisterUserExampleRouter(
|
|
iRouter gin.IRouter,
|
|
groupPathMiddlewares map[string][]gin.HandlerFunc,
|
|
singlePathMiddlewares map[string][]gin.HandlerFunc,
|
|
iLogic UserExampleLogicer,
|
|
opts ...UserExampleOption) {
|
|
|
|
o := &userExampleOptions{}
|
|
o.apply(opts...)
|
|
|
|
if o.responser == nil {
|
|
o.responser = errcode.NewResponser(o.isFromRPC, o.httpErrors, o.rpcStatus)
|
|
}
|
|
if o.zapLog == nil {
|
|
o.zapLog, _ = zap.NewProduction()
|
|
}
|
|
|
|
r := &userExampleRouter{
|
|
iRouter: iRouter,
|
|
groupPathMiddlewares: groupPathMiddlewares,
|
|
singlePathMiddlewares: singlePathMiddlewares,
|
|
iLogic: iLogic,
|
|
iResponse: o.responser,
|
|
zapLog: o.zapLog,
|
|
wrapCtxFn: o.wrapCtxFn,
|
|
}
|
|
r.register()
|
|
}
|
|
|
|
type userExampleRouter struct {
|
|
iRouter gin.IRouter
|
|
groupPathMiddlewares map[string][]gin.HandlerFunc
|
|
singlePathMiddlewares map[string][]gin.HandlerFunc
|
|
iLogic UserExampleLogicer
|
|
iResponse errcode.Responser
|
|
zapLog *zap.Logger
|
|
wrapCtxFn func(c *gin.Context) context.Context
|
|
}
|
|
|
|
func (r *userExampleRouter) register() {
|
|
r.iRouter.Handle("POST", "/api/v1/userExample", r.withMiddleware("POST", "/api/v1/userExample", r.Create_0)...)
|
|
r.iRouter.Handle("DELETE", "/api/v1/userExample/:id", r.withMiddleware("DELETE", "/api/v1/userExample/:id", r.DeleteByID_0)...)
|
|
r.iRouter.Handle("PUT", "/api/v1/userExample/:id", r.withMiddleware("PUT", "/api/v1/userExample/:id", r.UpdateByID_0)...)
|
|
r.iRouter.Handle("GET", "/api/v1/userExample/:id", r.withMiddleware("GET", "/api/v1/userExample/:id", r.GetByID_0)...)
|
|
r.iRouter.Handle("POST", "/api/v1/userExample/list", r.withMiddleware("POST", "/api/v1/userExample/list", r.List_0)...)
|
|
|
|
}
|
|
|
|
func (r *userExampleRouter) withMiddleware(method string, 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...)
|
|
}
|
|
}
|
|
|
|
// determine if a single route has been hit
|
|
key := strings.ToUpper(method) + "->" + path
|
|
if fns, ok := r.singlePathMiddlewares[key]; ok {
|
|
handlerFns = append(handlerFns, fns...)
|
|
}
|
|
|
|
return append(handlerFns, fn)
|
|
}
|
|
|
|
var _ middleware.CtxKeyString
|
|
|
|
func (r *userExampleRouter) Create_0(c *gin.Context) {
|
|
req := &CreateUserExampleRequest{}
|
|
var err error
|
|
|
|
if err = c.ShouldBindJSON(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindJSON error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
var ctx context.Context
|
|
if r.wrapCtxFn != nil {
|
|
ctx = r.wrapCtxFn(c)
|
|
} else {
|
|
ctx = middleware.WrapCtx(c)
|
|
}
|
|
|
|
out, err := r.iLogic.Create(ctx, req)
|
|
if err != nil {
|
|
r.iResponse.Error(c, err)
|
|
return
|
|
}
|
|
|
|
r.iResponse.Success(c, out)
|
|
}
|
|
|
|
func (r *userExampleRouter) DeleteByID_0(c *gin.Context) {
|
|
req := &DeleteUserExampleByIDRequest{}
|
|
var err error
|
|
|
|
if err = c.ShouldBindUri(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindUri error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
if err = c.ShouldBindQuery(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindQuery error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
var ctx context.Context
|
|
if r.wrapCtxFn != nil {
|
|
ctx = r.wrapCtxFn(c)
|
|
} else {
|
|
ctx = middleware.WrapCtx(c)
|
|
}
|
|
|
|
out, err := r.iLogic.DeleteByID(ctx, req)
|
|
if err != nil {
|
|
r.iResponse.Error(c, err)
|
|
return
|
|
}
|
|
|
|
r.iResponse.Success(c, out)
|
|
}
|
|
|
|
func (r *userExampleRouter) UpdateByID_0(c *gin.Context) {
|
|
req := &UpdateUserExampleByIDRequest{}
|
|
var err error
|
|
|
|
if err = c.ShouldBindUri(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindUri error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
if err = c.ShouldBindJSON(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindJSON error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
var ctx context.Context
|
|
if r.wrapCtxFn != nil {
|
|
ctx = r.wrapCtxFn(c)
|
|
} else {
|
|
ctx = middleware.WrapCtx(c)
|
|
}
|
|
|
|
out, err := r.iLogic.UpdateByID(ctx, req)
|
|
if err != nil {
|
|
r.iResponse.Error(c, err)
|
|
return
|
|
}
|
|
|
|
r.iResponse.Success(c, out)
|
|
}
|
|
|
|
func (r *userExampleRouter) GetByID_0(c *gin.Context) {
|
|
req := &GetUserExampleByIDRequest{}
|
|
var err error
|
|
|
|
if err = c.ShouldBindUri(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindUri error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
if err = c.ShouldBindQuery(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindQuery error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
var ctx context.Context
|
|
if r.wrapCtxFn != nil {
|
|
ctx = r.wrapCtxFn(c)
|
|
} else {
|
|
ctx = middleware.WrapCtx(c)
|
|
}
|
|
|
|
out, err := r.iLogic.GetByID(ctx, req)
|
|
if err != nil {
|
|
r.iResponse.Error(c, err)
|
|
return
|
|
}
|
|
|
|
r.iResponse.Success(c, out)
|
|
}
|
|
|
|
func (r *userExampleRouter) List_0(c *gin.Context) {
|
|
req := &ListUserExampleRequest{}
|
|
var err error
|
|
|
|
if err = c.ShouldBindJSON(req); err != nil {
|
|
r.zapLog.Warn("ShouldBindJSON error", zap.Error(err), middleware.GCtxRequestIDField(c))
|
|
r.iResponse.ParamError(c, err)
|
|
return
|
|
}
|
|
|
|
var ctx context.Context
|
|
if r.wrapCtxFn != nil {
|
|
ctx = r.wrapCtxFn(c)
|
|
} else {
|
|
ctx = middleware.WrapCtx(c)
|
|
}
|
|
|
|
out, err := r.iLogic.List(ctx, req)
|
|
if err != nil {
|
|
r.iResponse.Error(c, err)
|
|
return
|
|
}
|
|
|
|
r.iResponse.Success(c, out)
|
|
}
|