ginx

package
v0.0.0-...-087ca59 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 27 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 ErrLoginFailed = errors.New("login failed")

Functions

func AbortScopedUnitWork

func AbortScopedUnitWork(ctx context.Context)

func AddAuthHandler

func AddAuthHandler[TKey comparable](h AuthHandler[TKey])

AddAuthHandler adds a handler to the auth middleware.

func AddResultHandler

func AddResultHandler(h ResultHandlerFunc)

func AuthFunc

func AuthFunc[TKey comparable]() gin.HandlerFunc

func AuthHandlerFunc

func AuthHandlerFunc(builder *ServerBuilder) gin.HandlerFunc

AuthHandlerFunc 获取认证中间件handler

func Configure

func Configure(action ServerConfigureFunc)

Configure 配置服务,在 gin.Run 之前运行

func CurrentTenant

func CurrentTenant(c context.Context) *multi_tenancy.TenantInfo

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 *errx.HttpError)

func ErrorMiddleware

func ErrorMiddleware(c *gin.Context)

ErrorMiddleware request panic error handler

func JSON

func JSON(ctx *gin.Context, code int, v any)

func JSONResult

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

func ManyEntitiesCreated

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

func MultiTenancy

func MultiTenancy() gin.HandlerFunc

func NewHttpClient

func NewHttpClient(hoptions *HttpClientOptions, middle ...func(*http.Request) (*url.URL, error)) *http.Client

func NewJwtMiddleware

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

func NotFound

func NotFound(ctx *gin.Context)

func Ok

func Ok(ctx *gin.Context)

func OnError

func OnError(ctx context.Context, h ErrorHandFunc)

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

func OptionalAuthHandlerFunc

func OptionalAuthHandlerFunc(builder *ServerBuilder) gin.HandlerFunc

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

func Path

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

func PreConfigure

func PreConfigure(action ServerConfigureFunc)

PreConfigure 配置服务,在Configure之前运行

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 QueryPageParams

func QueryPageParams(ctx *gin.Context) *x.PageParam

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 UseRestRoutes

func UseRestRoutes(cfg func(*jwt.GinJWTMiddleware))

func WithScopedUnitwork

func WithScopedUnitwork(ctx context.Context) context.Context

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

func WithUserContext

func WithUserContext[TKey comparable](c context.Context, user *CurrentUserInfo[TKey]) context.Context

Types

type AuthHandler

type AuthHandler[TKey comparable] func(ctx *AuthHandlerContext[TKey]) error

type AuthHandlerContext

type AuthHandlerContext[TKey comparable] struct {
	User       *CurrentUserInfo[TKey]
	HasHandled bool
	// contains filtered or unexported fields
}

func (*AuthHandlerContext[TKey]) Context

func (ac *AuthHandlerContext[TKey]) Context() *gin.Context

type AuthOptions

type AuthOptions[TKey comparable] struct {
	// contains filtered or unexported fields
}

func (*AuthOptions[TKey]) AddHandler

func (o *AuthOptions[TKey]) AddHandler(h AuthHandler[TKey])

type AuthedEventData

type AuthedEventData[TKey comparable] struct {
	ID       TKey           `json:"id"`
	UserName string         `json:"userName"`
	Address  string         `json:"address"`
	Avatar   string         `json:"avatar"`
	Data     map[string]any `json:"data"`
	AuthType string         // 验证类型
}

func NewAuthedEventData

func NewAuthedEventData[TKey comparable](u *CurrentUserInfo[TKey]) *AuthedEventData[TKey]

type CreateEntityResult

type CreateEntityResult struct {
	ID string `json:"id"`
}

type CreateManyEntitiesResult

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

type CurrentUserInfo

type CurrentUserInfo[TKey comparable] struct {
	ID       TKey
	UserName string
	Address  string
	Avatar   string
	AuthType string // 验证类型
	Data     map[string]any
}

func CurrentUser

func CurrentUser[TKey comparable](c context.Context) *CurrentUserInfo[TKey]

func (*CurrentUserInfo[TKey]) Authenticated

func (u *CurrentUserInfo[TKey]) Authenticated() bool

func (*CurrentUserInfo[TKey]) Get

func (u *CurrentUserInfo[TKey]) Get(key string) any

func (*CurrentUserInfo[TKey]) Set

func (u *CurrentUserInfo[TKey]) Set(key string, value any)

type DataFilterKey

type DataFilterKey = string

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, ...error)

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, errs ...error)

type GinDataFilterProvider

type GinDataFilterProvider struct {
}

func (GinDataFilterProvider) DisableFilter

func (GinDataFilterProvider) EnableFilter

func (GinDataFilterProvider) EnableFilter(ctx context.Context, t reflect.Type)

func (GinDataFilterProvider) IsFilterDisabled

func (GinDataFilterProvider) IsFilterDisabled(ctx context.Context, t reflect.Type) bool

type HttpClientOptions

type HttpClientOptions struct {
	ProxyURL string
}

type JWTBuilder

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

func AddJwt

func AddJwt(builder *ServerBuilder, configure func(*jwt.GinJWTMiddleware)) *JWTBuilder

添加jwt

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) PostByForm

func (c *RequestClient) PostByForm(url string, formData map[string]string) (*http.Response, error)

type ResponseResult

type ResponseResult struct {
	Value any `json:"value"`
}

func NewResponseResult

func NewResponseResult(v any) *ResponseResult

type ResultHandlerFunc

type ResultHandlerFunc func(ctx *gin.Context, v *ResponseResult)

type ResultHandlerOptions

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

func (*ResultHandlerOptions) AddHandler

func (opts *ResultHandlerOptions) AddHandler(h ResultHandlerFunc)

type Server

type Server struct {
	G       *gin.Engine
	Options *ServerOptions
}

func NewServer

func NewServer(g *gin.Engine, options *ServerOptions) *Server

func (*Server) Run

func (s *Server) Run() error

func (*Server) Use

func (s *Server) Use(handlers ...gin.HandlerFunc) *Server

func (*Server) UseDefaultHandlers

func (s *Server) UseDefaultHandlers() *Server

type ServerBuilder

type ServerBuilder struct {
	Options *ServerOptions
	// 只在程序启动过程中进行操作,以保证协程安全
	Items map[string]any
	// contains filtered or unexported fields
}

func NewServerBuilder

func NewServerBuilder() *ServerBuilder

func (*ServerBuilder) Add

func (b *ServerBuilder) Add(fun func(b *ServerBuilder)) *ServerBuilder

func (*ServerBuilder) Build

func (b *ServerBuilder) Build() (*Server, error)

func (*ServerBuilder) Configure

func (b *ServerBuilder) Configure(action ServerConfigureFunc) *ServerBuilder

Configure 配置服务,该方法参数在App.Run中、gin.Run之前运行

func (*ServerBuilder) PreConfigure

func (b *ServerBuilder) PreConfigure(action ServerConfigureFunc) *ServerBuilder

PreConfigure 配置服务,该方法参数在App.Run中、gin.Run之前运行

func (*ServerBuilder) UseRestRoutes

func (b *ServerBuilder) UseRestRoutes()

type ServerConfigureFunc

type ServerConfigureFunc func(s *Server) error

type ServerOptions

type ServerOptions struct {
	Port     int
	RootUrl  string
	LogLevel zerolog.Level
}

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