router.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. sceneHisService := services.NewSceneHisService(models)
  52. deviceCommandService := services.NewDeviceCommandService(models)
  53. deviceStatusService := services.NewDeviceStatusService(models)
  54. v1router := srv.Party("/api/v1")
  55. // 登陆,注册
  56. loginAPI := mvc.New(v1router.Party("/"))
  57. loginAPI.Register(userService).Handle(new(controllers.UserController))
  58. // 用户接口组
  59. userRouter := v1router.Party("/user", newJWThandle())
  60. // user api
  61. userAPI := mvc.New(userRouter.Party("/"))
  62. userAPI.Register(userService).Handle(new(controllers.UserController))
  63. // product api
  64. productAPI := mvc.New(userRouter.Party("/product"))
  65. productAPI.Register(pService).Handle(new(controllers.ProductController))
  66. //application api
  67. appAPI := mvc.New(userRouter.Party("/application"))
  68. appAPI.Register(appService).Handle(new(controllers.AppController))
  69. // sensor api
  70. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  71. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  72. // protocal api
  73. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  74. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  75. //alert api
  76. alertAPI := mvc.New(userRouter.Party("/alert"))
  77. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  78. //device api
  79. deviceAPI := mvc.New(userRouter.Party("/device"))
  80. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  81. //file api
  82. fileAPI := mvc.New(userRouter.Party("/file"))
  83. fileAPI.Register(fileService).Handle(new(controllers.FileController))
  84. //ota api
  85. otaApi := mvc.New(userRouter.Party("/ota"))
  86. otaApi.Register(otaService).Handle(new(controllers.OtaController))
  87. //管理员接口组
  88. adminRouter := v1router.Party("/admin", newJWThandle())
  89. RoleAPI := mvc.New(adminRouter.Party("/role"))
  90. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  91. // ruleChain api
  92. RuleChainAPI := mvc.New(userRouter.Party("/rule_chain"))
  93. RuleChainAPI.Register(ruleChainService).Handle(new(controllers.RuleChainController))
  94. // subDevice api
  95. subDeviceAPI := mvc.New(userRouter).Party("/sub_device")
  96. subDeviceAPI.Register(subDeviceService).Handle(new(controllers.SubDeviceController))
  97. sceneHisAPI := mvc.New(userRouter).Party("/scene_his")
  98. sceneHisAPI.Register(sceneHisService).Handle(new(controllers.SceneHisController))
  99. // scene-service 内部调用接口(无需JWT认证)
  100. sceneHistoryAPI := mvc.New(srv.Party("/iot/v1")).Party("/scene_history")
  101. sceneHistoryAPI.Register(sceneHisService).Handle(new(controllers.SceneHistoryHandler))
  102. // deviceCommand api
  103. deviceCommandAPI := mvc.New(userRouter.Party("/device_command"))
  104. deviceCommandAPI.Register(deviceCommandService).Handle(new(controllers.DeviceCommandController))
  105. // deviceStatus api
  106. deviceStatusAPI := mvc.New(userRouter.Party("/device_status"))
  107. deviceStatusAPI.Register(deviceStatusService).Handle(new(controllers.DeviceStatusController))
  108. }