附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 2.4 KiB

2 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "os/signal"
  9. "syscall"
  10. "time"
  11. "applet/app/cfg"
  12. "applet/app/db"
  13. "applet/app/router"
  14. "applet/app/utils"
  15. )
  16. //系统初始化
  17. func init() {
  18. cfg.InitCfg() //配置初始化
  19. cfg.InitLog() //日志初始化
  20. cfg.InitCache() //缓存初始化
  21. cfg.InitCacheSecond() //缓存初始化
  22. cfg.InitMq() //缓存初始化
  23. cfg.InitEs() //ElasticSearch初始化
  24. if cfg.Debug { //判断是否是debug
  25. if err := db.InitDB(cfg.DB); err != nil { //主数据库初始化
  26. panic(err)
  27. }
  28. if err := db.InitZhimengDB(cfg.ZhimengDB); err != nil {
  29. panic(err)
  30. }
  31. channel := make(chan int, 0) //开辟管道,缓冲为
  32. go db.InitDBs(channel)
  33. <-channel
  34. }
  35. fmt.Println("init success")
  36. }
  37. // @title 智莺生活移动端接口
  38. // @version 1.0
  39. // @description 移动端接口
  40. // @termsOfService 智莺生活后端组
  41. // @contact.name sherlockwhite
  42. // @host localhost:5000
  43. // @securityDefinitions.apikey MasterID
  44. // @in header
  45. // @name MasterID
  46. // @BasePath /
  47. func main() {
  48. // 启动获取所有品牌
  49. //go taoke.GetAllBrand()
  50. r := router.Init() //创建路由
  51. // arkid.Init()
  52. srv := &http.Server{ //设置http服务参数
  53. Addr: cfg.SrvAddr, //指定ip和端口
  54. Handler: r, //指定路由
  55. }
  56. // 读取默认站长的推广位 并写进redis
  57. // master, err := db.UserProfileFindByID(,"1")
  58. // if err != nil {
  59. // panic(err)
  60. // }
  61. if cfg.CurlDebug {
  62. utils.CurlDebug = true
  63. }
  64. //
  65. // if has := cache.SetJson(svc.SysCfgGet(nil, "app_name")+"_default_pid_user", master, 0); has != true {
  66. // panic(errors.New("设置默认pid缓存失败"))
  67. // }
  68. // Initializing the server in a goroutine so that it won't block the graceful shutdown handling below
  69. go func() { //协程启动监听http服务
  70. fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
  71. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  72. log.Fatalf("listen: %s\n", err)
  73. }
  74. }()
  75. /*go func() {
  76. _ = http.ListenAndServe("0.0.0.0:6060", nil)
  77. }()*/
  78. // graceful shutdown
  79. //退出go守护进程
  80. quit := make(chan os.Signal)
  81. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  82. <-quit
  83. log.Println("Shutting down server...")
  84. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  85. defer cancel()
  86. if err := srv.Shutdown(ctx); err != nil {
  87. log.Fatal("Server forced to shutdown:", err)
  88. }
  89. log.Println("Server exiting")
  90. }