Middleware

A middleware is pre/post processor of a request.

Implement the middleware

  1. Implements the Middleware interface.
  2. (Optional) Implements the Validator interface to validate the middleware.
  3. It adds to the AppConfig.Middlewares in config/app.go.

Middleware interface definition is following:

type Middleware interface {
    Process(app *Application, c *Context, next func() error) error
}

Before method will be executed before processing of Controller, and After method will be executed after processing of Controller.

Built-in middlewares

Kocha provides some middlewares.

PanicRecoverMiddleware (godoc)

PanicRecoverMiddleware recovers a panic where occurred in request sequence.
To recover the all panics, PanicRecoverMiddleware to be set to first of middlewares.

FormMiddleware (godoc)

FormMiddleware parses form data from query string and/or request body. (both application/x-www-form-urlencoded and multipart/form-data)

SessionMiddleware (godoc)

SessionMiddleware will autosave and autoload a session on around a request processing.

FlashMiddleware (godoc)

FlashMiddleware will provided one-time messaging between the requests (aka flash messages).
This middleware depends on SessionMiddleware.

RequestLoggingMiddleware (godoc)

RequestLoggingMiddleware will output an access log.

DispatchMiddleware (godoc)

DispatchMiddleware dispatches a handler from request URL.
Also DispatchMiddleware should be set to last of middlewares because doesn't call other middlewares after DispatchMiddleware.