router.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. v1router := srv.Party("/api/v1")
  51. // 登陆,注册
  52. loginAPI := mvc.New(v1router.Party("/"))
  53. loginAPI.Register(userService).Handle(new(controllers.UserController))
  54. // 用户接口组
  55. userRouter := v1router.Party("/user", newJWThandle())
  56. // user api
  57. userAPI := mvc.New(userRouter.Party("/"))
  58. userAPI.Register(userService).Handle(new(controllers.UserController))
  59. // product api
  60. productAPI := mvc.New(userRouter.Party("/product"))
  61. productAPI.Register(pService).Handle(new(controllers.ProductController))
  62. //application api
  63. appAPI := mvc.New(userRouter.Party("/application"))
  64. appAPI.Register(appService).Handle(new(controllers.AppController))
  65. // sensor api
  66. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  67. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  68. // protocal api
  69. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  70. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  71. //alert api
  72. alertAPI := mvc.New(userRouter.Party("/alert"))
  73. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  74. //device api
  75. deviceAPI := mvc.New(userRouter.Party("/device"))
  76. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  77. fileAPI := mvc.New(userRouter.Party("/file"))
  78. fileAPI.Register(fileService).Handle(new(controllers.FileController))
  79. //管理员接口组
  80. adminRouter := v1router.Party("/admin", newJWThandle())
  81. RoleAPI := mvc.New(adminRouter.Party("/role"))
  82. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  83. // ruleChain api
  84. RuleChainAPI := mvc.New(userRouter.Party("/rule_chain"))
  85. RuleChainAPI.Register(ruleChainService).Handle(new(controllers.RuleChainController))
  86. // subDevice api
  87. subDeviceAPI := mvc.New(userRouter).Party("/sub_device")
  88. subDeviceAPI.Register(subDeviceService).Handle(new(controllers.SubDeviceController))
  89. }