api.go 3.3 KB

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