util

module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2022 License: MIT

README

各目录的介绍

cache 包

所有的数据,均存储在内存中。

该缓存的实现,依赖于

github.com/allegro/bigcache
type Exist interface {
	Set(key string, value interface{}, second uint)             //设置:键,值,过期秒数
	Get(key string) (interface{}, error)                        //获取:键。返回:值,错误(有错表示不存在或已过期)
	Exist(key string) bool                                      //是否存在:键。返回:是否存在,true存在,false不存在
	Del(key string)                                             //删除:键
	ExistDelete(key string) bool                                //存在即删除:键;返回:是否存在,true存在,false不存在
	ExistOrSet(key string, value interface{}, second uint) bool //不存在就设置:键,值,过期秒数;返回:是否存在,true存在,false不存在
	Clean()                                                     //清空所有
}

his 包

使用Redis作为数据库,实现一些基本的缓存,队列,等等。

该包的实现,依赖于

github.com/garyburd/redigo/redis
NewQueue

创建一个队列

NewLimit 记录管理器
type Limit interface {
	Add(key string, delta int)                                //记录量自增
	Sub(key string, delta int)                                //记录量自减
	Get(key string) (cur int, e error)                        //获取当前量
	Reset(key string) (cur int, e error)                      //重置为0
	IsToLimit(key string, max int) (b bool, cur int, e error) //是否达上限值
}
NewExist 键是否存在的管理器
type Exist interface {
	IsExist(key string) (bool, error)     //key是否存在
	Add(key string)                       //添加存在
	Del(key string)                       //移除存在
	Count() (sum int, err error)          //一共有多少个key
	GetAllKey() (all []string, err error) //返回所有的key
}
NewKeyValue kv键值的管理器
type KeyValue interface {
	Set(key string, value string)             //设置
	Get(key string) (value string, err error) //获取
	Del(key string)                           //删除
}

ht 包

主要就是发送http请求的包 可以发送基本的一些请求类型:GETPOSTPUTDELETE

  • SetModelRelease 设置模式为发布模式,这样就不会打印debug日志
  • SetModelDebug 默认模式,Debug模式,发送请求后,会打印debug日志,链接&响应体内容。

random 包

生成一些随机数据,对切片进行洗牌打乱。

rlog 包

日志打印包,主要是分颜色打印,不同打印等级,分不同的颜色。 可以自动删除旧日志。

  • SetAutoRemoveLogFile 设置自动删除日志规则

日志打印等级

const (
    //日志打印等级常量
    LogDebug    = iota //调试 0
    LogInfo            //信息 1
    LogWarn            //警告 2
    LogError           //错误 3
    LogPanic           //恐慌 4
    LogNotPrint        //不打印 5
)

rui 包

这个包,主要就是建立 http 服务的包。

const (
    ContentTypeDownload = `application/force-download` //文件下载
    
    ContentTypeDefault = `text/plain; charset=UTF-8`              //文本
    ContentTypeJSON    = `application/json; charset=UTF-8`        //json
    ContentTypeHTML    = `text/html; charset=UTF-8`               //HTML
    ContentTypeXJS     = `application/x-javascript;charset=UTF-8` //JavaScript
    ContentTypeJS      = `text/javascript; charset=utf-8`         //JavaScript
    ContentTypeCSS     = `text/css; charset=utf-8`                //CSS
    
    ContentTypeImageJPEG = `image/jpeg`   //jpeg图片
    ContentTypeImageJPG  = `image/jpeg`   //jpg图片
    ContentTypeImagePNG  = `image/png`    //png图片
    ContentTypeImageWEBP = `image/webp`   //webp图片
    ContentTypeImageGIF  = `image/gif`    //gif图片
    ContentTypeImageICON = `image/x-icon` //icon图标
    
    ContentTypeImageOctetStream = `application/octet-stream` //八位字节流
)

type Application interface {
    RouterGroup
    AllHandleFunc() []RouterItem                                          //返回所有的请求信息
    PrintAllRequest()                                                     //打印所有的请求信息
    Run(host string, port uint) error                                     //以http运行
    RunTLS(host string, port uint, certFile string, keyFile string) error //以https方式运行
}

