Files
2022-03-17 15:59:24 +08:00

91 lines
2.0 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/inconshreveable/log15"
"gitlab.33.cn/chat/dtalk/service/discovery/config"
"gitlab.33.cn/chat/dtalk/service/discovery/server/http"
"gitlab.33.cn/chat/dtalk/service/discovery/service"
)
const srvName = "discovery"
var log = log15.New("cmd", srvName)
var (
// projectVersion 项目版本
projectVersion = "0.0.1"
// goVersion go版本
goVersion = ""
// gitCommit git提交commit id
gitCommit = ""
// buildTime 编译时间
buildTime = ""
isShowVersion = flag.Bool("v", false, "show project version")
)
// showVersion 显示项目版本信息
func showVersion(isShow bool) {
if isShow {
fmt.Printf("Project: %s\n", srvName)
fmt.Printf(" Version: %s\n", projectVersion)
fmt.Printf(" Go Version: %s\n", goVersion)
fmt.Printf(" Git Commit: %s\n", gitCommit)
fmt.Printf(" Build Time: %s\n", buildTime)
os.Exit(0)
}
}
// @Title 聊天单模块集成测试
// @Version 0.1
// @Description
// @SecurityDefinitions.ApiKey ApiKeyAuth
// @In header
// @Name Authorization
// @BasePath /
func main() {
flag.Parse()
showVersion(*isShowVersion)
if err := config.Init(); err != nil {
panic(err)
}
log.Info("config info:",
"Redis", *config.Conf.Redis,
"Server", *config.Conf.Server)
// service init
svc := service.New(config.Conf)
httpSrv := http.Init(svc)
// init signal
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("service get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpSrv.Shutdown(ctx); err != nil {
log.Error("server shutdown:", "err", err)
}
time.Sleep(time.Second * 2)
log.Info(srvName + " server exit")
return
case syscall.SIGHUP:
// TODO reload
default:
return
}
}
}