理解iris框架的路由底层结构

发布时间 2023-09-04 15:47:13作者: lisus2000

1.iris路由实现原理

首先,我们看iris.New函数的作用。该函数就是创建了一个Application结构体的实例 app。然后后面的操作都是基于该实例 app 进行的操作。下面是该Application结构体的主要字段,

 在Application的字段中,从名字上看有两个字段是和路由相关的:router.APIBuilder和 router.Router。那我们接着再分别看下这两个结构体的主要构成。

 源码位置:api_builder.go和route.go

router.APIBuilder结构体及其相关的reporitoryRoute结构体可以看到,这里包含了路由的相关信息。例如,在repository中的routes字段,代表所有的路由,可以简单理解为一个建议的路由表。在Route结构体中包含了请求方法Method字段、请求路径字段Path、对应的请求处理函数Handlers字段。其中还有macro.Template类型的Tmp字段是针对正则路由的正则表达式,源码如下

// Route contains the information about a registered Route.
// If any of the following fields are changed then the
// caller should Refresh the router.
type Route struct {
// The Party which this Route was created and registered on.
Party Party
Title string `json:"title"` // custom name to replace the method on debug logging.
Name string `json:"name"` // "userRoute"
Description string `json:"description"` // "lists a user"
Method string `json:"method"` // "GET"
StatusCode int `json:"statusCode"` // 404 (only for HTTP error handlers).
methodBckp string // if Method changed to something else (which is possible at runtime as well, via RefreshRouter) then this field will be filled with the old one.
Subdomain string `json:"subdomain"` // "admin."
tmpl macro.Template // Tmpl().Src: "/api/user/{id:uint64}"
// temp storage, they're appended to the Handlers on build.
// Execution happens before Handlers, can be empty.
// They run right after any builtinBeginHandlers.
beginHandlers context.Handlers
// temp storage, these are always registered first as Handlers on Build.
// There are the handlers may be added by the framework and
// can NOT be modified by the end-developer (i.e overlapRoute & bindMultiParamTypesHandler),
// even if a function like UseGlobal is used.
builtinBeginHandlers context.Handlers

// Handlers are the main route's handlers, executed by order.
// Cannot be empty.
Handlers context.Handlers `json:"-"`
MainHandlerName string `json:"mainHandlerName"`
MainHandlerIndex int `json:"mainHandlerIndex"`
// temp storage, they're appended to the Handlers on build.
// Execution happens after Begin and main Handler(s), can be empty.
doneHandlers context.Handlers

Path string `json:"path"` // the underline router's representation, i.e "/api/user/:id"
// FormattedPath all dynamic named parameters (if any) replaced with %v,
// used by Application to validate param values of a Route based on its name.
FormattedPath string `json:"formattedPath"`

// the source code's filename:filenumber that this route was created from.
SourceFileName string `json:"sourceFileName"`
SourceLineNumber int `json:"sourceLineNumber"`

// where the route registered.
RegisterFileName string `json:"registerFileName"`
RegisterLineNumber int `json:"registerLineNumber"`

// see APIBuilder.handle, routerHandler.bindMultiParamTypesHandler and routerHandler.Build,
// it's the parent route of the last registered of the same path parameter. Specifically for path parameters.
topLink *Route
// overlappedLink specifically for overlapRoute feature.
// keeps the second route of the same path pattern registered.
// It's used ONLY for logging.
overlappedLink *Route

// Sitemap properties: https://www.sitemaps.org/protocol.html
NoSitemap bool // when this route should be hidden from sitemap.
LastMod time.Time `json:"lastMod,omitempty"`
ChangeFreq string `json:"changeFreq,omitempty"`
Priority float32 `json:"priority,omitempty"`

// ReadOnly is the read-only structure of the Route.
ReadOnly context.RouteReadOnly

// OnBuild runs right before BuildHandlers.
OnBuild func(r *Route)
NoLog bool // disables debug logging.
}