12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package main
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- rotatelogs "github.com/lestrrat/go-file-rotatelogs"
- "github.com/sirupsen/logrus"
- "io"
- "path"
- "lc-base-frame/global"
- "lc-base-frame/initialize"
- "lc-base-frame/service/system"
- "net/http"
- "os"
- "time"
- )
- func main() {
- initLogrus()
- 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(time.Duration(global.Config.Logrus.MaxAge)*time.Hour), // 文件最大保存时间
- rotatelogs.WithRotationTime(time.Duration(global.Config.Logrus.RotationTime)*time.Hour), // 日志切割时间间隔
- )
- logrus.SetLevel(logrus.DebugLevel)
- if global.Config.Logrus.StdOut {
- logrus.SetOutput(io.MultiWriter(os.Stdout, writer))
- } else {
- logrus.SetOutput(writer)
- }
- logrus.SetReportCaller(true)
- }
|