main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // 程序结束前关闭数据库链接
  22. db, _ := global.Db.DB()
  23. defer db.Close()
  24. }
  25. if global.Config.System.UseMultipoint || global.Config.System.UseRedis {
  26. // 初始化redis服务
  27. initialize.Redis()
  28. }
  29. // 从db加载jwt黑名单数据
  30. if global.Db != nil {
  31. system.LoadAll()
  32. }
  33. Router := initialize.Routers()
  34. address := fmt.Sprintf(":%d", global.Config.System.Addr)
  35. s := initServer(address, Router)
  36. logrus.Error(s.ListenAndServe().Error())
  37. }
  38. type server interface {
  39. ListenAndServe() error
  40. }
  41. func initServer(address string, router *gin.Engine) server {
  42. return &http.Server{
  43. Addr: address,
  44. Handler: router,
  45. ReadTimeout: 20 * time.Second,
  46. WriteTimeout: 20 * time.Second,
  47. MaxHeaderBytes: 1 << 20,
  48. }
  49. }