rui

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
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` //八位字节流
)

Variables

This section is empty.

Functions

func CorsBind added in v0.0.2

func CorsBind(app RouterGroup, corsFunc func(Context))

CorsBind 创建一个跨越绑定

func NewCors added in v0.0.2

func NewCors() func(Context)

NewCors 创建一个跨域处理函数

Types

type Application

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方式运行
	OpenValidator()                                                       //开启验证器
}

func New

func New(logLevel int) Application

New 新建一个服务 logLevel 日志打印等级:调试 0;信息 1;警告 2;错误 3;恐慌 4;不打印 5

type Context

type Context interface {
	Request() *http.Request      //返回原生的请求指针。
	ResponseHeader() http.Header //响应头管理。

	StopNext()                                       //终止后续响应函数。
	SetKV(key string, value any)                     //设置key-value,可以供后续流程获取到。
	GetKV(key string) (any, 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 any) error      //读取请求体,json解码到结构体,参数pointer必须是地址类型。
	ReadBodyJsonBindWhere(pointer any) error //读取请求体,json解码到结构体,参数pointer必须是地址类型。validator验证器。

	Write(bs []byte)                                    //写入响应体内容。该写入方式记录响应体内容。
	WriteFile(bs []byte, contentType string)            //写入响应体内容,文件内容,该写入方式,不被记录响应体。
	WriteString(s string)                               //写入响应体内容。该写入方式记录响应体内容。
	WriteJson(pointer any)                              //写入响应体内容并设置文档类型为json,参数pointer必须是指针类型。该写入方式记录响应体内容。
	WriteStd(code int, msg string, data any, other any) //写入响应体内容并设置文档类型为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 any) error                          //GET,DELETE请求,获取普通表单字段并且绑定结构体,结构体字段不能为数组或切片;值绑定采用tag = form或json,默认validator验证器。

	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 any) error                          //POST,PUT请求,获取普通表单字段并且绑定结构体,结构体字段不能为数组或切片;值绑定采用tag = form或json,默认validator验证器。

	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 //响应体当前的内容。
}

type RouterGroup

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响应,是不过中间件的。
	// contains filtered or unexported methods
}

type RouterItem

type RouterItem struct {
	URL     string `json:"url"`      //请求路径
	Method  string `json:"method"`   //请求方法
	CodeRow string `json:"code_row"` //注册代码行
}

Jump to

Keyboard shortcuts

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