package main import ( "fmt" "github.com/gin-gonic/gin" rotatelogs "github.com/lestrrat/go-file-rotatelogs" "github.com/sirupsen/logrus" "lcfns/eventServer" "lcfns/gatewayServer" "lcfns/global" "lcfns/initialize" "lcfns/service/system" "net/http" "os" "path" "time" ) func main() { initLogrus() go eventServer.StartEventServer() //摄像头事件监听服务端 go gatewayServer.GatewayServe() //网关服务端 initialize.OtherInit() //初始化缓存 //initialize.Redis() global.Db = initialize.Gorm() //初始化数据库orm if global.Db != nil { // 程序结束前关闭数据库链接 db, _ := global.Db.DB() defer db.Close() } if global.Config.System.UseMultipoint || global.Config.System.UseRedis { // 初始化redis服务 initialize.Redis() } // 从db加载jwt黑名单数据 if global.Db != nil { system.LoadAll() } Router := initialize.Routers() address := fmt.Sprintf(":%d", global.Config.System.Addr) s := initServer(address, Router) logrus.Error(s.ListenAndServe().Error()) } type server interface { ListenAndServe() error } func initServer(address string, router *gin.Engine) server { return &http.Server{ Addr: address, Handler: router, ReadTimeout: 20 * time.Second, WriteTimeout: 20 * time.Second, MaxHeaderBytes: 1 << 20, } } func initLogrus() { err := os.MkdirAll("./log", os.ModeDir) if err != nil { panic(err) } fileName := path.Join("./log", "info") writer, _ := rotatelogs.New( fileName+".%Y%m%d.log", rotatelogs.WithMaxAge(15*24*time.Hour), // 文件最大保存时间 rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔 ) logrus.SetLevel(logrus.DebugLevel) logrus.SetOutput(os.Stdout) logrus.SetReportCaller(true) logrus.SetOutput(writer) }