合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 中间件 当我们讨论 Iris 的中间件的时候,我们往往讨论的是在一个 HTTP 请求生命周期中的代码运行前和或者我们的主程序处理代码之后的事情。例如,记录中间件可能会将传入的请求详细信息写入日志,然后调用处理程序代码,然后再编写有关对日志的响应的详细信息。 对中间件来说最酷的事情是,这些单元非常灵活且可重复使用。 中间件只是`func(ctx iris.Context)`的处理程序形式之一,当上一个中间件调用`ctx.Next()` 时,当前中间件就会执行。 身份验证,即:如果请求已通过身份验证,则调用`ctx.Next()`处理请求中的其余处理程序链,否则将引发错误响应。 # 编写一个中间件 ``` package main import "github.com/kataras/iris/v12" func main() { app := iris.New() // 或者 app.Use(before) 和 app.Done(after). app.Get("/", before, mainHandler, after) app.Run(iris.Addr(":8080")) } func before(ctx iris.Context) { shareInformation := "this is a sharable information between handlers" requestPath := ctx.Path() println("Before the mainHandler: " + requestPath) ctx.Values().Set("info", shareInformation) ctx.Next() //执行下一个处理程序,在本例中为主要处理程序。 } func after(ctx iris.Context) { println("After the mainHandler") } func mainHandler(ctx iris.Context) { println("Inside mainHandler") // 从 "before" 处理程序中获取信息。 info := ctx.Values().GetString("info") // 向客户端写一些内容作为响应。 ctx.HTML("<h1>Response</h1>") ctx.HTML("<br/> Info: " + info) ctx.Next() // 执行 "after" 中间件 } ``` ``` $ go run main.go # 并且定位到 http://localhost:8080 现在监听: http://localhost:8080 应用程序启动, 按 CTRL+C 关闭。 mainHandler 之前: / mainHandler 内 mainHandler 之后 ``` # 全局的 ``` package main import "github.com/kataras/iris/v12" func main() { app := iris.New() // 注册路由 app.Get("/", indexHandler) app.Get("/contact", contactHandler) //这些调用的顺序无关紧要, // `UseGlobal` 和 `DoneGlobal` 应用于现有路由 // 以及将来的路由。 // // 记住: `Use` 和 `Done` 都应用于当前路由分组和它的子分组, // 所以如果我们在路由注册前使用 `app.Use/Done, // 那么就和在这个实例中使用 UseGlobal/DoneGlobal 一样, // 因为 `app`是根 "分组"。 app.UseGlobal(before) app.DoneGlobal(after) app.Run(iris.Addr(":8080")) } func before(ctx iris.Context) { // [...] } func after(ctx iris.Context) { // [...] } func indexHandler(ctx iris.Context) {  //向客户端写一些内容作为响应。 ctx.HTML("<h1>Index</h1>") ctx.Next() // 通过 `Done` 执行 "after" 处理程序的注册 } func contactHandler(ctx iris.Context) {    //向客户端写一些内容作为响应。 ctx.HTML("<h1>Contact</h1>") ctx.Next() // 通过 `Done` 执行 "after" 处理程序的注册 } ``` 你还可以使用 `ExecutionRules` 去强制执行完成的处理程序,而不需要使用`ctx.Next()` ,如下所示: ``` app.SetExecutionRules(iris.ExecutionRules{ // Begin: ... // Main: ... Done: iris.ExecutionOptions{Force: true}, }) ``` # 转换 `http.Handler/HandlerFunc` 然而,你不用局限于此。 - 你以自由使用与 [net/http](https://golang.org/pkg/net/http/) 包兼容的任何第三方中间件。 Iris, 与其他框架不同, 它 100% 兼容标准 , 这就是为什么大多数采用 Go 来适应它们工作流程的大公司(例如非常著名的美国电视网络)都信任 Iris; 它是最新的,并且将始终与标准 `net/http` 包保持一致,该软件包由 Go 作者在 Go 编程语言的每个新发行版上进行了现代化。 使用 `iris.FromStd(aThirdPartyMiddleware)` 为 `net/http` 编写的任何第三方中间件均与Iris 兼容。请记住,`ctx.ResponseWriter()` 和 `ctx.Request()` 返回[http.Handler](https://golang.org/pkg/net/http/#Handler) 的 `net/http` 一个有输入参数。 * [From func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)](https://github.com/kataras/iris/wiki/convert-handlers/negroni-like/main.go) * [From http.Handler or http.HandlerFunc](https://github.com/kataras/iris/wiki/convert-handlers/nethttp/main.go) * [From func(http.HandlerFunc) http.HandlerFunc](https://github.com/kataras/iris/wiki/convert-handlers/real-usecase-raven/writing-middleware/main.go) ----- 以下是专门为 Iris 制作的一些处理程序的列表: # 内置的 | 中间件 | 例子| | -------- | -------- | |[基本身份验证](https://github.com/kataras/iris/tree/master/middleware/basicauth) | [iris/_examples/authentication/basicauth](https://github.com/kataras/iris/tree/master/_examples/authentication/basicauth)| |[ 请求记录器](https://github.com/kataras/iris/tree/master/middleware/logger)| [iris/_examples/http_request/request-logger](https://github.com/kataras/iris/tree/master/_examples/http_request/request-logger)| | [HTTP方法覆盖](https://github.com/kataras/iris/tree/master/middleware/methodoverride)| [iris/middleware/methodoverride/methodoverride_test.go](https://github.com/kataras/iris/blob/master/middleware/methodoverride/methodoverride_test.go)| | [分析 (pprof)](https://github.com/kataras/iris/tree/master/middleware/pprof)| [iris/_examples/miscellaneous/pprof](https://github.com/kataras/iris/tree/master/_examples/miscellaneous/pprof)| | [Google reCAPTCHA](https://github.com/kataras/iris/tree/master/middleware/recaptcha) |[ iris/_examples/miscellaneous/recaptcha](https://github.com/kataras/iris/tree/master/_examples/miscellaneous/recaptcha)| | [恢复](https://github.com/kataras/iris/tree/master/middleware/recover)| [iris/_examples/miscellaneous/recover](https://github.com/kataras/iris/tree/master/_examples/miscellaneous/recover)| # 社区制造 大部分实验处理程序都从第三方来源移植为与 iris 的处理程序形式一起使用。 | 中间件 | 描述 |例子 | | -------- | -------- | -------- | | [jwt](https://github.com/iris-contrib/middleware/tree/master/jwt) | 中间件 检查传入请求的 Authorization 标头上的 JWT 并将其解码。 | [iris-contrib/middleware/jwt/_example](https://github.com/iris-contrib/middleware/tree/master/jwt/_example)| | [cors](https://github.com/iris-contrib/middleware/tree/master/cors) | HTTP访问控制。| [iris-contrib/middleware/cors/_example](https://github.com/iris-contrib/middleware/tree/master/cors)| | [secure](https://github.com/iris-contrib/middleware/tree/master/secure) | 实施一些快速安全性的中间件 | [iris- contrib/middleware/secure/_example](https://github.com/iris-contrib/middleware/tree/master/secure)| | [tollbooth](https://github.com/iris-contrib/middleware/tree/master/tollboothic) | 对 HTTP 请求进行速率限制的通用中间件。 |[ iris-contrib/middleware/tollbooth/_examples/limit-handler](https://github.com/iris-contrib/middleware/tree/master/tollboothic)| | [cloudwatch](https://github.com/iris-contrib/middleware/tree/master/cloudwatch) | AWS cloudwatch metrics middleware.| [iris-contrib/middleware/cloudwatch/_example](https://github.com/iris-contrib/middleware/tree/master/cloudwatch)| | [new relic](https://github.com/iris-contrib/middleware/tree/master/newrelic) | Official New Relic Go Agent. | [iris-contrib/middleware/newrelic/_example](https://github.com/iris-contrib/middleware/tree/master/newrelic)| | [prometheus](https://github.com/iris-contrib/middleware/tree/master/prometheus) |轻松创建用于专业检测工具的指标终结点| [iris-contrib/middleware/prometheus/_example](https://github.com/iris-contrib/middleware/tree/master/prometheus)| | [casbin](http://) | 授权库,支持访问控制模型,如ACL,RBAC,ABAC | [iris-contrib/middleware/casbin/_examples](https://github.com/iris-contrib/middleware/tree/master/casbin)| | [raven](https://github.com/iris-contrib/middleware/tree/master/casbin) | Sentry client in Go| [iris-contrib/middleware/raven/_example](https://github.com/iris-contrib/middleware/blob/master/raven/_example/main.go)| | [csrf](https://github.com/iris-contrib/middleware/tree/master/csrf) | 跨站请求伪造保护| i[ris-contrib/middleware/csrf/_example](https://github.com/iris-contrib/middleware/tree/master/csrf)|