main.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. "lcfns/eventServer"
  7. "lcfns/gatewayServer"
  8. "lcfns/global"
  9. "lcfns/initialize"
  10. "lcfns/service/system"
  11. "net/http"
  12. "time"
  13. )
  14. func main() {
  15. initialize.InitLogrus()
  16. go eventServer.StartEventServer() //摄像头事件监听服务端
  17. go gatewayServer.GatewayServe() //网关服务端
  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. }