type RouterGroup interface {
    Use(handle ...func(Context))      //中间件
    Group(urlPath string) RouterGroup //请求分组
    
    GET(urlPath string, handle func(Context))     //GET请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    HEAD(urlPath string, handle func(Context))    //HEAD请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    OPTIONS(urlPath string, handle func(Context)) //OPTIONS请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    POST(urlPath string, handle func(Context))    //POST请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    PUT(urlPath string, handle func(Context))     //PUT请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    PATCH(urlPath string, handle func(Context))   //PATCH请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    DELETE(urlPath string, handle func(Context))  //DELETE请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    Any(urlPath string, handle func(Context))     //以上所有请求,自定义WebSocket链接请使用 WebSocket 或 Handler 或 HandlerFunc
    
    WebSocket(urlPath string, CheckOrigin bool, fx func(*websocket.Conn)) //WebSocket服务,CheckOrigin是否允许跨域访问
    StaticFile(urlPath string, filePath string)                           //一个文件的静态文件服务
    ServeFiles(urlPath string, root http.FileSystem)                      //多个文件的静态文件服务;注意,静态文件服务,是不过中间件的。
    Handler(method string, urlPath string, handler http.Handler)          //原生http响应;注意,原生http响应,是不过中间件的。
    HandlerFunc(method string, urlPath string, handler http.HandlerFunc)  //原生http响应函数;注意,原生http响应,是不过中间件的。
}

