router.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "github.com/golang-jwt/jwt/v4"
  4. "github.com/kataras/iris/v12/mvc"
  5. "sparrow/pkg/generator"
  6. "sparrow/services/knowoapi/controllers"
  7. "sparrow/services/knowoapi/model"
  8. "sparrow/services/knowoapi/services"
  9. jwtmiddleware "github.com/iris-contrib/middleware/jwt"
  10. "github.com/kataras/iris/v12"
  11. )
  12. func registerErrors(srv *iris.Application) {
  13. srv.OnAnyErrorCode(handleErrors)
  14. }
  15. func handleErrors(ctx iris.Context) {
  16. //logger.Default().Serve(ctx)
  17. err := controllers.ErrorResponse{
  18. Code: ctx.GetStatusCode(),
  19. Message: ctx.Values().GetStringDefault("reason", "未知错误"),
  20. }
  21. ctx.JSON(err)
  22. }
  23. // jwt 中间件配置
  24. func newJWThandle() func(ctx iris.Context) {
  25. jwtHandler := jwtmiddleware.New(jwtmiddleware.Config{
  26. ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
  27. return []byte(model.SignedString), nil
  28. },
  29. ErrorHandler: func(ctx iris.Context, err error) {
  30. ctx.StatusCode(iris.StatusUnauthorized)
  31. ctx.Values().Set("reason", err)
  32. },
  33. SigningMethod: jwt.SigningMethodHS256,
  34. })
  35. return jwtHandler.Serve
  36. }
  37. // 请求路由
  38. func registerRouters(srv *iris.Application, models *model.All, gen *generator.KeyGenerator) {
  39. pService := services.NewProductService(models, gen)
  40. userService := services.NewUserService(models, gen)
  41. appService := services.NewAppService(models, gen)
  42. sensorService := services.NewSensorService(models)
  43. protocalService := services.NewProtocalService(models)
  44. alertService := services.NewAlertService(models)
  45. deviceService := services.NewDeviceService(models)
  46. roleService := services.NewRoleService(models)
  47. ruleChainService := services.NewRuleChainService(models)
  48. subDeviceService := services.NewSubDeviceService(models)
  49. fileService := services.NewFileService()
  50. otaService := services.NewOtaService(models)
  51. v1router := srv.Party("/api/v1")
  52. // 登陆,注册
  53. loginAPI := mvc.New(v1router.Party("/"))
  54. loginAPI.Register(userService).Handle(new(controllers.UserController))
  55. // 用户接口组
  56. userRouter := v1router.Party("/user", newJWThandle())
  57. // user api
  58. userAPI := mvc.New(userRouter.Party("/"))
  59. userAPI.Register(userService).Handle(new(controllers.UserController))
  60. // product api
  61. productAPI := mvc.New(userRouter.Party("/product"))
  62. productAPI.Register(pService).Handle(new(controllers.ProductController))
  63. //application api
  64. appAPI := mvc.New(userRouter.Party("/application"))
  65. appAPI.Register(appService).Handle(new(controllers.AppController))
  66. // sensor api
  67. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  68. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  69. // protocal api
  70. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  71. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  72. //alert api
  73. alertAPI := mvc.New(userRouter.Party("/alert"))
  74. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  75. //device api
  76. deviceAPI := mvc.New(userRouter.Party("/device"))
  77. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  78. //file api
  79. fileAPI := mvc.New(userRouter.Party("/file"))
  80. fileAPI.Register(fileService).Handle(new(controllers.FileController))
  81. //ota api
  82. otaApi := mvc.New(userRouter.Party("/ota"))
  83. otaApi.Register(otaService).Handle(new(controllers.OtaController))
  84. //管理员接口组
  85. adminRouter := v1router.Party("/admin", newJWThandle())
  86. RoleAPI := mvc.New(adminRouter.Party("/role"))
  87. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  88. // ruleChain api
  89. RuleChainAPI := mvc.New(userRouter.Party("/rule_chain"))
  90. RuleChainAPI.Register(ruleChainService).Handle(new(controllers.RuleChainController))
  91. // subDevice api
  92. subDeviceAPI := mvc.New(userRouter).Party("/sub_device")
  93. subDeviceAPI.Register(subDeviceService).Handle(new(controllers.SubDeviceController))
  94. }