router.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "sparrow/pkg/generator"
  4. "sparrow/services/knowoapi/controllers"
  5. "sparrow/services/knowoapi/model"
  6. "sparrow/services/knowoapi/services"
  7. "github.com/opentracing/opentracing-go"
  8. jwt "github.com/dgrijalva/jwt-go"
  9. jwtmiddleware "github.com/iris-contrib/middleware/jwt"
  10. "github.com/kataras/iris"
  11. "github.com/kataras/iris/mvc"
  12. )
  13. func registerErrors(srv *iris.Application) {
  14. srv.OnAnyErrorCode(handleErrors)
  15. }
  16. func handleErrors(ctx iris.Context) {
  17. //logger.Default().Serve(ctx)
  18. err := controllers.ErrorResponse{
  19. Code: ctx.GetStatusCode(),
  20. Message: ctx.Values().GetStringDefault("reason", "未知错误"),
  21. }
  22. ctx.JSON(err)
  23. }
  24. // jwt 中间件配置
  25. func newJWThandle() func(ctx iris.Context) {
  26. jwtHandler := jwtmiddleware.New(jwtmiddleware.Config{
  27. ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
  28. return []byte(model.SignedString), nil
  29. },
  30. ErrorHandler: func(ctx iris.Context, message string) {
  31. ctx.StatusCode(iris.StatusUnauthorized)
  32. ctx.Values().Set("reason", message)
  33. },
  34. SigningMethod: jwt.SigningMethodHS256,
  35. })
  36. return jwtHandler.Serve
  37. }
  38. // 请求路由
  39. func registerRouters(srv *iris.Application, models *model.All, gen *generator.KeyGenerator) {
  40. pService := services.NewProductService(models, gen)
  41. userService := services.NewUserService(models, gen)
  42. appService := services.NewAppService(models, gen)
  43. protocalService := services.NewProtocalService(models)
  44. alertService := services.NewAlertService(models)
  45. deviceService := services.NewDeviceService(models)
  46. roleService := services.NewRoleService(models)
  47. v1router := srv.Party("/api/v1", func(ctx iris.Context) {
  48. span := opentracing.StartSpan(ctx.Path())
  49. defer span.Finish()
  50. span.SetTag("http.method", ctx.Method())
  51. span.SetTag("http.url", ctx.Path())
  52. span.SetTag("http.status_code", ctx.GetStatusCode())
  53. ctx.Values().Set("span", span)
  54. ctx.Next()
  55. })
  56. // 登陆,注册
  57. loginAPI := mvc.New(v1router.Party("/"))
  58. loginAPI.Register(userService).Handle(new(controllers.UserController))
  59. // 用户接口组
  60. userRouter := v1router.Party("/user", newJWThandle())
  61. // user api
  62. userAPI := mvc.New(userRouter.Party("/"))
  63. userAPI.Register(userService).Handle(new(controllers.UserController))
  64. // product api
  65. productAPI := mvc.New(userRouter.Party("/product"))
  66. productAPI.Register(pService).Handle(new(controllers.ProductController))
  67. //application api
  68. appAPI := mvc.New(userRouter.Party("/application"))
  69. appAPI.Register(appService).Handle(new(controllers.AppController))
  70. // protocal api
  71. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  72. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  73. //alert api
  74. alertAPI := mvc.New(userRouter.Party("/alert"))
  75. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  76. //device api
  77. deviceAPI := mvc.New(userRouter.Party("/device"))
  78. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  79. //管理员接口组
  80. adminRouter := v1router.Party("/admin", newJWThandle())
  81. RoleAPI := mvc.New(adminRouter.Party("/role"))
  82. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  83. }