tsing

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2021 License: BSD-3-Clause Imports: 18 Imported by: 2

README

Tsing

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

  • 高性能,零内存分配
  • 微核心,方便二次开发
  • 轻量,无第三方包依赖,兼容net/http标准包
  • 统一异常处理,减少代码量,使开发者专注于业务逻辑

Tsing诞生比较晚也没有刻意的宣传推广,但由于框架核心非常精简,可靠性并不会低于其它热门框架,并且基准测试结果证明它的性能要强于所有参与测试的主流框架,已经在多个未公开的项目中稳定运行。

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

github.com/dxvgef/tsing

手册:

更多示例代码请参考/example_test.go文件

基准测试

测试结果:

Benchmark_Tsing_V1-4                       42688             26372 ns/op               0 B/op          0 allocs/op
Benchmark_Tsing_V1_Recover-4               41553             27571 ns/op               0 B/op          0 allocs/op
Benchmark_Httprouter-4                     33806             32360 ns/op           13792 B/op        167 allocs/op
Benchmark_Httprouter_Recover-4             35547             33129 ns/op           13792 B/op        167 allocs/op
Benchmark_Gin-4                            33469             34294 ns/op            6497 B/op        203 allocs/op
Benchmark_Gin_Recover-4                    31071             37423 ns/op            6497 B/op        203 allocs/op
Benchmark_Echo-4                           31489             36706 ns/op               0 B/op          0 allocs/op
Benchmark_Echo_Recover-4                   21991             53318 ns/op            9745 B/op        203 allocs/op

dxvgef/tsing-benchmarktsing,httprouter,echo,gin,chi等框架的基准测试代码,不定期更新各框架的版本或加入新的框架进行测试

相关资源

  • dxvgef/tsing-demo Tsing整合常见功能的示例项目,可以做为新项目初始化使用
  • Tsing Gateway 开源、跨平台、去中心化集群、动态配置的API网关
  • Tsing Center 开源、跨平台、去中心化集群、动态配置的服务中心

用户及案例

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

帮助/说明

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

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

Documentation

Index

Constants

View Source
const MaxMultipartMemory = 1 << 20

默认body限制

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.8.0

type Config struct {
	RootPath           string // 应用的根路径
	AllowOrigins       string
	ExposeHeaders      string
	AllowMethods       string
	AllowHeaders       string
	Charset            string       // 响应的字符集,默认是UTF-8
	MaxMultipartMemory int64        // 允许的请求Body大小(默认1 << 20 = 1MB)
	EventHandler       EventHandler // 事件-处理器函数,如果不赋值,则不启用事件
	UseRawPath         bool         // 使用url.RawPath查找参数
	UnescapePathValues bool         // 反转义路由参数
	EventTrace         bool         // 事件-启用跟踪信息
	EventShortPath     bool         // 事件-启用短文件路径
	EventHandlerError  bool         // 事件-启用处理器返回的错误
	EventSource        bool         // 事件-启用来源
	Recover            bool         // 自动恢复处理器的panic
	CORS               bool         // 是否启用自动CORS处理
	AllowCredentials   bool
}

引擎配置

type Context

type Context struct {
	ResponseWriter http.ResponseWriter

	Request *http.Request
	// contains filtered or unexported fields
}

上下文

func (*Context) Abort added in v1.0.0

func (ctx *Context) Abort()

中止执行

func (*Context) Caller added in v1.2.0

func (ctx *Context) Caller(err error) error

在处理器函数内return一个error时,用Caller可以记录下处理器内的行号详细信息

func (*Context) Form added in v1.0.0

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

获取某个GET/POST/PUT参数值的string类型

func (*Context) FormParam added in v1.1.0

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

获取单个GET/POST/PUT参数

func (*Context) FormParams added in v1.1.0

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

获取所有GET/POST/PUT参数值

func (*Context) GetValue added in v0.7.0

func (ctx *Context) GetValue(key interface{}) interface{}

从Context中取值

func (*Context) IsAborted added in v1.0.0

func (ctx *Context) IsAborted() bool

是否已中止

func (*Context) JSON added in v1.4.0

func (ctx *Context) JSON(status int, data interface{}, charset ...string) error

输出JSON

func (*Context) ParseJSON added in v1.4.0

func (ctx *Context) ParseJSON(obj interface{}) error

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

func (*Context) Path added in v1.6.0

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

获取某个路由参数值的string类型

