智盟项目
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.

64 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. if err := db.InitZhimengDB(cfg.ZhimengDB); err != nil {
  26. panic(err)
  27. }
  28. }
  29. fmt.Println("init success")
  30. }
  31. func main() {
  32. r := router.Init() //创建路由
  33. srv := &http.Server{ //设置http服务参数
  34. Addr: cfg.SrvAddr, //指定ip和端口
  35. Handler: r, //指定路由
  36. }
  37. go func() { //协程启动监听http服务
  38. fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
  39. if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  40. log.Fatalf("listen: %s\n", err)
  41. }
  42. }()
  43. //退出go守护进程
  44. quit := make(chan os.Signal)
  45. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  46. <-quit
  47. log.Println("Shutting down server...")
  48. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  49. defer cancel()
  50. if err := srv.Shutdown(ctx); err != nil {
  51. log.Fatal("Server forced to shutdown:", err)
  52. }
  53. log.Println("Server exiting")
  54. }