router.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. v1router := srv.Party("/api/v1", func(ctx iris.Context) {
  49. span := opentracing.StartSpan(ctx.Path())
  50. defer span.Finish()
  51. span.SetTag("http.method", ctx.Method())
  52. span.SetTag("http.url", ctx.Path())
  53. span.SetTag("http.status_code", ctx.GetStatusCode())
  54. ctx.Values().Set("span", span)
  55. ctx.Next()
  56. })
  57. // 登陆,注册
  58. loginAPI := mvc.New(v1router.Party("/"))
  59. loginAPI.Register(userService).Handle(new(controllers.UserController))
  60. // 用户接口组
  61. userRouter := v1router.Party("/user", newJWThandle())
  62. // user api
  63. userAPI := mvc.New(userRouter.Party("/"))
  64. userAPI.Register(userService).Handle(new(controllers.UserController))
  65. // product api
  66. productAPI := mvc.New(userRouter.Party("/product"))
  67. productAPI.Register(pService).Handle(new(controllers.ProductController))
  68. //application api
  69. appAPI := mvc.New(userRouter.Party("/application"))
  70. appAPI.Register(appService).Handle(new(controllers.AppController))
  71. // sensor api
  72. sensorAPI := mvc.New(userRouter.Party("/sensor"))
  73. sensorAPI.Register(sensorService).Handle(new(controllers.SensorController))
  74. // protocal api
  75. protocalAPI := mvc.New(userRouter.Party("/protocal"))
  76. protocalAPI.Register(protocalService).Handle(new(controllers.ProtocalController))
  77. //alert api
  78. alertAPI := mvc.New(userRouter.Party("/alert"))
  79. alertAPI.Register(alertService).Handle(new(controllers.AlertController))
  80. //device api
  81. deviceAPI := mvc.New(userRouter.Party("/device"))
  82. deviceAPI.Register(deviceService).Handle(new(controllers.DeviceController))
  83. //管理员接口组
  84. adminRouter := v1router.Party("/admin", newJWThandle())
  85. RoleAPI := mvc.New(adminRouter.Party("/role"))
  86. RoleAPI.Register(roleService).Handle(new(controllers.RoleController))
  87. }