You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB
Go

package router
import (
"reflect"
ctxpkg "github.com/noahlann/nnet/pkg/context"
)
// Router 路由器接口
type Router interface {
// Register 注册路由(使用匹配器)
Register(matcher Matcher, handler Handler, opts ...RouteOption) Route
// RegisterString 注册字符串路由
RegisterString(pattern string, handler Handler, opts ...RouteOption) Route
// RegisterFrameHeader 注册帧头匹配路由
RegisterFrameHeader(field, operator string, value interface{}, handler Handler, opts ...RouteOption) Route
// RegisterFrameData 注册帧数据匹配路由
RegisterFrameData(path, operator string, value interface{}, handler Handler, opts ...RouteOption) Route
// RegisterCustom 注册自定义匹配路由
RegisterCustom(fn func(input MatchInput, ctx ctxpkg.Context) bool, handler Handler, opts ...RouteOption) Route
// Group 创建路由分组
Group() RouterGroup
// Match 匹配路由
Match(input MatchInput, ctx ctxpkg.Context) (Route, Handler, error)
}
// RouterGroup 路由分组接口
type RouterGroup interface {
Router
// Use 添加中间件
Use(middleware ...Handler) RouterGroup
// Prefix 获取分组前缀
Prefix() string
}
// RouteOption 路由选项
type RouteOption func(*RouteConfig)
// RouteConfig 路由配置
type RouteConfig struct {
RequestType reflect.Type // 请求类型
ResponseType reflect.Type // 响应类型(可选,主要用于验证)
CodecName string // 编解码器名称(可选,使用默认编解码器)
}
// WithRequestType 指定请求类型
func WithRequestType(typ interface{}) RouteOption {
return func(cfg *RouteConfig) {
cfg.RequestType = reflect.TypeOf(typ)
}
}
// WithResponseType 指定响应类型(可选,主要用于验证)
func WithResponseType(typ interface{}) RouteOption {
return func(cfg *RouteConfig) {
cfg.ResponseType = reflect.TypeOf(typ)
}
}
// WithCodec 指定编解码器名称
func WithCodec(codecName string) RouteOption {
return func(cfg *RouteConfig) {
cfg.CodecName = codecName
}
}