README
¶
路由
本包仅用于 http 路由匹配,使用场景为路径参数解析。例如: /apis/{group}/{version}.
动机
原使用的 gin 框架,其路由匹配使用的前缀树实现,匹配速度较快。但是在使用过程中有许多的限制, 例如不能实现确定路径与通配路径同时存在(/api/core/v1 与 /{group}/{version}同时存在),相同前缀的路由无法被注册(/app1, /app2). 无法在路径中注册带":"的路由(google api 中的自定义方法),以及其他复杂的匹配问题。
语法
- 使用 '{' 与'}'定义变量匹配,其间的字符作为变量名称。可以使用 "{}"定义无名称变量。
- 使用 '*' 作为最后一个字符表示向后匹配。/{name}*,将使 name 向后匹配。
- 其他字符作为常规字符进行匹配。
路由匹配
固定匹配
-
/api/v1/
- ✅/api/v1/
- ❌/api/v1
- ❌/api/v11
- ❌/api/v789
-
/api/v1*
- ✅/api/v1/
- ✅/api/v1
- ✅/api/v11
- ✅/api/v11/hello
- ✅/api/v1/hello
- ❌/api/v789
-
/api/*1 (非后缀的*作为普通字符对待)
- ✅/api/*1
- ❌/api/v1
- ❌/api/v/1
变量匹配
-
/api/{version}
- ✅/api/*1;version=*1
- ✅/api/v1;version=v1
- ❌/api/v/1
- ❌/api/v1/
-
/api/v{version}
- ✅/api/v1;version=1
- ✅/api/vhello;version=hello
- ✅/api/v*1;version=*1
- ❌/api/v1/
- ❌/api/v/1
-
/api/v{version}*
- ✅/api/v1;version=1
- ✅/api/vhello/world;version=hello/world
- ✅/api/v/1;version=/1
- ❌/apis/v1
-
/api/v{version}/{group}
- ✅/api/v1/;version=1,group=
- ✅/api/vhello/world;version=hello,group=world
- ❌/api/v1;version=1
- ✅/api/v/1;version=,group=1
-
/person/{firstname}-{lastname}
- ✅/person/jack-ma;firstname=jack,lastname=ma
- ❌/person/jack-ma/
- ❌/person/jackma
向后匹配
-
/prefix/{path}*
- ✅/prefix/abc;path=abc
- ✅/prefix/abc/def;path=abc/def
- ❌/prefixabc;
-
/prefix*
- ✅/prefix/abc
- ✅/prefixabc/def
- ❌/pref/jackma
混合使用
若同时定义如下路由:
- /api/v{version}/{group}
- /api/v1/{group}
- /api/v1/core
则若同时满足上述条件下,路径参数少的匹配被选中。
举例:
- 请求/api/v1/core,则匹配中 /api/v1/core;
- 请求/api/v1/hello,则匹配中 /api/v1/{group};
- 请求/api/v2/hello,则匹配中 /api/v{version}/{group};
Documentation
¶
Index ¶
- Constants
- Variables
- func Build(data interface{}) *spec.Schema
- func BuildOpenAPIWebService(wss []*restful.WebService, path string, postfun func(swagger *spec.Swagger)) *restful.WebService
- func CompilePathPattern(pattern string) ([][]Element, error)
- func IntFmtProperty(format string) *spec.Schema
- func IsDynamic(t reflect.Type) bool
- func MatchSection(compiled []Element, sections []string) (bool, bool, map[string]string)
- func ObjectProperty() *spec.Schema
- func ObjectPropertyProperties(properties spec.SchemaProperties) *spec.Schema
- func ParamIn(kind int) string
- func ParsePathTokens(path string) []string
- func TypeName(t reflect.Type) string
- type Builder
- type CompileError
- type Element
- type ElementKind
- type Function
- type Group
- type InterfaceBuildOption
- type Param
- type ParamKind
- type PathTokens
- type ResponseMeta
- type Route
- func (n *Route) Accept(mime ...string) *Route
- func (n *Route) ContentType(mime ...string) *Route
- func (n *Route) Doc(summary string) *Route
- func (n *Route) Paged() *Route
- func (n *Route) Parameters(params ...Param) *Route
- func (n *Route) Response(body interface{}, descs ...string) *Route
- func (n *Route) SetProperty(k string, v interface{}) *Route
- func (n *Route) ShortDesc(summary string) *Route
- func (n *Route) Tag(tags ...string) *Route
- func (n *Route) To(fun Function) *Route
- type Router
- func (m *Router) ANY(path string, handler gin.HandlerFunc)
- func (m *Router) DELETE(path string, handler gin.HandlerFunc)
- func (m *Router) GET(path string, handler gin.HandlerFunc)
- func (m *Router) Match(c *gin.Context) gin.HandlerFunc
- func (m *Router) MustRegister(method, path string, handler gin.HandlerFunc)
- func (m *Router) PATCH(path string, handler gin.HandlerFunc)
- func (m *Router) POST(path string, handler gin.HandlerFunc)
- func (m *Router) PUT(path string, handler gin.HandlerFunc)
- func (m *Router) Register(method, path string, handler gin.HandlerFunc) error
- type SchemaBuildFunc
- type ServeMux
- type SwaggerBuilder
- type Tree
Constants ¶
View Source
const DefinitionsRoot = "#/definitions/"
Variables ¶
View Source
var ContextKeyPathVars = struct{ name string }{/* contains filtered or unexported fields */}
using get pathvars from context.Context returns map[string]string{}
View Source
var DefaultBuilder = NewBuilder(InterfaceBuildOptionOverride)
View Source
var DefaultNotFoundHandler = gin.WrapF(http.NotFound)
Functions ¶
func BuildOpenAPIWebService ¶
func CompilePathPattern ¶
func IntFmtProperty ¶
StrFmtProperty creates a property for the named string format
func MatchSection ¶
func ObjectProperty ¶
func ObjectPropertyProperties ¶ added in v1.23.9
func ObjectPropertyProperties(properties spec.SchemaProperties) *spec.Schema
func ParsePathTokens ¶
Types ¶
type Builder ¶
type Builder struct { InterfaceBuildOption InterfaceBuildOption Definitions map[string]spec.Schema }
func NewBuilder ¶
func NewBuilder(interfaceOption InterfaceBuildOption) *Builder
type CompileError ¶
func (CompileError) Error ¶
func (e CompileError) Error() string
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
func CompileSection ¶
func MustCompileSection ¶
type ElementKind ¶
type ElementKind string
const ( ElementKindNone ElementKind = "" ElementKindConst ElementKind = "const" ElementKindVariable ElementKind = "{}" ElementKindStar ElementKind = "*" ElementKindSplit ElementKind = "/" )
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func (*Group) AddSubGroup ¶
func (*Group) Parameters ¶
type InterfaceBuildOption ¶
type InterfaceBuildOption string
const ( InterfaceBuildOptionDefault InterfaceBuildOption = "" // default as an 'object{}' InterfaceBuildOptionOverride InterfaceBuildOption = "override" // override using interface's value if exist InterfaceBuildOptionIgnore InterfaceBuildOption = "ignore" // ignore interface field InterfaceBuildOptionMerge InterfaceBuildOption = "merge" // anyOf 'object{}' type and interface's value type )
type Param ¶
type Param struct { Name string Kind ParamKind Type string IsOptional bool Description string Example interface{} }
func BodyParameter ¶
func FormParameter ¶
func PathParameter ¶
func QueryParameter ¶
type PathTokens ¶
type PathTokens struct {
Tokens []string
}
type ResponseMeta ¶
type Route ¶
type Route struct { Summary string Path string Method string Func Function Tags []string Consumes []string Produces []string Params []Param Responses []ResponseMeta Properties map[string]interface{} }
func (*Route) ContentType ¶ added in v1.22.0
ContentType of all available responses type
func (*Route) Parameters ¶
func (*Route) SetProperty ¶
type Router ¶
type Router struct { Notfound gin.HandlerFunc // contains filtered or unexported fields }
func (*Router) MustRegister ¶
func (m *Router) MustRegister(method, path string, handler gin.HandlerFunc)
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is a http.ServeMux like library,but support path variable
func NewServeMux ¶
func NewServeMux() *ServeMux
func (*ServeMux) HandlerFunc ¶
type SwaggerBuilder ¶
type SwaggerBuilder struct {
// contains filtered or unexported fields
}
Click to show internal directories.
Click to hide internal directories.