api.go 3.4 KB

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