tsing

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

README

Tsing

Tsing是一个Go语言的HTTP API框架,具有以下功能特性:

  • 高性能,零内存分配
  • 微核心,仅包含路由和会话管理两大模块
  • 轻量,无第三方包依赖,兼容net/http标准包
  • 可自动处理路由处理器中的Panic错误,防止进程退出
  • 使用回调函数代替传统的内置Logger机掉,异常处理更灵活
  • 支持后置回调处理器AfterHandler(仅在路由命中时有效)

Tsing是汉字【青】以及同音字做为名词时的英文,例如:清华大学(Tsinghua University)、青岛(Tsing Tao)。

已经在多个项目中稳定运行。

执行流程

  1. 根据URI查找路由
  2. 执行Use()方法注册的中间件
  3. 执行GET()等方法注册的路由处理器
  4. 执行AfterHandler后置处理器,无视之前处理器的执行结果
  • 如果任意环节的处理器出现了以下情况,会中止执行后面的处理器
    • 处理器返回了error
    • 处理器中执行了Context.Break()
    • 处理器执行时触发了panic
  • 如果路由未命中,只会执行ErrorHandler错误回调处理器,不会触发中间件和后置处理器

安装

要求:Go 1.18+

github.com/dxvgef/tsing/v2

示例

请参考/example_test.go文件

基准测试

测试代码:github.com/dxvgef/tsing-benchmark

Benchmark_TsingV2-8                        50865             23725 ns/op               0 B/op          0 allocs/op
Benchmark_TsingV2_Recover-8                48708             24582 ns/op               0 B/op          0 allocs/op
Benchmark_TsingV1-8                        48664             24875 ns/op               0 B/op          0 allocs/op
Benchmark_TsingV1_Recover-8                45986             26267 ns/op               0 B/op          0 allocs/op
Benchmark_Gin-8                            47978             24542 ns/op               0 B/op          0 allocs/op
Benchmark_Gin_Recover-8                    43753             27390 ns/op               0 B/op          0 allocs/op
Benchmark_Httprouter-8                     46738             25555 ns/op           13792 B/op        167 allocs/op
Benchmark_Httprouter_Recover-8             44786             26703 ns/op           13792 B/op        167 allocs/op
Benchmark_Echo-8                           38401             31216 ns/op               0 B/op          0 allocs/op
Benchmark_Echo_Recover-8                   28674             41750 ns/op            9748 B/op        203 allocs/op
Benchmark_HTTPTreemux-8                    15448             77755 ns/op           65857 B/op        671 allocs/op

相关资源

  • dxvgef/tsing-demo Tsing整合常见功能的示例项目,可以做为新项目初始化使用
  • dxvgef/filter 参数值过滤包,由数据输入、格式化、校验、输出几个部份组成
  • Tsing Gateway 跨平台、去中心化集群、动态配置的API网关
  • Tsing Center 跨平台、去中心化集群、动态配置的服务中心

用户案例

如果你在使用本项目,请通过Issues告知我们项目的简介

帮助反馈

本项目已在多个项目的生产环境中稳定运行。如有问题可在Issues里提出。

诚邀更多的开发者参与到本项目维护中,帮助这个开源项目更好的发展。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackHandler added in v2.1.0

type CallbackHandler func(*Context)

CallbackHandler 回调处理器

type Config

type Config struct {
	MaxMultipartMemory     int64           // 允许的请求Body大小(默认32 << 20 = 32MB)
	Recovery               bool            // 自动恢复panic,防止进程退出
	HandleMethodNotAllowed bool            // 不处理 405 错误(可以减少路由匹配时间),以 404 错误返回
	ErrorHandler           CallbackHandler // 错误回调处理器
	AfterHandler           CallbackHandler // 后置回调处理器,总是会在其它处理器全部执行完之后执行
}

Config 引擎参数配置

type Context

type Context struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	Status         int   // 处理器执行结果的状态码(HTTP)
	Error          error // 处理器执行错误时的消息
	// contains filtered or unexported fields
}

Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.

