You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Middlewares can be added to a router using `Router.AddMiddlewareFunc()`:
478
+
Middlewares can be added to a router using `Router.Use()`:
479
479
480
480
```go
481
481
r:= mux.NewRouter()
482
482
r.HandleFunc("/", handler)
483
-
r.AddMiddleware(simpleMw)
483
+
r.Use(loggingMiddleware)
484
484
```
485
485
486
486
A more complex authentication middleware, which maps session token to users, could be written as:
@@ -524,7 +524,7 @@ r.HandleFunc("/", handler)
524
524
amw:= authenticationMiddleware{}
525
525
amw.Populate()
526
526
527
-
r.AddMiddlewareFunc(amw.Middleware)
527
+
r.Use(amw.Middleware)
528
528
```
529
529
530
530
Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to. Middlewares *should* write to `ResponseWriter` if they *are* going to terminate the request, and they *should not* write to `ResponseWriter` if they *are not* going to terminate it.
0 commit comments