router.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. v1router := srv.Party("/api/v1")
  53. // 登陆,注册
  54. loginAPI := mvc.New(v1router.Party("/"))
  55. loginAPI.Register(userService).Handle(new(controllers.UserController))
  56. // 用户接口组
  57. userRouter := v1router.Party("/user", newJWThandle())
  58. // user api
  59. userAPI := mvc.New(userRouter.Party("/"))
  60. userAPI.Register(userService).Handle(new(controllers.UserController))
  61. // product api
  62. productAPI := mvc.New(userRouter.Party("/product"))
  63. productAPI.Register(pService).Handle(new(controllers.ProductController))
  64. //application api
  65. appAPI := mvc.New(userRouter.Party("/application"))
  66. appAPI.Register(appService).Handle(new(controllers.AppController))
  67. // sensor api
  68. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  69. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  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. //file api
  80. fileAPI := mvc.New(userRouter.Party("/file"))
  81. fileAPI.Register(fileService).Handle(new(controllers.FileController))
  82. //ota api
  83. otaApi := mvc.New(userRouter.Party("/ota"))
  84. otaApi.Register(otaService).Handle(new(controllers.OtaController))
  85. //管理员接口组
  86. adminRouter := v1router.Party("/admin", newJWThandle())
  87. RoleAPI := mvc.New(adminRouter.Party("/role"))
  88. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  89. // ruleChain api
  90. RuleChainAPI := mvc.New(userRouter.Party("/rule_chain"))
  91. RuleChainAPI.Register(ruleChainService).Handle(new(controllers.RuleChainController))
  92. // subDevice api
  93. subDeviceAPI := mvc.New(userRouter).Party("/sub_device")
  94. subDeviceAPI.Register(subDeviceService).Handle(new(controllers.SubDeviceController))
  95. sceneHisAPI := mvc.New(userRouter).Party("/scene_his")
  96. sceneHisAPI.Register(sceneHisService).Handle(new(controllers.SceneHisController))
  97. }