广告平台(站长下代理使用)
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.
 
 
 
 
 
 

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