附近小店
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.
 
 
 

98 lines
2.3 KiB

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