| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package main
- import (
- "context"
- "net/http"
- "os"
- "os/signal"
- "speechAnalysis/conf"
- "speechAnalysis/constvar"
- "speechAnalysis/models"
- "speechAnalysis/pkg/logx"
- "speechAnalysis/router"
- "speechAnalysis/service"
- "syscall"
- "time"
- )
- func main() {
- // 读取配置
- if err := conf.Init(); err != nil {
- logx.Errorf("config init error! ", err.Error())
- return
- }
- // 日志初始化
- logx.Init(*conf.LogConf)
- defer logx.Sync()
- // 数据库初始化
- if err := models.Init(); err != nil {
- logx.Errorf("db init error! ", err.Error())
- return
- }
- //监控预加载音频文件
- cxt, cancel := context.WithCancel(context.Background())
- defer cancel()
- go service.PreLoad(cxt)
- logx.Infof("server start serve...")
- server := &http.Server{
- Addr: ":" + conf.WebConf.Port,
- Handler: router.NewRouter(),
- ReadTimeout: 5 * time.Second,
- WriteTimeout: 5 * time.Second,
- }
- go shutdown(server)
- logx.Error(server.ListenAndServe().Error())
- }
- func shutdown(server *http.Server) {
- quit := make(chan os.Signal, 1)
- signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM)
- <-quit
- if constvar.GrpcClient != nil {
- _ = constvar.GrpcClient.Close()
- }
- // 创建一个上下文,设置超时时间
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- logx.Infof("server exiting...")
- if err := server.Shutdown(ctx); err != nil {
- logx.Warnf("服务优雅退出失败: %v", err)
- return
- }
- logx.Infof("server exited success")
- }
|