main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. "lc-fangdaosha/global"
  7. "lc-fangdaosha/initialize"
  8. "lc-fangdaosha/service/system"
  9. "net/http"
  10. "os"
  11. "time"
  12. )
  13. func main() {
  14. logrus.SetLevel(logrus.DebugLevel)
  15. logrus.SetOutput(os.Stdout)
  16. logrus.SetReportCaller(true)
  17. initialize.OtherInit() //初始化缓存
  18. //initialize.Redis()
  19. global.Db = initialize.Gorm() //初始化数据库orm
  20. if global.Db != nil {
  21. //initialize.RegisterTables() //初始化表
  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. }