当我们谈论 Iris 中的中间件时,我们谈论的是一个 HTTP 请求的生命周期中主处理器代码运行前/后运行的代码。例如,日志中间件可能记录一个传入请求的详情到日志中,然后调用处理器代码,然后再编写有关响应的详细信息到日志中。关于中间件的一件很酷的事情是,这些单元非常灵活且可重复使用。

中间件仅是一个 Handler 格式的函数 func(ctx iris.Context),当前一个中间件调用 ctx.Next() 方法时,此中间件被执行,这可以用作身份验证,即如果请求验证通过,就调用 ctx.Next() 来执行该请求剩下链上的处理器,否则触发一个错误响应。

编写一个中间件(Writing a middleware)

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.New()
  5. // or app.Use(before) and app.Done(after).
  6. app.Get("/", before, mainHandler, after)
  7. app.Run(iris.Addr(":8080"))
  8. }
  9. func before(ctx iris.Context) {
  10. shareInformation := "this is a sharable information between handlers"
  11. requestPath := ctx.Path()
  12. println("Before the mainHandler: " + requestPath)
  13. ctx.Values().Set("info", shareInformation)
  14. ctx.Next() // execute the next handler, in this case the main one.
  15. }
  16. func after(ctx iris.Context) {
  17. println("After the mainHandler")
  18. }
  19. func mainHandler(ctx iris.Context) {
  20. println("Inside mainHandler")
  21. // take the info from the "before" handler.
  22. info := ctx.Values().GetString("info")
  23. // write something to the client as a response.
  24. ctx.HTML("<h1>Response</h1>")
  25. ctx.HTML("<br/> Info: " + info)
  26. ctx.Next() // execute the "after".
  27. }
  1. $ go run main.go # and navigate to the http://localhost:8080
  2. Now listening on: http://localhost:8080
  3. Application started. Press CTRL+C to shut down.
  4. Before the mainHandler: /
  5. Inside mainHandler
  6. After the mainHandler

全局范围(Globally)

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.New()
  5. // register our routes.
  6. app.Get("/", indexHandler)
  7. app.Get("/contact", contactHandler)
  8. // Order of those calls does not matter,
  9. // `UseGlobal` and `DoneGlobal` are applied to existing routes
  10. // and future routes also.
  11. //
  12. // Remember: the `Use` and `Done` are applied to the current party's and its children,
  13. // so if we used the `app.Use/Done before the routes registration
  14. // it would work like UseGlobal/DoneGlobal in this case,
  15. // because the `app` is the root "Party".
  16. app.UseGlobal(before)
  17. app.DoneGlobal(after)
  18. app.Run(iris.Addr(":8080"))
  19. }
  20. func before(ctx iris.Context) {
  21. // [...]
  22. }
  23. func after(ctx iris.Context) {
  24. // [...]
  25. }
  26. func indexHandler(ctx iris.Context) {
  27. // write something to the client as a response.
  28. ctx.HTML("<h1>Index</h1>")
  29. ctx.Next() // execute the "after" handler registered via `Done`.
  30. }
  31. func contactHandler(ctx iris.Context) {
  32. // write something to the client as a response.
  33. ctx.HTML("<h1>Contact</h1>")
  34. ctx.Next() // execute the "after" handler registered via `Done`.
  35. }

你也可以使用 ExecutionRules 强制处理器在没有 ctx.Next() 的情况下完成执行。你可以这样做:

  1. app.SetExecutionRules(iris.ExecutionRules{
  2. // Begin: ...
  3. // Main: ...
  4. Done: iris.ExecutionOptions{Force: true},
  5. })

转化 http.Handler/HandlerFunc(Convert http.Handler/HandlerFunc)

使用它们是没有限制的 - 你很自由地使用任何与 net/http 包兼容的第三方的中间件。

Iris与其他框架不同,它是 100% 与标准库兼容,这就是为什么多数大型公司信任 Iris,使用 Go 来完成它们的工作流程,如著名的 US Television Network;它是最新的,并且始终与标准库 net/http 包保持一致,标准库 net/http 在每次 Go 版本更新的时候都会进行优化。

任意用 net/http 编写的第三方中间件通过 iris.FromStd(AThirdPartyMiddleware) 与 Iris 兼容。记住, ctx.ResponseWriter()ctx.Request() 返回与 net/httphttp.Handler 相同的输入参数。

这里是一系列为 Iris 特定功能创建的 handlers:

内建的

  • basic authentication
  • Google reCAPTCHA
  • localization and internationalization
  • request logger
  • profiling (pprof)
  • recovery

社区创建的

请查看文档。