ginx

package module
v0.0.0-...-da6892f Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 26 Imported by: 2

Documentation

Index

Constants

View Source
const (
	GIN_RequestId = "GIN_RequestId"
	GIN_Error     = "GIN_Error"
	GIN_AccountId = "GIN_AccountId" // string
	GIN_Data      = "GIN_Data"      // map[string]any

)

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(username, password string, handle func(*gin.Context, string)) gin.HandlerFunc

handle key: no_token, invalid_token, incorrect_token, User:XXXX

func BasicBcrypt

func BasicBcrypt(username, password string, handle func(*gin.Context, ...string)) gin.HandlerFunc

handle key: no_token, invalid_token, incorrect_token, User:XXXX

func CacheControl

func CacheControl(seconds int) gin.HandlerFunc

func Cors

func Cors(origins []string, maxAges ...time.Duration) gin.HandlerFunc

HTTP/1.1 204 No Content Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Origin,Content-Type,Authorization Access-Control-Allow-Methods: GET,POST,OPTIONS,HEAD Access-Control-Allow-Origin: http://localhost:5173 Access-Control-Max-Age: 3600 Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Date: Thu, 08 Aug 2024 00:18:50 GMT

func Cors0

func Cors0(origin string, methods string) gin.HandlerFunc

func Get

func Get[T any](ctx *gin.Context, key string) (item T, err error)

errors: value is unset, type not match

func GetAPI

func GetAPI(ctx *gin.Context) string

func GetDate

func GetDate(ctx *gin.Context) (*time.Time, error)

func Healthz

func Healthz(ctx *gin.Context)

func IndexStaticFiles

func IndexStaticFiles(router *gin.RouterGroup, d string) (err error)

func JSONStatic

func JSONStatic(data any) gin.HandlerFunc

func NewAPILog

func NewAPILog(logger Logger[zap.Field], debug bool,
	errorHandler func(*gin.Context) []string,
	meters ...func(string, float64, []string),
) (hf gin.HandlerFunc)

func PromHandler

func PromHandler() gin.HandlerFunc

a replace of gin.WrapH(promhttp.Handler())

func ResponseFile

func ResponseFile(ctx *gin.Context, buf *bytes.Buffer, filename, typ string)

func ServeStaticDir

func ServeStaticDir(httpDir, local string, listDir bool) func(*gin.RouterGroup)

func ServeStaticFS

func ServeStaticFS(rg *gin.RouterGroup, efs embed.FS, p string, secs int) (err error)

go:embed static efs embed.FS

p = "static" secs = 3600

func ServeStaticFile

func ServeStaticFile(bts []byte, name string) gin.HandlerFunc

name: filename, e.g. favicon.ico ct: Content-Type, e.g. image/x-icon

func SetData

func SetData(ctx *gin.Context, kvs map[string]any)

func SetError

func SetError(ctx *gin.Context, err any)

func SetKV

func SetKV(ctx *gin.Context, key string, value any)

func SetRequestId

func SetRequestId(ctx *gin.Context, requestId string)

func Validate

func Validate[T any](item *T) (err error)

func WsUpgrade

func WsUpgrade(ctx *gin.Context)

Types

type HttpConfig

type HttpConfig struct {
	Path         string   `mapstructure:"path"`
	AllowOrigins []string `mapstructure:"allow_origins"`
	Tls          bool     `mapstructure:"tls"`
	Cert         string   `mapstructure:"cert"`
	Key          string   `mapstructure:"key"`
}

func (*HttpConfig) FromFile

func (self *HttpConfig) FromFile(fp string) (err error)

func (*HttpConfig) SetEngine

func (self *HttpConfig) SetEngine(engine *gin.Engine)

func (*HttpConfig) SetServer

func (self *HttpConfig) SetServer(server *http.Server) (err error)

type JwtData

type JwtData struct {
	Issuer    string   `json:"iss"` // required: *app_name
	Subject   string   `json:"sub"` // required: *account_id
	Audience  []string `json:"aud,omitempty"`
	IssuedAt  int64    `json:"iat"` // required:
	ExpiresAt int64    `json:"exp"` // required:
	NotBefore int64    `json:"nbf,omitempty"`
	ID        string   `json:"jti"` // required: *request_id

	Data map[string]string `json:"_data"`
}

see jwt.RegisteredClaims

type JwtHMAC

type JwtHMAC struct {
	Key      string        `mapstructure:"key"`
	Interval time.Duration `mapstructure:"interval"` // dynamic refresh token ttl in cache
	Duration time.Duration `mapstructure:"duration"`
	Method   uint          `mapstructure:"method"`
	// contains filtered or unexported fields
}

func NewJwtHMAC

func NewJwtHMAC(vp *viper.Viper, issuer string) (jh *JwtHMAC, err error)

func (*JwtHMAC) Auth

func (self *JwtHMAC) Auth(signed string) (data *JwtData, code string, err error)

code: enum("token_expired", "invalid_token")

func (*JwtHMAC) ParsePayload

func (self *JwtHMAC) ParsePayload(signed string) (data *JwtData, err error)

func (*JwtHMAC) Sign

func (self *JwtHMAC) Sign(data *JwtData) (signed string, err error)

Authorization: Bearer xxxx go doc jwt/v5.RegisteredClaims: iss, sub, aud, exp, nbf, iat, jti

type Logger

type Logger[T any] interface {
	Debug(string, ...T)
	Info(string, ...T)
	Warn(string, ...T)
	Error(string, ...T)
}

Jump to

Keyboard shortcuts

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