Kratos has a series of built-in middleware to deal with common purpose such as logging or metrics. You could also implement Middleware interface to develop your custom middleware to process common business such as the user authentication etc.

Built-in Middleware

Their codes are located in middleware directory.

logging

In middleware/logging, this middleware is for logging the request.

metrics

In middleware/metrics, this middleware is for enabling metric.

recovery

In middleware/recovery,, this middleware is for panic recovery.

status

In middleware/status, this middleware is for transformation of gRPC error.

tracing

In middleware/tracing, this middleware is for enabling trace.

validate

In middleware/validate, this middleware is for parameter validation.

Usage

Register it with ServerOption in NewGRPCServer or NewHTTPServer.

For example:

  1. // http
  2. // define opts
  3. var opts = []http.ServerOption{
  4. http.Middleware(
  5. recovery.Recovery(),
  6. tracing.Server(),
  7. logging.Server(),
  8. ),
  9. }
  10. // create server
  11. http.NewServer(opts...)
  12. //grpc
  13. var opts = []grpc.ServerOption{
  14. grpc.Middleware(
  15. recovery.Recovery(),
  16. status.Server(),
  17. tracing.Server(),
  18. logging.Server(),
  19. ),
  20. }
  21. // create server
  22. grpc.NewServer(opts...)

Custom Middleware

Implement Middleware interface.

(TBD)