Documentation ¶
Index ¶
- Constants
- Variables
- func AddJwt(builder *server.ServerBuiler, routePrefix string, ...)
- func AuthHandlerFunc(buidler *server.ServerBuiler) gin.HandlerFunc
- func EntityCreated(ctx *gin.Context, id string)
- func EntityNotFound(ctx *gin.Context, msg string)
- func EntityUpdated(ctx *gin.Context, count int)
- func Error(ctx *gin.Context, err *HttpError)
- func ErrorMiddleware(c *gin.Context)
- func JSON[T any](ctx *gin.Context, v T)
- func ManyEntitiesCreated(ctx *gin.Context, count int)
- func NewHttpClient(hoptions *HttpClientOptions) *http.Client
- func NewJwtMiddleware(configure func(*jwt.GinJWTMiddleware)) (*jwt.GinJWTMiddleware, error)
- func NotFound(ctx *gin.Context)
- func OnError(ctx context.Context, h ErrorHandFunc)
- func OptionalAuthHandlerFunc(buidler *server.ServerBuiler) gin.HandlerFunc
- func OptionalString(ctx *gin.Context, key string) *string
- func Panic(code errx.ErrCode, message string)
- func PanicEntityNotFound(message string)
- func PanicErr(status int, code errx.ErrCode, message string)
- func PanicNotFound(message string)
- func PanicUnAuthenticated(message string)
- func PanicUnAuthorized(message string)
- func PanicValidatition(message string)
- func Path(ctx *gin.Context, key string) string
- func Proxy(ctx *gin.Context, path string)
- func QueryBool(c *gin.Context, key string) bool
- func QueryID(ctx *gin.Context, key string) *primitive.ObjectID
- func QueryIDRequired(ctx *gin.Context, key string) primitive.ObjectID
- func QueryInt(ctx *gin.Context, key string, defaultValue int) int
- func QueryPage(ctx *gin.Context) (int, int)
- func QueryPageAndSort(ctx *gin.Context) *x.PageAndSort
- func QueryStrings(ctx *gin.Context, key string) []string
- func SuccessWithMessage(ctx *gin.Context, success bool, msg string)
- func UnitWorkMiddleware() gin.HandlerFunc
- func WithScopedUnitWork(ctx context.Context) context.Context
- type CreateEntityResult
- type CreateManyEntitiesResult
- type CurrentUserInfo
- type DataResult
- type EntityUpdatedResult
- type ErrorHandFunc
- type ErrorHandlers
- type HttpClientOptions
- type HttpError
- type JwtClaims
- type RequestClient
- func (c *RequestClient) Get(url string, retry bool) ([]byte, error)
- func (c *RequestClient) GetObj(url string, result interface{}) error
- func (c *RequestClient) GetResponse(url string, retry bool) (*http.Response, error)
- func (c *RequestClient) GetString(url string) (string, error)
- func (c *RequestClient) Post(url string, body any) (*http.Response, error)
- func (c *RequestClient) PostObj(url string, body any, result interface{}) error
- type SuccessWithMessageResult
- type UnitWork
Constants ¶
View Source
const ( ClaimRole = "role" ClaimOrg = "org" ClaimUserId = "id" ClaimUserAddr = "addr" )
View Source
const ErrHandlersKey = "ErrorHandlers"
View Source
const UnitWorkKey = "UnitWorkKey"
Variables ¶
View Source
var ( ErrCodePageNotFound errx.ErrCode = 100204 ErrCodeEntityNotFound errx.ErrCode = 100244 // 参数校验失败 ErrCodeBadRequest errx.ErrCode = 100240 // 未认证(登录) ErrCodeUnAuthenticated errx.ErrCode = 100241 )
View Source
var ErrLoginFailed = errors.New("login failed")
View Source
var ErrPageNotFound = &HttpError{ HttpStatus: http.StatusNotFound, Code: ErrCodePageNotFound, Message: "page not found", }
View Source
var ErrUnAuthenticated = &HttpError{ Message: "user not login", Code: ErrCodeUnAuthenticated, HttpStatus: http.StatusUnauthorized, }
View Source
ErrCodeUnauthorized, HttpStatus: http.StatusUnauthorized, }Message: "permission forbidden", Code:
View Source
var UnhandledError = &HttpError{ Message: "服务器未处理异常", Code: errx.ErrCodeUnkown, HttpStatus: http.StatusInternalServerError, }
Functions ¶
func AddJwt ¶
func AddJwt(builder *server.ServerBuiler, routePrefix string, configure func(*jwt.GinJWTMiddleware))
AddJwt 添加jwt
func AuthHandlerFunc ¶
func AuthHandlerFunc(buidler *server.ServerBuiler) gin.HandlerFunc
AuthHandlerFunc 获取认证中间件handler
func EntityCreated ¶
func EntityNotFound ¶
func EntityUpdated ¶
func ErrorMiddleware ¶
ErrorMiddleware request panic error handler
func ManyEntitiesCreated ¶
func NewHttpClient ¶
func NewHttpClient(hoptions *HttpClientOptions) *http.Client
func NewJwtMiddleware ¶
func NewJwtMiddleware(configure func(*jwt.GinJWTMiddleware)) (*jwt.GinJWTMiddleware, error)
func OnError ¶
func OnError(ctx context.Context, h ErrorHandFunc)
添加请求生命周期中出现错误时的处理方法。 注意: 该方法中不要使用panic
func OptionalAuthHandlerFunc ¶
func OptionalAuthHandlerFunc(buidler *server.ServerBuiler) gin.HandlerFunc
OptionalAuthHandlerFunc 支持匿名和用户登录两种访问方式
func PanicEntityNotFound ¶
func PanicEntityNotFound(message string)
func PanicNotFound ¶
func PanicNotFound(message string)
func PanicUnAuthenticated ¶
func PanicUnAuthenticated(message string)
func PanicUnAuthorized ¶
func PanicUnAuthorized(message string)
func PanicValidatition ¶
func PanicValidatition(message string)
func QueryPageAndSort ¶
func QueryPageAndSort(ctx *gin.Context) *x.PageAndSort
func UnitWorkMiddleware ¶
func UnitWorkMiddleware() gin.HandlerFunc
Types ¶
type CreateEntityResult ¶
type CreateEntityResult struct {
ID string `json:"id"`
}
type CreateManyEntitiesResult ¶
type CreateManyEntitiesResult struct {
Count int `json:"count"`
}
type CurrentUserInfo ¶
type CurrentUserInfo struct { ID primitive.ObjectID DisplayName string Address string Avatar string }
func CurrentUser ¶
func CurrentUser(c context.Context) *CurrentUserInfo
func (*CurrentUserInfo) Authenticated ¶
func (u *CurrentUserInfo) Authenticated() bool
type DataResult ¶
type EntityUpdatedResult ¶
type EntityUpdatedResult struct {
Count int `json:"count"`
}
type ErrorHandFunc ¶
type ErrorHandlers ¶
type ErrorHandlers struct {
// contains filtered or unexported fields
}
func NewErrorHandlers ¶
func NewErrorHandlers() *ErrorHandlers
func (*ErrorHandlers) Add ¶
func (eh *ErrorHandlers) Add(h ErrorHandFunc)
func (*ErrorHandlers) Run ¶
func (eh *ErrorHandlers) Run(c *gin.Context)
type HttpClientOptions ¶
type HttpClientOptions struct {
ProxyURL string
}
type HttpError ¶
type HttpError struct { HttpStatus int `json:"-"` Code errx.ErrCode `json:"code"` Message string `json:"message"` Data any `json:"data"` }
func NewUnkonwErr ¶
type RequestClient ¶
type RequestClient struct {
// contains filtered or unexported fields
}
func NewRequestClient ¶
func NewRequestClient(hoptions *HttpClientOptions) *RequestClient
func (*RequestClient) GetObj ¶
func (c *RequestClient) GetObj(url string, result interface{}) error
func (*RequestClient) GetResponse ¶
Click to show internal directories.
Click to hide internal directories.