main.go 1.8 KB

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