router.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "sparrow/pkg/generator"
  4. "sparrow/services/knowoapi/controllers"
  5. "sparrow/services/knowoapi/model"
  6. "sparrow/services/knowoapi/services"
  7. "github.com/opentracing/opentracing-go"
  8. jwt "github.com/dgrijalva/jwt-go"
  9. jwtmiddleware "github.com/iris-contrib/middleware/jwt"
  10. "github.com/kataras/iris"
  11. "github.com/kataras/iris/mvc"
  12. )
  13. func registerErrors(srv *iris.Application) {
  14. srv.OnAnyErrorCode(handleErrors)
  15. }
  16. func handleErrors(ctx iris.Context) {
  17. //logger.Default().Serve(ctx)
  18. err := controllers.ErrorResponse{
  19. Code: ctx.GetStatusCode(),
  20. Message: ctx.Values().GetStringDefault("reason", "未知错误"),
  21. }
  22. ctx.JSON(err)
  23. }
  24. // jwt 中间件配置
  25. func newJWThandle() func(ctx iris.Context) {
  26. jwtHandler := jwtmiddleware.New(jwtmiddleware.Config{
  27. ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
  28. return []byte(model.SignedString), nil
  29. },
  30. ErrorHandler: func(ctx iris.Context, message string) {
  31. ctx.StatusCode(iris.StatusUnauthorized)
  32. ctx.Values().Set("reason", message)
  33. },
  34. SigningMethod: jwt.SigningMethodHS256,
  35. })
  36. return jwtHandler.Serve
  37. }
  38. // 请求路由
  39. func registerRouters(srv *iris.Application, models *model.All, gen *generator.KeyGenerator) {
  40. pService := services.NewProductService(models, gen)
  41. userService := services.NewUserService(models, gen)
  42. appService := services.NewAppService(models, gen)
  43. sensorService := services.NewSensorService(models)
  44. protocalService := services.NewProtocalService(models)
  45. alertService := services.NewAlertService(models)
  46. deviceService := services.NewDeviceService(models)
  47. roleService := services.NewRoleService(models)
  48. ruleChainService := services.NewRuleChainService(models)
  49. v1router := srv.Party("/api/v1", func(ctx iris.Context) {
  50. span := opentracing.StartSpan(ctx.Path())
  51. defer span.Finish()
  52. span.SetTag("http.method", ctx.Method())
  53. span.SetTag("http.url", ctx.Path())
  54. span.SetTag("http.status_code", ctx.GetStatusCode())
  55. ctx.Values().Set("span", span)
  56. ctx.Next()
  57. })
  58. // 登陆,注册
  59. loginAPI := mvc.New(v1router.Party("/"))
  60. loginAPI.Register(userService).Handle(new(controllers.UserController))
  61. // 用户接口组
  62. userRouter := v1router.Party("/user", newJWThandle())
  63. // user api
  64. userAPI := mvc.New(userRouter.Party("/"))
  65. userAPI.Register(userService).Handle(new(controllers.UserController))
  66. // product api
  67. productAPI := mvc.New(userRouter.Party("/product"))
  68. productAPI.Register(pService).Handle(new(controllers.ProductController))
  69. //application api
  70. appAPI := mvc.New(userRouter.Party("/application"))
  71. appAPI.Register(appService).Handle(new(controllers.AppController))
  72. // sensor api
  73. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  74. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  75. // protocal api
  76. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  77. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  78. //alert api
  79. alertAPI := mvc.New(userRouter.Party("/alert"))
  80. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  81. //device api
  82. deviceAPI := mvc.New(userRouter.Party("/device"))
  83. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  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(adminRouter.Party("/rule_chain"))
  90. RuleChainAPI.Register(ruleChainService).Handle(new(controllers.RuleChainController))
  91. }