在这个小节你将会学习如果重写已存在的 Context 的方法。

    Context 是一个接口。然而你可能了解,当使用其它框架的时,即使它是一个接口,你也无法重写它。Iris 使用 app.ContextPool.Attach 方法连接你的实现到 context pool 中。

    1. 首先我们导入 github.com/kataras/iris/v12/context,这里需要它。
    2. 其次编写你自己的实现
    3. 然后添加 DoNext 两个包级别函数
    4. 使用 ApplicationContextPool 将其设置为应用于路由处理程序的Context实现。

    示例代码:

    请读注释:

    1. package main
    2. import (
    3. "reflect"
    4. "github.com/kataras/iris/v12"
    5. // 1.
    6. "github.com/kataras/iris/v12/context"
    7. )
    8. // 2.
    9. // Create your own custom Context, put any fields you'll need.
    10. type MyContext struct {
    11. // Embed the `iris.Context` - 嵌入 iris.Context
    12. // It's totally optional but you will need this if you
    13. // don't want to override all the context's methods!
    14. // 这是可选的,但是你不想重写所有的 context的方法,需要这么做。
    15. iris.Context
    16. }
    17. // Optionally: validate MyContext implements iris.Context on compile-time.
    18. // 可选的,在编译的时候验证是否实现 iris.Context
    19. var _ iris.Context = &MyContext{}
    20. // 3.
    21. func (ctx *MyContext) Do(handlers context.Handlers) {
    22. context.Do(ctx, handlers)
    23. }
    1. // 3.
    2. func (ctx *MyContext) Next() {
    3. context.Next(ctx)
    4. }
    5. // [Override any context's method you want here...]
    6. // Like the HTML below:
    7. func (ctx *MyContext) HTML(htmlContents string) (int, error) {
    8. ctx.Application().Logger().Infof("Executing .HTML function from MyContext")
    9. ctx.ContentType("text/html")
    10. return ctx.WriteString(htmlContents)
    11. }
    12. func main() {
    13. app := iris.New()
    14. // 4.
    15. app.ContextPool.Attach(func() iris.Context {
    16. return &MyContext{
    17. // If you use the embedded Context,
    18. // call the `context.NewContext` to create one:
    19. Context: context.NewContext(app),
    20. }
    21. })
    22. // Register a view engine on .html files inside the ./view/** directory.
    23. app.RegisterView(iris.HTML("./view", ".html"))
    24. // Register your route, as you normally do
    25. app.Handle("GET", "/", recordWhichContextForExample,
    26. func(ctx iris.Context) {
    27. // use the context's overridden HTML method.
    28. ctx.HTML("<h1> Hello from my custom context's HTML! </h1>")
    29. })
    30. // This will be executed by the
    31. // MyContext.Context embedded default context
    32. // when MyContext is not directly define the View function by itself.
    33. app.Handle("GET", "/hi/{firstname:alphabetical}",recordWhichContextForExample,
    34. func(ctx iris.Context) {
    35. firstname := ctx.Values().GetString("firstname")
    36. ctx.ViewData("firstname", firstname)
    37. ctx.Gzip(true)
    38. ctx.View("hi.html")
    39. })
    40. app.Run(iris.Addr(":8080"))
    41. }
    42. // Should always print "($PATH) Handler is executing from 'MyContext'"
    43. func recordWhichContextForExample(ctx iris.Context) {
    44. ctx.Application().Logger().Infof("(%s) Handler is executing from: '%s'",
    45. ctx.Path(), reflect.TypeOf(ctx).Elem().Name())
    46. ctx.Next()
    47. }