123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package main
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- rotatelogs "github.com/lestrrat/go-file-rotatelogs"
- "github.com/sirupsen/logrus"
- "lc-fangdaosha/eventServer"
- "lc-fangdaosha/gatewayServer"
- "lc-fangdaosha/global"
- "lc-fangdaosha/initialize"
- "lc-fangdaosha/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)
- }
|