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

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