你可能永远不需要这个,但是以防万一。

    有时候你可能需要覆写或者决定一个请求进入时这个路由器是否执行。如果你在以前有许多 net/http 和其他 web 框架的经验,这个函数将会使你感到熟悉(它有 net/http 中间件的格式,但是不接收下一个处理器,它接收的是一个处理器,来作为是否执行的函数)。

    1. // WrapperFunc is used as an expected input parameter signature
    2. // for the WrapRouter. It's a "low-level" signature which is compatible
    3. // with the net/http.
    4. // It's being used to run or no run the router based on a custom logic.
    5. type WrapperFunc func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc)
    6. // WrapRouter adds a wrapper on the top of the main router.
    7. // Usually it's useful for third-party middleware
    8. // when need to wrap the entire application with a middleware like CORS.
    9. //
    10. // Developers can add more than one wrappers,
    11. // those wrappers' execution comes from last to first.
    12. // That means that the second wrapper will wrap the first, and so on.
    13. //
    14. // Before build.
    15. func WrapRouter(wrapperFunc WrapperFunc)

    路由器基于 SubdomainHTTP 方法 和他的动态路径来寻找它的路由。路由包装器可以重写这种行为,执行自定义的代码。

    在这个示例中,你将会看到 .WrapRouter 的一个用例。你可以使用 .WrapRouter 添加自定义逻辑,来决定路由器什么时候执行或者不执行,来达到控制是否执行注册的路由处理器的目的。这仅仅是为了证明概念,你可以跳过本篇教程。

    示例代码:

    1. package main
    2. import (
    3. "net/http"
    4. "strings"
    5. "github.com/kataras/iris/v12"
    6. )
    7. func newApp() *iris.Application {
    8. app := iris.New()
    9. app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
    10. ctx.HTML("<b>Resource Not found</b>")
    11. })
    12. app.Get("/profile/{username}", func(ctx iris.Context) {
    13. ctx.Writef("Hello %s", ctx.Params().Get("username"))
    14. })
    15. app.HandleDir("/", "./public")
    16. myOtherHandler := func(ctx iris.Context) {
    17. ctx.Writef("inside a handler which is fired manually by our custom router wrapper")
    18. }
    19. // wrap the router with a native net/http handler.
    20. // if url does not contain any "." (i.e: .css, .js...)
    21. // (depends on the app , you may need to add more file-server exceptions),
    22. // then the handler will execute the router that is responsible for the
    23. // registered routes (look "/" and "/profile/{username}")
    24. // if not then it will serve the files based on the root "/" path.
    25. app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
    26. path := r.URL.Path
    27. if strings.HasPrefix(path, "/other") {
    28. // acquire and release a context in order to use it to execute
    29. // our custom handler
    30. // remember: we use net/http.Handler because here
    31. // we are in the "low-level", before the router itself.
    32. ctx := app.ContextPool.Acquire(w, r)
    33. myOtherHandler(ctx)
    34. app.ContextPool.Release(ctx)
    35. return
    36. }
    37. // else continue serving routes as usual.
    38. router.ServeHTTP(w, r)
    39. })
    40. return app
    41. }
    42. func main() {
    43. app := newApp()
    44. // http://localhost:8080
    45. // http://localhost:8080/index.html
    46. // http://localhost:8080/app.js
    47. // http://localhost:8080/css/main.css
    48. // http://localhost:8080/profile/anyusername
    49. // http://localhost:8080/other/random
    50. app.Run(iris.Addr(":8080"))
    51. // Note: In this example we just saw one use case,
    52. // you may want to .WrapRouter or .Downgrade in order to
    53. // bypass the Iris' default router, i.e:
    54. // you can use that method to setup custom proxies too.
    55. }

    这里不需要多说,它仅是一个接受原生ResponseWriterRequest 以及路由器下一个处理器的函数包装器,它是全部路由器的一个中间件