api.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "mesh/conf"
  6. "mesh/db"
  7. "mesh/db/repo"
  8. "github.com/gin-contrib/cors"
  9. "github.com/gin-contrib/sessions"
  10. "github.com/gin-contrib/sessions/cookie"
  11. "github.com/gin-gonic/gin"
  12. "github.com/go-redis/redis/v8"
  13. )
  14. type Service struct {
  15. Gin *gin.Engine
  16. Mongo *db.MongoDB
  17. Redis *redis.Client
  18. Port int32
  19. DebugUserId string
  20. DebugUserPhone string
  21. DebugUserRole string
  22. JWT *UtilsJwt
  23. Conf *conf.AppConf
  24. }
  25. func (svc *Service) Run() {
  26. svc.Gin.Run(fmt.Sprintf(":%d", svc.Port))
  27. }
  28. type ApiSession struct {
  29. Svc *Service
  30. User *JWTUser
  31. }
  32. func (api *ApiSession) CreateRepoCtx() *repo.RepoSession {
  33. return &repo.RepoSession{
  34. Ctx: context.Background(),
  35. Client: api.Svc.Mongo,
  36. }
  37. }
  38. func NewHttpService(app *conf.AppConf, dbMongo *db.MongoDB, redisClient *redis.Client) *Service {
  39. engine := gin.Default()
  40. store := cookie.NewStore([]byte("spu3d-server"))
  41. engine.Use(sessions.Sessions("dcsession", store))
  42. engine.Static("/public", "static")
  43. config := cors.DefaultConfig()
  44. // config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}
  45. config.AllowAllOrigins = true
  46. config.AllowHeaders = append(config.AllowHeaders, "authorization")
  47. engine.Use(cors.New(config))
  48. jwt := NewUitlsJwt(app)
  49. s := &Service{Conf: app, Redis: redisClient, JWT: jwt, Gin: engine, Mongo: dbMongo, Port: app.Port, DebugUserId: app.Debug.UserId, DebugUserPhone: app.Debug.UserPhone, DebugUserRole: app.Debug.UserRole}
  50. RegRouters(s)
  51. return s
  52. }
  53. // GinRouter 路由
  54. type GinRouter struct {
  55. group *gin.RouterGroup
  56. svc *Service
  57. }
  58. func (svc *Service) NewGinRouter(path string) *GinRouter {
  59. return &GinRouter{group: svc.Gin.Group(path), svc: svc}
  60. }
  61. // RouterInterface 路由接口
  62. type RouterInterface interface {
  63. GET(path string, httpHandler Handler)
  64. POST(path string, httpHandler Handler)
  65. }
  66. // GET http Get 请求
  67. func (g GinRouter) GET(path string, httpHandler Handler) {
  68. g.group.GET(path, ResultWrapper(httpHandler, g.svc))
  69. }
  70. // POST http POST 请求
  71. func (g GinRouter) POST(path string, httpHandler Handler) {
  72. g.group.POST(path, ResultWrapper(httpHandler, g.svc))
  73. }
  74. // GETJWT http Get 请求
  75. func (g GinRouter) GETJWT(path string, httpHandler JWTHander) {
  76. g.group.GET(path, g.svc.JWT.MiddleFunc(), ResultJWTWrapper(httpHandler, g.svc))
  77. }
  78. // GETJWTTest http Get 请求
  79. func (g GinRouter) GETJWTTest(path string, httpHandler JWTHander) {
  80. g.group.GET(path, ResultJWTTestWrapper(httpHandler, g.svc))
  81. }
  82. // POSTJWT http POST 请求
  83. func (g GinRouter) POSTJWT(path string, httpHandler JWTHander) {
  84. g.group.POST(path, g.svc.JWT.MiddleFunc(), ResultJWTWrapper(httpHandler, g.svc))
  85. }
  86. // DeleteJWT http POST 请求
  87. func (g GinRouter) DeleteJWT(path string, httpHandler JWTHander) {
  88. g.group.DELETE(path, g.svc.JWT.MiddleFunc(), ResultJWTWrapper(httpHandler, g.svc))
  89. }
  90. // DeleteJWT http POST 请求
  91. func (g GinRouter) DeleteJWTTEST(path string, httpHandler JWTHander) {
  92. g.group.DELETE(path, ResultJWTTestWrapper(httpHandler, g.svc))
  93. }
  94. // POSTJWTTest 测试
  95. func (g GinRouter) POSTJWTTest(path string, httpHandler JWTHander) {
  96. g.group.POST(path, ResultJWTTestWrapper(httpHandler, g.svc))
  97. }