main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. rotatelogs "github.com/lestrrat/go-file-rotatelogs"
  6. "github.com/sirupsen/logrus"
  7. "io"
  8. "path"
  9. "lc-base-frame/global"
  10. "lc-base-frame/initialize"
  11. "lc-base-frame/service/system"
  12. "net/http"
  13. "os"
  14. "time"
  15. )
  16. func main() {
  17. initLogrus()
  18. initialize.OtherInit() //初始化缓存
  19. //initialize.Redis()
  20. global.Db = initialize.Gorm() //初始化数据库orm
  21. if global.Db != nil {
  22. // 程序结束前关闭数据库链接
  23. db, _ := global.Db.DB()
  24. defer db.Close()
  25. }
  26. if global.Config.System.UseMultipoint || global.Config.System.UseRedis {
  27. // 初始化redis服务
  28. initialize.Redis()
  29. }
  30. // 从db加载jwt黑名单数据
  31. if global.Db != nil {
  32. system.LoadAll()
  33. }
  34. Router := initialize.Routers()
  35. address := fmt.Sprintf(":%d", global.Config.System.Addr)
  36. s := initServer(address, Router)
  37. logrus.Error(s.ListenAndServe().Error())
  38. }
  39. type server interface {
  40. ListenAndServe() error
  41. }
  42. func initServer(address string, router *gin.Engine) server {
  43. return &http.Server{
  44. Addr: address,
  45. Handler: router,
  46. ReadTimeout: 20 * time.Second,
  47. WriteTimeout: 20 * time.Second,
  48. MaxHeaderBytes: 1 << 20,
  49. }
  50. }
  51. func initLogrus() {
  52. err := os.MkdirAll("./log", os.ModeDir)
  53. if err != nil {
  54. panic(err)
  55. }
  56. fileName := path.Join("./log", "info")
  57. writer, _ := rotatelogs.New(
  58. fileName+".%Y%m%d.log",
  59. rotatelogs.WithMaxAge(time.Duration(global.Config.Logrus.MaxAge)*time.Hour), // 文件最大保存时间
  60. rotatelogs.WithRotationTime(time.Duration(global.Config.Logrus.RotationTime)*time.Hour), // 日志切割时间间隔
  61. )
  62. logrus.SetLevel(logrus.DebugLevel)
  63. if global.Config.Logrus.StdOut {
  64. logrus.SetOutput(io.MultiWriter(os.Stdout, writer))
  65. } else {
  66. logrus.SetOutput(writer)
  67. }
  68. logrus.SetReportCaller(true)
  69. }