type Context interface {
    Request() *http.Request      //返回原生的请求指针。
    ResponseHeader() http.Header //响应头管理。
    
    StopNext()                                       //终止后续响应函数。
    SetKV(key string, value interface{})             //设置key-value,可以供后续流程获取到。
    GetKV(key string) (interface{}, error)           //根据key获取value。
    GetKVInt(key string, def ...int) int             //根据key获取value,并且转为int类型,如果不存在或出错则默认或def[0]。
    GetKVUint(key string, def ...uint) uint          //根据key获取value,并且转为uint类型,如果不存在或出错则默认或def[0]。
    GetKVInt64(key string, def ...int64) int64       //根据key获取value,并且转为int64类型,如果不存在或出错则默认或def[0]。
    GetKVUint64(key string, def ...uint64) uint64    //根据key获取value,并且转为uint64类型,如果不存在或出错则默认或def[0]。
    GetKVFloat64(key string, def ...float64) float64 //根据key获取value,并且转为float64类型,如果不存在或出错则默认或def[0]。
    GetKVBool(key string, def ...bool) bool          //根据key获取value,并且转为bool类型,如果不存在或出错则默认或def[0]。
    
    SetStatusCode(code int)            //设置http状态码。
    SetContentType(contentType string) //设置文档类型。
    
    ReadBody() []byte                                //获取请求体内容。
    ReadCopyBody() []byte                            //复制一份请求体出来。
    ReadBodyJsonBind(pointer interface{}) error      //读取请求体,json解码到结构体,参数pointer必须是地址类型。
    ReadBodyJsonBindWhere(pointer interface{}) error //读取请求体,json解码到结构体,参数pointer必须是地址类型。可以为结构体成员添加条件规则tag = where,目前只支持基本条件规则。

    Write(bs []byte)                                                    //写入响应体内容。该写入方式记录响应体内容。
    WriteFile(bs []byte, contentType string)                            //写入响应体内容,文件内容,该写入方式,不被记录响应体。
    WriteString(s string)                                               //写入响应体内容。该写入方式记录响应体内容。
    WriteJson(pointer interface{})                                      //写入响应体内容并设置文档类型为json,参数pointer必须是指针类型。该写入方式记录响应体内容。
    WriteStd(code int, msg string, data interface{}, other interface{}) //写入响应体内容并设置文档类型为json,会生成标准的json返回内容。该写入方式记录响应体内容。

    GetIP() netip.AddrPort        //获取IP地址,假如是反向代理过来的,获取的结果将会是反向代理服务器的IP。如果需要获取相对真实的IP,请使用GetFirstIP。
    GetFirstIP() string           //获取相对真实的IP地址,获取优先级:X-Real-IP、X-Forwarded-For、RemoteAddr。
    GetIpByXRealIP() string       //获取请求头X-Real-IP。
    GetIpByXForwardedFor() string //获取请求头X-Forwarded-For。
    
    GetPath() string  //获取请求路径。
    GetURL() *url.URL //获取请求URL。
    
    ParamsGet(key string, def ...string) string          //获取请求路径参数,如果不存在或出错则默认或def[0],比如 /user/:id 获取这个id。
    ParamsGetInt(key string, def ...int) int             //获取请求路径参数,如果不存在或出错则默认或def[0],并转为int类型,比如 /user/:id 获取这个id。
    ParamsGetInt64(key string, def ...int64) int64       //获取请求路径参数,如果不存在或出错则默认或def[0],并转为int64类型,比如 /user/:id 获取这个id。
    ParamsGetUint(key string, def ...uint) uint          //获取请求路径参数,如果不存在或出错则默认或def[0],并转为uint类型,比如 /user/:id 获取这个id。
    ParamsGetUint64(key string, def ...uint64) uint64    //获取请求路径参数,如果不存在或出错则默认或def[0],并转为uint64类型,比如 /user/:id 获取这个id。
    ParamsGetBool(key string, def ...bool) bool          //获取请求路径参数,如果不存在或出错则默认或def[0],并转为bool类型,比如 /user/:id 获取这个id。
    ParamsGetFloat64(key string, def ...float64) float64 //获取请求路径参数,如果不存在或出错则默认或def[0],并转为float64类型,比如 /user/:id 获取这个id。
    
    FormValue(key string, def ...string) string               //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0]。
    FormValues(key string, def ...[]string) []string          //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0]。
    FormValueInt(key string, def ...int) int                  //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int类型。
    FormValueInts(key string, def ...[]int) []int             //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int类型。
    FormValueUint(key string, def ...uint) uint               //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint类型。
    FormValueUints(key string, def ...[]uint) []uint          //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint类型。
    FormValueInt64(key string, def ...int64) int64            //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int64类型。
    FormValueInt64s(key string, def ...[]int64) []int64       //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int64类型。
    FormValueUint64(key string, def ...uint64) uint64         //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint64类型。
    FormValueUint64s(key string, def ...[]uint64) []uint64    //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint64类型。
    FormValueBool(key string, def ...bool) bool               //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为bool类型。
    FormValueBools(key string, def ...[]bool) []bool          //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为bool类型。
    FormValueFloat64(key string, def ...float64) float64      //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为float64类型。
    FormValueFloat64s(key string, def ...[]float64) []float64 //GET,DELETE请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为float64类型。
    FormValueBind(pointer interface{}) error                  //GET,DELETE请求,获取普通表单字段并且绑定结构体;字段名称为结构体成员的tag = json,只支持(不支持切片)基本的bool,int全家,uint全家,float全家,string类型。可以为结构体成员添加条件规则tag = where,目前只支持基本条件规则。
    
    PostFormValue(key string, def ...string) string               //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0]。
    PostFormValues(key string, def ...[]string) []string          //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0]。
    PostFormValueInt(key string, def ...int) int                  //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int类型。
    PostFormValueInts(key string, def ...[]int) []int             //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int类型。
    PostFormValueUint(key string, def ...uint) uint               //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint类型。
    PostFormValueUints(key string, def ...[]uint) []uint          //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint类型。
    PostFormValueInt64(key string, def ...int64) int64            //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int64类型。
    PostFormValueInt64s(key string, def ...[]int64) []int64       //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为int64类型。
    PostFormValueUint64(key string, def ...uint64) uint64         //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint64类型。
    PostFormValueUint64s(key string, def ...[]uint64) []uint64    //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为uint64类型。
    PostFormValueBool(key string, def ...bool) bool               //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为bool类型。
    PostFormValueBools(key string, def ...[]bool) []bool          //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为bool类型。
    PostFormValueFloat64(key string, def ...float64) float64      //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为float64类型。
    PostFormValueFloat64s(key string, def ...[]float64) []float64 //POST,PUT请求,获取普通表单字段,如果不存在或出错则默认或def[0],并转为float64类型。
    PostFormValueBind(pointer interface{}) error                  //POST,PUT请求,获取普通表单字段并且绑定结构体;字段名称为结构体成员的tag = json,只支持(不支持切片)基本的bool,int全家,uint全家,float全家,string类型。可以为结构体成员添加条件规则tag = where,目前只支持基本条件规则。
    
    File(key string) (multipart.File, *multipart.FileHeader, error) //POST,PUT请求,获取表单上传的文件。
    FileSave(key string, savePath string) (string, error)           //POST,PUT请求,获取表单上传的文件并保存,返回文件名sha256值+扩展名。
    FilesSave(savePath string) ([]string, error)                    //POST,PUT请求,接收多文件并且存储,返回文件名切片,文件名sha256值+扩展名。
    
    ResponseBody() []byte //响应体当前的内容。
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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