|
- package main
-
- import (
- "applet/app/db"
- "context"
- "fmt"
- "log"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
-
- "applet/app/cfg"
- "applet/app/router"
- )
-
- // 系统初始化
- func init() {
- cfg.InitCfg() //配置初始化
- cfg.InitLog() //日志初始化
- cfg.InitCache() //缓存初始化
- if cfg.Debug { //判断是否是debug
- if err := db.InitDB(cfg.DB); err != nil { //主数据库初始化
- panic(err)
- }
- }
- fmt.Println("init success")
-
- }
-
- func main() {
- r := router.Init() //创建路由
-
- srv := &http.Server{ //设置http服务参数
- Addr: cfg.SrvAddr, //指定ip和端口
- Handler: r, //指定路由
- }
-
- go func() { //协程启动监听http服务
- fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
- if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- log.Fatalf("listen: %s\n", err)
- }
- }()
-
- //退出go守护进程
- quit := make(chan os.Signal)
- signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
- <-quit
- log.Println("Shutting down server...")
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- if err := srv.Shutdown(ctx); err != nil {
- log.Fatal("Server forced to shutdown:", err)
- }
- log.Println("Server exiting")
-
- }
|