ginx

package
v0.0.0-...-9a83fbb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

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
	ErrCodeUnauthorized    errx.ErrCode = 100243
)
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
var ErrUnauthorized = &HttpError{
	Message:    "permission forbidden",
	Code:       ErrCodeUnauthorized,
	HttpStatus: http.StatusUnauthorized,
}
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 EntityCreated(ctx *gin.Context, id string)

func EntityNotFound

func EntityNotFound(ctx *gin.Context, msg string)

func EntityUpdated

func EntityUpdated(ctx *gin.Context, count int)

func Error

func Error(ctx *gin.Context, err *HttpError)

func ErrorMiddleware

func ErrorMiddleware(c *gin.Context)

ErrorMiddleware request panic error handler

func JSON

func JSON[T any](ctx *gin.Context, v T)

func ManyEntitiesCreated

func ManyEntitiesCreated(ctx *gin.Context, count int)

func NewHttpClient

func NewHttpClient(hoptions *HttpClientOptions) *http.Client

func NewJwtMiddleware

func NewJwtMiddleware(configure func(*jwt.GinJWTMiddleware)) (*jwt.GinJWTMiddleware, error)

func NotFound

func NotFound(ctx *gin.Context)

func OnError

func OnError(ctx context.Context, h ErrorHandFunc)

添加请求生命周期中出现错误时的处理方法。 注意: 该方法中不要使用panic

func OptionalAuthHandlerFunc

func OptionalAuthHandlerFunc(buidler *server.ServerBuiler) gin.HandlerFunc

OptionalAuthHandlerFunc 支持匿名和用户登录两种访问方式

func OptionalString

func OptionalString(ctx *gin.Context, key string) *string

func Panic

func Panic(code errx.ErrCode, message string)

func PanicEntityNotFound

func PanicEntityNotFound(message string)

func PanicErr

func PanicErr(status int, code errx.ErrCode, 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 Path

func Path(ctx *gin.Context, key string) string

func Proxy

func Proxy(ctx *gin.Context, path string)

func QueryBool

func QueryBool(c *gin.Context, key string) bool

func QueryID

func QueryID(ctx *gin.Context, key string) *primitive.ObjectID

func QueryIDRequired

func QueryIDRequired(ctx *gin.Context, key string) primitive.ObjectID

func QueryInt

func QueryInt(ctx *gin.Context, key string, defaultValue int) int

func QueryPage

func QueryPage(ctx *gin.Context) (int, int)

func QueryPageAndSort

func QueryPageAndSort(ctx *gin.Context) *x.PageAndSort

func QueryStrings

func QueryStrings(ctx *gin.Context, key string) []string

func SuccessWithMessage

func SuccessWithMessage(ctx *gin.Context, success bool, msg string)

func UnitWorkMiddleware

func UnitWorkMiddleware() gin.HandlerFunc

func WithScopedUnitWork

func WithScopedUnitWork(ctx context.Context) context.Context

WithScopedUnitWork 使用默认的UnitWork,在当前请求周期中有效,自动提交事务

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 DataResult[T any] struct {
	Success bool `json:"success"`
	Data    T    `json:"data"`
}

type EntityUpdatedResult

type EntityUpdatedResult struct {
	Count int `json:"count"`
}

type ErrorHandFunc

type ErrorHandFunc func(*gin.Context)

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 New

func New(err error) HttpError

func NewUnkonwErr

func NewUnkonwErr(err any) HttpError

func (*HttpError) Error

func (e *HttpError) Error() string

type JwtClaims

type JwtClaims map[string]interface{}

func UserClaims

func UserClaims(c context.Context) JwtClaims

func (JwtClaims) Find

func (m JwtClaims) Find(key string) interface{}

func (JwtClaims) FindAll

func (m JwtClaims) FindAll(key string) []interface{}

type RequestClient

type RequestClient struct {
	// contains filtered or unexported fields
}

func NewRequestClient

func NewRequestClient(hoptions *HttpClientOptions) *RequestClient

func (*RequestClient) Get

func (c *RequestClient) Get(url string, retry bool) ([]byte, error)

func (*RequestClient) GetObj

func (c *RequestClient) GetObj(url string, result interface{}) error

func (*RequestClient) GetResponse

func (c *RequestClient) GetResponse(url string, retry bool) (*http.Response, error)

func (*RequestClient) GetString

func (c *RequestClient) GetString(url string) (string, error)

func (*RequestClient) Post

func (c *RequestClient) Post(url string, body any) (*http.Response, error)

func (*RequestClient) PostObj

func (c *RequestClient) PostObj(url string, body any, result interface{}) error

type SuccessWithMessageResult

type SuccessWithMessageResult struct {
	Success bool   `json:"success"`
	Msg     string `json:"msg"`
}

type UnitWork

type UnitWork interface {
	Start(ctx context.Context) context.Context
	Commit(ctx context.Context) error
	Abort(ctx context.Context)
}

func NewUnitWork

func NewUnitWork(ctx context.Context) UnitWork

NewUnitWork 在当前请求周期中创建新的UnitWork,可控制UnitWork提交或取消 另外,在请求结束时,UnitWorkMiddleware会自动提交,当请求执行过程中发生错误时自动abort

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL