Iris 具有你遇到过的最简单,最强大的路由流程。
Iris 对于路由的路径语法,解析和评估有自己的交互器(是的,就像编程语言一样!)。
它很快, 为什么? 它计算它的需求,如果没有任何特殊的正则,那么它只需用低级路径语法注册路由,否则它预先编译正则并添加必要的中间件。这意味着与其他路由器或 Web 框架相比,性能成本为零。
# 参数
路径参数的名称应只能包含字母。不允许使用数字或符号,例如 "_"。
不要将 `ctx.Params()` 和 `ctx.Values()` 混淆。
* 路径参数的值可以从 `ctx.Params()` 中检索。
* `Context` 的本地存储可以用于在处理程序和中间件之间通信,它可以储存到 `ctx.Values()`中。
内置的可用参数类型可以在下表中找到。
| 参数类型 | Go 类型 | 验证 | 获取的辅助方法 |
| -------- | -------- | -------- |-------- |
| `:string ` | string |任何内容(单路径) | `Params().Get` |
| `:int` | int | -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch | `Params().GetInt `|
| `:int8` | int8 | -128 to 127| `Params().GetInt8`|
| `:int16`| int16 | -32768 to 32767| `Params().GetInt16`|
| `:int32` | int32| -2147483648 to 2147483647| `Params().GetInt32`|
|` :int64` | int64| -9223372036854775808 to 9223372036854775807 | `Params().GetInt64`|
| `:uint` | uint | 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), depends on the host arch |` Params().GetUint`|
| `:uint8` | uint8 | 0 to 255| `Params().GetUint8`|
|` :uint16`| uint16 | 0 to 65535 |` Params().GetUint16`|
| `:uint32`| uint32 | 0 to 4294967295| `Params().GetUint32`|
|` :uint64`| uint64 | 0 to 18446744073709551615 | `Params().GetUint64`|
| `:bool` | bool | "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False" |` Params().GetBool`|
| `:alphabetical` | string| 小写或大写字母| `Params().Get`|
| `:file` | string | 小写或大写字母、数字、下划线 (*)、破折号 (-)、点 (.) 和空格或其他对文件名无效的特殊字符 |` Params().Get`|
| `:path `| string | 任何内容,都可以由斜杠(路径段)分隔,但应该是路径的最后一部分 | `Params().Get`|
使用方法:
```
app.Get("/users/{id:uint64}", func(ctx iris.Context){
id := ctx.Params().GetUint64Default("id", 0)
// [...]
})
```
|内置方法| 参数类型|
| -------- | -------- |
|regexp(expr string)| `:string`|
|prefix(prefix string) |`:string`|
|suffix(suffix string)|` :string`|
|contains(s string)| `:string`|
|min(minValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64)| `:string(char length)`,` :int`, `:int8`,` :int16`, `:int32`, `:int64`,` :uint`, `:uint8`, `:uint16`, `:uint32`, `:uint64`|
|max(maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) |`:string(char length)`, `:int`, `:int8`,` :int16`,` :int32`,` :int64`, `:uint`,` :uint8`, `:uint16`, `:uint32`, `:uint64`|
|range(minValue, maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) |`:int`, `:int8`, `:int16`, `:int32`, `:int64`, `:uint`, `:uint8`, `:uint16`, `:uint32`, `:uint64`|
使用方法:
```
app.Get("/profile/{name:alphabetical max(255)}", func(ctx iris.Context){
name := ctx.Params().Get("name")
// len(name) <=255 否则这个路由将触发 404 Not Found
// ,此处理程序将不会执行。
})
```
# 自定义:
`RegisterFunc` 可以接受任何返回 `func(paramValue string) bool` 的函数。或者只是一个 `func(string) bool`。如果验证失败,则它将触发 `404` 或关键字具有的任何状态代码。
```
latLonExpr := "^-?[0-9]{1,3}(?:\\.[0-9]{1,10})?$"
latLonRegex, _ := regexp.Compile(latLonExpr)
//将自定义的无参数宏函数注册为:string参数类型。
// MatchString 是 func(string) bool 类型的之一,因此我们按原样使用它。
app.Macros().Get("string").RegisterFunc("coordinate", latLonRegex.MatchString)
app.Get("/coordinates/{lat:string coordinate()}/{lon:string coordinate()}",
func(ctx iris.Context) {
ctx.Writef("Lat: %s | Lon: %s", ctx.Params().Get("lat"), ctx.Params().Get("lon"))
})
```
注册一个你自定义的接受两个 int 参数的自定义宏函数。
```
app.Macros().Get("string").RegisterFunc("range",
func(minLength, maxLength int) func(string) bool {
return func(paramValue string) bool {
return len(paramValue) >= minLength && len(paramValue) <= maxLength
}
})
app.Get("/limitchar/{name:string range(1,200) else 400}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef(`Hello %s | the name should be between 1 and 200 characters length
otherwise this handler will not be executed`, name)
})
```
注册一个你自定义的宏函数,该函数接受字符串的片段` [...,...]`.
```
app.Macros().Get("string").RegisterFunc("has",
func(validNames []string) func(string) bool {
return func(paramValue string) bool {
for _, validName := range validNames {
if validName == paramValue {
return true
}
}
return false
}
})
app.Get("/static_validation/{name:string has([kataras,maropoulos])}",
func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef(`Hello %s | the name should be "kataras" or "maropoulos"
otherwise this handler will not be executed`, name)
})
```
# 示例代码:
```
func main() {
app := iris.Default()
// 处理程序匹配 /user/john 但是不匹配 /user/ 或者 /user。
app.Get("/user/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Hello %s", name)
})
// 处理程序匹配 /users/42
//但是不匹配 /users/-1 因为 uint 应该大于 0
// /users 或者 /users/ 都不匹配。
app.Get("/users/{id:uint64}", func(ctx iris.Context) {
id := ctx.Params().GetUint64Default("id", 0)
ctx.Writef("User with ID: %d", id)
})
// 但是,这个处理程序将匹配 /user/john/send以及 /user/john/everything/else/here 。
// 却不匹配 /user/john 也不匹配 /user/john/。
app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
action := ctx.Params().Get("action")
message := name + " is " + action
ctx.WriteString(message)
})
app.Run(iris.Addr(":8080"))
}
```
当缺失参数类型时,则默认它为一个 `string` 类型,因此 `{name:string}` 等同于 `{name}`。