func (*Context) PathParams added in v1.0.0

func (ctx *Context) PathParams() PathParams

获取所有路由参数值

func (*Context) Post added in v0.7.0

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

获取某个POST/PATCH/PUT参数值的string类型

func (*Context) PostParam added in v1.1.0

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

获取某个POST/PATCH/PUT参数

func (*Context) PostParams added in v1.1.0

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

获取所有POST/PATCH/PUT参数值

func (*Context) Query added in v0.7.0

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

获取某个GET参数值的string类型

func (*Context) QueryParam added in v1.1.0

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

获取某个GET参数

func (*Context) QueryParams added in v1.1.0

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

获取所有GET参数值

func (*Context) Redirect

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

向客户端发送重定向响应

func (*Context) RemoteIP added in v0.7.0

func (ctx *Context) RemoteIP() string

获得客户端真实IP

func (*Context) SetValue added in v0.7.0

func (ctx *Context) SetValue(key, value interface{})

在Context中写值

func (*Context) Status added in v1.4.1

func (ctx *Context) Status(status int) (err error)

输出状态码

func (*Context) String added in v1.4.0

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

输出字符串

type Engine added in v1.0.0

type Engine struct {
	*Router         // 路由器
	Config  *Config // 配置
	// contains filtered or unexported fields
}

引擎

func New

func New(config *Config) *Engine

创建一个新引擎

func (*Engine) ServeHTTP added in v1.0.0

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

实现http.Handler接口,并且是连接调度的入口

type Event

type Event struct {
	Status         int      // HTTP状态码
	Message        error    // 消息(error)
	Source         *_Source // 来源
	Trace          []string // 跟踪
	ResponseWriter http.ResponseWriter
	Request        *http.Request
}

事件

type EventHandler

type EventHandler func(*Event)

事件处理器

type Handler

type Handler func(*Context) error

路由处理器

type HandlersChain added in v1.0.0

type HandlersChain []Handler

处理器链

type PathParam added in v1.0.0

type PathParam struct {
	Key   string
	Value string
}

路由中的路径参数

type PathParams added in v1.0.0

type PathParams []PathParam

func (PathParams) Get added in v1.0.0

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

获得路径参数

func (PathParams) Value added in v1.0.0

func (ps PathParams) Value(name string) (value string)

获得路径参数值

type Router added in v1.0.0

type Router struct {
	Handlers HandlersChain
	// contains filtered or unexported fields
}

路由组

func (*Router) Any added in v1.0.0

func (router *Router) Any(relativePath string, handlers ...Handler) RouterInterface

注册所有路由

func (*Router) Append added in v1.0.0

func (router *Router) Append(handlers ...Handler) RouterInterface

添加处理器

func (*Router) DELETE added in v1.0.0

func (router *Router) DELETE(urlPath string, handlers ...Handler) RouterInterface

注册DELETE路由

func (*Router) Dir added in v1.0.0

func (router *Router) Dir(urlPath, absPath string) RouterInterface

注册一个响应服务端目录的路由

func (*Router) File added in v1.0.0

func (router *Router) File(urlPath, absPath string) RouterInterface

注册一个响应服务端文件的路由

func (*Router) GET added in v1.0.0

func (router *Router) GET(urlPath string, handlers ...Handler) RouterInterface

注册GET路由

func (*Router) Group added in v1.0.0

func (router *Router) Group(urlPath string, handlers ...Handler) *Router

定义路由组

func (*Router) HEAD added in v1.0.0

func (router *Router) HEAD(urlPath string, handlers ...Handler) RouterInterface

注册HEAD路由

func (*Router) Handle added in v1.0.0

func (router *Router) Handle(method, urlPath string, handlers ...Handler) RouterInterface

注册自定义HTTP方法的路由

func (*Router) OPTIONS added in v1.0.0

func (router *Router) OPTIONS(urlPath string, handlers ...Handler) RouterInterface

注册OPTIONS路由

func (*Router) PATCH added in v1.0.0

func (router *Router) PATCH(urlPath string, handlers ...Handler) RouterInterface

注册PATCH路由

func (*Router) POST added in v1.0.0

func (router *Router) POST(urlPath string, handlers ...Handler) RouterInterface

注册POST路由

func (*Router) PUT added in v1.0.0

func (router *Router) PUT(urlPath string, handlers ...Handler) RouterInterface

注册PUT路由

Jump to

Keyboard shortcuts

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