| | |
| | | package main |
| | | |
| | | import ( |
| | | "context" |
| | | "net/http" |
| | | "os" |
| | | "os/signal" |
| | | "syscall" |
| | | "time" |
| | | "wms/conf" |
| | | "wms/constvar" |
| | | "wms/models" |
| | | "wms/pkg/logx" |
| | | "wms/router" |
| | |
| | | return |
| | | } |
| | | |
| | | // 启动APS RPC服务 |
| | | //safe.Go(service.StartAPServer) |
| | | |
| | | go shutdown() |
| | | logx.Infof("aps-server start serve...") |
| | | server := &http.Server{ |
| | | Addr: ":" + conf.WebConf.Port, |
| | |
| | | ReadTimeout: 5 * time.Second, |
| | | WriteTimeout: 5 * time.Second, |
| | | } |
| | | |
| | | go shutdown(server) |
| | | logx.Error(server.ListenAndServe().Error()) |
| | | } |
| | | |
| | | func shutdown() { |
| | | func shutdown(server *http.Server) { |
| | | quit := make(chan os.Signal, 1) |
| | | signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM) |
| | | <-quit |
| | | |
| | | _ = constvar.GrpcClient.Close() |
| | | logx.Infof("aps-server exited...") |
| | | os.Exit(0) |
| | | logx.Infof("server exiting...") |
| | | // 创建一个上下文,设置超时时间 |
| | | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| | | defer cancel() |
| | | |
| | | // 关闭HTTP服务器 |
| | | if err := server.Shutdown(ctx); err != nil { |
| | | logx.Infof("服务优雅退出失败: %v", err) |
| | | return |
| | | } |
| | | logx.Infof("server exited") |
| | | } |