package api

import (
	"copter-train/conf"
	"copter-train/db"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

// 初始化http服务
func NewHttpService(app *conf.AppConf, dbMongo *db.MongoDB, redisClient *redis.Client) *Service {

	engine := gin.Default()

	// store := cookie.NewStore([]byte("adhuaxi-server"))
	// engine.Use(sessions.Sessions("dcsession", store))
	// engine.Static("/public", "static")
	config := cors.DefaultConfig()
	// config.AllowOrigins == []string{"http://google.com", "http://facebook.com"}
	config.AllowAllOrigins = true
	config.AllowHeaders = append(config.AllowHeaders, "authorization")
	engine.Use(cors.New(config))

	jwt := NewUitlsJwt(app)

	ApiService = &Service{Conf: app, Redis: redisClient, JWT: jwt, Gin: engine, Mongo: dbMongo, Port: app.Port}

	RegRouters(ApiService)

	return ApiService
}
