tsing

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: BSD-3-Clause Imports: 15 Imported by: 2

README

Tsing

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

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

go get -u github.com/dxvgef/tsing

手册:

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

完整的项目示例

dxvgef/tsing-demo 是一个基于Tsing整合常见功能的完整示例,可以做为项目初始化使用

基准测试

tsinghttprouterechogin等框架的基准测试对比
dxvgef/tsing-benchmark

测试结果:

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

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 {
	UseRawPath         bool         // 使用url.RawPath查找参数
	UnescapePathValues bool         // 反转义路由参数
	MaxMultipartMemory int64        // 分配给http.Request的值
	EventHandler       EventHandler // 事件-处理器函数,如果不赋值,则不启用事件
	EventTrace         bool         // 事件-启用跟踪信息
	EventShortPath     bool         // 事件-启用短文件路径
	RootPath           string       // 应用的根路径
	EventHandlerError  bool         // 事件-启用处理器返回的错误
	EventSource        bool         // 事件-启用来源
	Recover            bool         // 自动恢复panic
}

引擎配置

type Context

type Context struct {
	PathParams PathParams

	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) 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) 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)

向客户端发送重定向响应

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) Source added in v1.0.0

func (ctx *Context) Source(err error) 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(path 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(path string, handlers ...Handler) RouterInterface

注册DELETE路由

func (*Router) Dir added in v1.0.0

func (router *Router) Dir(path, serverPath string) RouterInterface

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

func (*Router) File added in v1.0.0

func (router *Router) File(path, file string) RouterInterface

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

func (*Router) GET added in v1.0.0

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

注册GET路由

func (*Router) Group added in v1.0.0

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

定义路由组

func (*Router) HEAD added in v1.0.0

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

注册HEAD路由

func (*Router) Handle added in v1.0.0

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

注册自定义HTTP方法的路由

func (*Router) OPTIONS added in v1.0.0

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

注册OPTIONS路由

func (*Router) PATCH added in v1.0.0

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

注册PATCH路由

func (*Router) POST added in v1.0.0

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

注册POST路由

func (*Router) PUT added in v1.0.0

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

注册PUT路由

Jump to

Keyboard shortcuts

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