func (*Context) Abort added in v2.1.0

func (ctx *Context) Abort() *Context

Abort 停止执行该路由注册的其它处理器

func (*Context) AllFormValue

func (ctx *Context) AllFormValue() url.Values

AllFormValue 获取所有Form参数

func (*Context) AllPathValue

func (ctx *Context) AllPathValue() []Param

AllPathValues 获取所有路径参数值

func (*Context) AllQueryValue

func (ctx *Context) AllQueryValue() url.Values

AllQueryValue 获取所有GET参数

func (*Context) EngineConfig

func (ctx *Context) EngineConfig() Config

EngineConfig 获取引擎配置

func (*Context) FileFromFS

func (ctx *Context) FileFromFS(filepath string, fs http.FileSystem)

FileFromFS writes the specified file from http.FileSystem into the body stream in an efficient way.

func (*Context) FormFile

func (ctx *Context) FormFile(name string) (*multipart.FileHeader, error)

FormFile 根据参数名获取上传的第一个文件

func (*Context) FormFiles

func (ctx *Context) FormFiles(name string) ([]*multipart.FileHeader, error)

FormFiles 根据参数名获取上传的所有文件

func (*Context) FormParam

func (ctx *Context) FormParam(key string) (string, bool)

FormParam 获取某个Form参数的第一个值,并判断参数是否存在

func (*Context) FormParams

func (ctx *Context) FormParams(key string) ([]string, bool)

FormParams 获取某个Form参数的所有值,并判断参数是否存在

func (*Context) FormValue

func (ctx *Context) FormValue(key string) string

FormValue 获取某个Form参数值的string类型

func (*Context) FormValues

func (ctx *Context) FormValues(key string) []string

FormValues 获取某个Form参数的所有值

func (*Context) FullPath

func (ctx *Context) FullPath() string

FullPath 返回路由注册时的路径

func (*Context) GetValue

func (ctx *Context) GetValue(key any) any

GetValue 从Context中读取键值

func (*Context) InitFormCache

func (ctx *Context) InitFormCache() error

InitFormCache 初始化表单参数缓存

func (*Context) IsAborted added in v2.1.0

func (ctx *Context) IsAborted() bool

IsAborted 判断是否已停止执行其它处理器

func (*Context) JSON

func (ctx *Context) JSON(status int, data any, charset ...string) error

JSON 输出JSON

func (*Context) NoContent

func (ctx *Context) NoContent() error

NoContent 输出204状态码

func (*Context) ParseJSON

func (ctx *Context) ParseJSON(obj any) error

ParseJSON 将json格式的body数据反序列化到传入的对象

func (*Context) PathParam

func (ctx *Context) PathParam(key string) (string, bool)

PathParam 获取路径参数,并判断参数是否存在

func (*Context) PathValue

func (ctx *Context) PathValue(key string) string

PathValue 获取路径参数值

func (*Context) QueryParam

func (ctx *Context) QueryParam(key string) (string, bool)

QueryParam 获取某个GET参数的第一个值,并判断参数是否存在

func (*Context) QueryParams

func (ctx *Context) QueryParams(key string) ([]string, bool)

QueryParams 获取某个GET参数的所有值,并判断参数是否存在

func (*Context) QueryValue

func (ctx *Context) QueryValue(key string) string

QueryValue 获取某个GET参数的第一个值

func (*Context) QueryValues

func (ctx *Context) QueryValues(key string) []string

QueryValues 获取某个GET参数的所有值

func (*Context) Redirect

func (ctx *Context) Redirect(code int, url string) error

Redirect 向客户端发送重定向响应

func (*Context) SaveFile

func (ctx *Context) SaveFile(fileHeader *multipart.FileHeader, savePath string, perm os.FileMode) error

SaveFile 保存上传的文件到本地路径

func (*Context) ServeFile

func (ctx *Context) ServeFile(filePath string)

ServeFile 将服务端指定路径的文件写入到客户端流

