router.go 3.4 KB

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