Using middleware

  1. package main
  2. import (
  3. "github.com/kataras/iris/v12"
  4. "github.com/kataras/iris/v12/middleware/recover"
  5. )
  6. func main() {
  7. // Creates an iris application without any middleware by default
  8. app := iris.New()
  9. // Global middleware using `UseRouter`.
  10. //
  11. // Recovery middleware recovers from any panics and writes a 500 if there was one.
  12. app.UseRouter(recover.New())
  13. // Per route middleware, you can add as many as you desire.
  14. app.Get("/benchmark", MyBenchLogger(), benchEndpoint)
  15. // Authorization group
  16. // authorized := app.Party("/", AuthRequired())
  17. // exactly the same as:
  18. authorized := app.Party("/")
  19. // per group middleware! in this case we use the custom created
  20. // AuthRequired() middleware just in the "authorized" group.
  21. authorized.Use(AuthRequired())
  22. {
  23. authorized.Post("/login", loginEndpoint)
  24. authorized.Post("/submit", submitEndpoint)
  25. authorized.Post("/read", readEndpoint)
  26. // nested group
  27. testing := authorized.Party("testing")
  28. testing.Get("/analytics", analyticsEndpoint)
  29. }
  30. // Listen and serve on 0.0.0.0:8080
  31. app.Listen(":8080")
  32. }