数据采集
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.
 
 
 
 
 
 

61 lines
1.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. )
  15. // 系统初始化
  16. func init() {
  17. cfg.InitCfg() //配置初始化
  18. cfg.InitLog() //日志初始化
  19. cfg.InitCache() //缓存初始化
  20. cfg.InitEs() //ElasticSearch初始化
  21. if cfg.Debug { //判断是否是debug
  22. if err := db.InitDB(cfg.DB); err != nil { //主数据库初始化
  23. panic(err)
  24. }
  25. }
  26. fmt.Println("init success")
  27. }
  28. func main() {
  29. r := router.Init() //创建路由
  30. srv := &http.Server{ //设置http服务参数
  31. Addr: cfg.SrvAddr, //指定ip和端口
  32. Handler: r, //指定路由
  33. }
  34. go func() { //协程启动监听http服务
  35. fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
  36. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  37. log.Fatalf("listen: %s\n", err)
  38. }
  39. }()
  40. //退出go守护进程
  41. quit := make(chan os.Signal)
  42. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  43. <-quit
  44. log.Println("Shutting down server...")
  45. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  46. defer cancel()
  47. if err := srv.Shutdown(ctx); err != nil {
  48. log.Fatal("Server forced to shutdown:", err)
  49. }
  50. log.Println("Server exiting")
  51. }