add RouterMiddlewares to iris guide

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-10-19 01:39:32 +03:00
parent 1efbcabfce
commit 45aa45237c
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503
2 changed files with 14 additions and 3 deletions

View File

@ -247,6 +247,8 @@ type (
}
Step5 interface {
// RouterMiddlewares registers one or more handlers to run before everything else.
RouterMiddlewares(handlers ...Handler) Step5
// Middlewares registers one or more handlers to run before the requested route's handler.
Middlewares(handlers ...Handler) Step6
}
@ -338,9 +340,15 @@ func (s *step4) Timeout(requestResponseLife, read, write time.Duration) Step5 {
type step5 struct {
step4 *step4
routerMiddlewares []Handler // top-level router middlewares, fire even on 404s.
middlewares []Handler
}
func (s *step5) RouterMiddlewares(handlers ...Handler) Step5 {
s.routerMiddlewares = append(s.routerMiddlewares, handlers...)
return s
}
func (s *step5) Middlewares(handlers ...Handler) Step6 {
s.middlewares = handlers
@ -426,6 +434,10 @@ func (s *step7) Build() *Application {
app.UseRouter(recover.New())
for _, routerLevelMiddleware := range s.step6.step5.routerMiddlewares {
app.UseRouter(routerLevelMiddleware)
}
app.UseRouter(func(ctx Context) {
ctx.Header("Server", "Iris")
if dev := s.step6.step5.step4.step3.developer; dev != "" {

View File

@ -11,7 +11,7 @@ import (
// It accepts the controller ptr to a struct value,
// the gRPCServer itself, and a strict option which is explained below.
//
// The differences by a common controller are:
// The differences between an GRPC-based controller and a common one are:
// HTTP verb: only POST (Party.AllowMethods can be used for more),
// method parsing is disabled: path is the function name as it is,
// if 'strictMode' option is true then this controller will only serve gRPC-based clients
@ -71,6 +71,5 @@ func (g GRPC) Apply(c *ControllerActivator) {
}
route.Description += " " + bckp // e.g. "gRPC controller"
}
}
}