func (*Context) SetValue

func (ctx *Context) SetValue(key, value any)

SetValue 在Context中写入键值,可用于在本次会话的处理器链中传递

func (*Context) StatusCode added in v2.0.1

func (ctx *Context) StatusCode(code int) (err error)

StatusCode 输出状态码

func (*Context) String

func (ctx *Context) String(status int, data string, charset ...string) (err error)

String 输出字符串

type Engine

type Engine struct {
	RouterGroup
	// contains filtered or unexported fields
}

Engine 引擎

func New

func New(config ...Config) *Engine

New 新建引擎实例

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Handler added in v2.1.0

type Handler func(*Context) error

Handler 路由处理器

type HandlersChain

type HandlersChain []Handler

HandlersChain 处理器链

type Node

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

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) (va string)

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

func (Params) Get

func (ps Params) Get(name string) (string, bool)

Get 获取路径参数

type Router

type Router interface {
	Routes
	Group(string, ...Handler) *RouterGroup
}

Router 路由器接口,包括单路由和路由组

type RouterGroup

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

RouterGroup 路由组

func (*RouterGroup) DELETE

func (group *RouterGroup) DELETE(relativePath string, handlers ...Handler)

DELETE 注册DELETE方法的路由

func (*RouterGroup) GET

func (group *RouterGroup) GET(relativePath string, handlers ...Handler)

GET 注册GET方法的路由

func (*RouterGroup) Group

func (group *RouterGroup) Group(relativePath string, handlers ...Handler) *RouterGroup

Group 注册路由组

func (*RouterGroup) HEAD

func (group *RouterGroup) HEAD(relativePath string, handlers ...Handler)

HEAD 注册HEAD方法的路由

func (*RouterGroup) Handle

func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Handler)

Handle 注册自定义方法的路由

func (*RouterGroup) Match

func (group *RouterGroup) Match(methods []string, relativePath string, handlers ...Handler)

Match 为一个路径同时注册多个方法的路由

func (*RouterGroup) OPTIONS

func (group *RouterGroup) OPTIONS(relativePath string, handlers ...Handler)

OPTIONS 注册OPTIONS方法的路由

func (*RouterGroup) PATCH

func (group *RouterGroup) PATCH(relativePath string, handlers ...Handler)

PATCH 注册PATCH方法的路由

func (*RouterGroup) POST

func (group *RouterGroup) POST(relativePath string, handlers ...Handler)

POST 注册POST方法的路由

func (*RouterGroup) PUT

func (group *RouterGroup) PUT(relativePath string, handlers ...Handler)

PUT 注册PUT方法的路由

func (*RouterGroup) Static added in v2.2.0

func (group *RouterGroup) Static(relativePath, localPath string, listDir bool)

Static 注册一个指向服务端本地目录的静态路由,例如: Static("/public", "./public")

func (*RouterGroup) StaticFile

func (group *RouterGroup) StaticFile(relativePath, filePath string)

StaticFile 注册一个指向服务端本地文件的静态路由,例如: StaticFile("favicon.ico", "./resources/favicon.ico")

func (*RouterGroup) StaticFileFS

func (group *RouterGroup) StaticFileFS(relativePath, filePath string, fs http.FileSystem)

StaticFileFS 与StaticFile函数类型,但可以自定义文件系统,例如: StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})

func (*RouterGroup) Use added in v2.1.0

func (group *RouterGroup) Use(handlers ...Handler)

Use 使用中间件

type Routes

type Routes interface {
	Use(...Handler)

	After(...Handler)
	Handle(string, string, ...Handler)
	GET(string, ...Handler)
	POST(string, ...Handler)
	DELETE(string, ...Handler)
	PATCH(string, ...Handler)
	PUT(string, ...Handler)
	OPTIONS(string, ...Handler)
	HEAD(string, ...Handler)
	Match([]string, string, ...Handler)
}

Routes 定义所有路由器接口

Jump to

Keyboard shortcuts

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