rx

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request *http.Request
	Writer  ResponseWriter
	Params  Params

	Keys map[string]any
	// contains filtered or unexported fields
}

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AbortWithError added in v0.0.6

func (c *Context) AbortWithError(err error)

func (*Context) AbortWithJSON

func (c *Context) AbortWithJSON(code int, obj any)

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

func (*Context) Aborted added in v0.0.7

func (c *Context) Aborted() bool

func (*Context) Context added in v0.0.6

func (c *Context) Context() context.Context

func (*Context) Get

func (c *Context) Get(key string) (value any, exists bool)

func (*Context) JSON

func (c *Context) JSON(code int, obj any)

func (*Context) Next

func (c *Context) Next()

func (*Context) Param added in v0.0.6

func (c *Context) Param(name string) string

func (*Context) Render

func (c *Context) Render(code int, r Render)

func (*Context) Route

func (c *Context) Route() Route

func (*Context) Set

func (c *Context) Set(key string, value any)

func (*Context) Status

func (c *Context) Status(code int)

func (*Context) String

func (c *Context) String(code int, s string)

func (*Context) UseContext added in v0.0.7

func (c *Context) UseContext(ctx context.Context)

UseContext 替换当前请求的上下文。

后续的中间件和处理函数通过 Context.Context() 获取到的将是新的上下文, 常用于超时控制、链路追踪等场景。传入 nil 时不做任何修改。

type Engine

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

func New

func New() *Engine

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*Engine) Use

func (e *Engine) Use(middleware ...HandlerFunc)

Use 追加全局中间件。

全局中间件对所有请求生效,按注册顺序依次执行,并且先于路由自身的处理函数。 多次调用会依次累加;需要终止后续处理时调用 Context.Abort。

func (*Engine) UseErrorEncoder added in v0.0.8

func (e *Engine) UseErrorEncoder(encoder ErrorEncoder)

UseErrorEncoder 设置错误编码器。

请求处理过程中发生错误时,ErrorEncoder 负责把错误转换成响应。 传入 nil 时恢复为默认实现,即返回 500 并输出错误信息。 多次调用以最后一次为准。

func (*Engine) UseNotFoundHandler added in v0.0.8

func (e *Engine) UseNotFoundHandler(handlers ...HandlerFunc)

UseNotFoundHandler 设置路由未匹配时的兜底处理器。

该处理器只在没有任何路由匹配时执行(即返回 404),并且位于全局中间件之后。 若处理器没有输出任何内容,Engine 会输出默认的 404 提示。 多次调用以最后一次为准。

func (*Engine) UseRouter added in v0.0.6

func (e *Engine) UseRouter(router Router)

UseRouter 设置路由表。

Router 负责把 method 和 path 匹配到具体的处理函数,是 Engine 处理请求的前提。 传入 nil 时使用默认实现,此时所有请求都会返回 400 并提示如何正确指定路由。 多次调用以最后一次为准。

type ErrorEncoder added in v0.0.8

type ErrorEncoder func(c *Context, err error)

type HandlerFunc

type HandlerFunc func(c *Context)

func Logger added in v0.0.6

func Logger() HandlerFunc

type HandlersChain

type HandlersChain []HandlerFunc

func CombineHandlers added in v0.0.6

func CombineHandlers(base HandlersChain, handlers ...HandlerFunc) HandlersChain

type Param added in v0.0.6

type Param struct {
	// Key 是规则中声明的参数名,不包含前缀的 : 或 *。
	Key string

	// Value 是输入路径中被该参数捕获到的原始字符串。
	Value string
}

Param 是一次路径匹配得到的单个参数。

type Params added in v0.0.6

type Params []Param

Params 是按规则中参数出现顺序保存的参数列表。

func (Params) ByName added in v0.0.6

func (ps Params) ByName(name string) string

ByName 返回指定名称的参数;不存在时返回空字符串。

func (Params) Get added in v0.0.6

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

Get 返回指定名称的参数。

type Render

type Render interface {
	Render(http.ResponseWriter) error

	ContentType() []string
}

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher

	// Status returns the HTTP response status code of the current request.
	Status() int

	// Size returns the number of bytes already written into the response http body.
	// See Written()
	Size() int

	// WriteString writes the string into the response body.
	WriteString(string) (int, error)

	// Written returns true if the response body was already written.
	Written() bool

	// WriteHeaderNow forces to write the http header (status code + headers).
	WriteHeaderNow()

	// Pusher get the http.Pusher for server push
	Pusher() http.Pusher
}

ResponseWriter ...

type Route

type Route interface {
	Path() string
	Handlers() HandlersChain
}

Route 表示 Router 已经匹配到的处理目标。

func NewRoute

func NewRoute(path string, handlers ...HandlerFunc) Route

NewRoute 创建只执行本地处理函数的路由。

type Router added in v0.0.6

type Router interface {
	Match(method string, path string, params Params) (Route, Params, error)
}

Router 负责根据 method 和 path 匹配并返回 Route。

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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