util

package
v0.0.0-...-4dae00c Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRedisClient

func DefaultRedisClient() (client *redis.Client, err error)

func GinResJSON

func GinResJSON(c *gin.Context, bts []byte)

func IPFreqLimit1

func IPFreqLimit1(client *redis.Client, dur time.Duration, number int,
	param func(*gin.Context) string) (
	limiter func(*gin.Context), err error)

IPFreqLimit1 generate ip access frequence limiter middleware with redis set, internal error with redis will response 500 to client, overrun limit will response 429

@Parameter  client   redis client to user, can't be nil
@Parameter  dur      record and count ip access in duration, must greater or equal time.Second
@Parameter  number   maxmium access number, must greater than 0
@Parameter  param    function to generate identifier string as suffix
@return     limiter  gin middleware
@return     err      erorr of failed to create middleware

func IPFreqLimit2

func IPFreqLimit2(client *redis.Client, dur time.Duration, number int,
	param func(*gin.Context) string) (
	limiter func(*gin.Context), err error)

IPFreqLimit2 generate ip access frequence limiter middleware with redis hset, internal error with redis will response 500 to client, overrun limit will response 429

@Parameter  client   redis client to user, can't be nil
@Parameter  dur      record and count ip access in duration, must greater or equal time.Second
@Parameter  number   maxmium access number, must greater than 0
@Parameter  param    function to generate identifier string as suffix
@Return     limiter  gin middleware
@Return     err      erorr of failed to create middleware

func RedisCache

func RedisCache(client *redis.Client, rk string,
	fn func() (interface{}, error), dk string, dur time.Duration) (
	bts []byte, code int, err error)

Types

type API

type API struct {
	Name, Path, Method string
	RecJSON, Auth      bool
	BodyStr            string
}

func (*API) Preprocess

func (api *API) Preprocess() (err error)

func (*API) Valid

func (api *API) Valid() (err error)

type CallRedis

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

func NewCallRedis

func NewCallRedis(client *redis.Client, expire time.Duration, withMutex bool) (
	cr *CallRedis, err error)

func (*CallRedis) Get

func (cr *CallRedis) Get(key string, fn func() (interface{}, error)) (result string, err error)

func (*CallRedis) ResetExpire

func (cr *CallRedis) ResetExpire(expire time.Duration)

type Config

type Config struct {
	Name string `json:"name"`

	S map[string]string    `json:"s"`
	I map[string]int64     `json:"i"`
	F map[string]float64   `json:"f"`
	B map[string]bool      `json:"b"`
	T map[string]time.Time `json:"t"`
	D map[string]dur       `json:"d"`

	SX map[string][]string    `json:"sx"`
	IX map[string][]int64     `json:"ix"`
	FX map[string][]float64   `json:"fx"`
	BX map[string][]bool      `json:"bx"`
	TX map[string][]time.Time `json:"tx"`
	DX map[string][]dur       `json:"dx"`
}

type ReqT

type ReqT struct {
	URL, Token string
	LogPrefix  string
	Headers    map[string][2]string
	Timeout    duration

	List []*API `toml:"-"`
	API  struct {
		I []API
	}
	// contains filtered or unexported fields
}

func NewReqT

func NewReqT(tf string) (rt *ReqT, err error)

func (*ReqT) Close

func (rt *ReqT) Close() (err error)

func (*ReqT) Request

func (rt *ReqT) Request(api *API) (req *http.Request, err error)

type Selector

type Selector struct {
	Name     string             `json:"name"`
	JStr     string             `json:"jstr"`
	Exp      string             `json:"exp"`
	Type     string             `json:"type"` // xpath, css, templ
	Num      int                `json:"num"`  // default is 1
	Required bool               `json:"required"`
	AsHTML   bool               `json:"as_html"`
	Templ    *template.Template `json:"-"`
}

func SelectorsFromToml

func SelectorsFromToml(fp string) (sels []Selector, err error)

func (*Selector) Compile

func (sel *Selector) Compile() (err error)

type TomlEnv

type TomlEnv struct {
	Name string             `json:"name"`
	StrS map[string]string  `json:"strs"`
	StrI map[string]int64   `json:"stri"`
	StrF map[string]float64 `json:"strf"`
	StrB map[string]bool    `json:"strb"`
}

func NewConfig

func NewConfig(fp string) (tc *TomlEnv, err error)

func NewTomlEnv

func NewTomlEnv(fp string) (tc *TomlEnv, err error)

func (*TomlEnv) Get

func (te *TomlEnv) Get(key string) (string, bool)

func (*TomlEnv) GetB

func (te *TomlEnv) GetB(key string) (value bool, ok bool)

func (*TomlEnv) GetF

func (te *TomlEnv) GetF(key string) (value float64, ok bool)

func (*TomlEnv) GetI

func (te *TomlEnv) GetI(key string) (value int64, ok bool)

func (*TomlEnv) GetS

func (te *TomlEnv) GetS(key string) (value string, ok bool)

func (*TomlEnv) Set

func (te *TomlEnv) Set(key string, value interface{}) (err error)

Jump to

Keyboard shortcuts

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