// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package main import ( "context" "flag" "fmt" "log" "net" "os" "os/signal" "smart-government-management/impl/v1/user" "smart-government-management/utils" "syscall" "time" "smart-government-management/impl" "smart-government-management/parser" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/tasker" "smart-government-management/pb" "google.golang.org/grpc" ) var ( configFile = flag.String("config", "/etc/smart/common.yaml", "config file location") version = flag.Bool("version", false, "print the version") GitCommit = "library-import" Version = "library-import" // 重置开发者账号 rd = flag.String("rd", "", " ") // 重置管理员账号 ra = flag.String("ra", "", " ") ) func showVersion() { fmt.Println("Version: ", Version) fmt.Println("GitCommit:", GitCommit) } func prepare(filename string) { // 加载配置 err := parser.LoadConfig(filename) if err != nil { fmt.Printf("get conf failed, err: %+v\n\n", err) os.Exit(1) } // 注册处理函数 // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler) parser.Register(parser.LoggerHandler, parser.RedisHandler, parser.MysqlHandler, parser.InfluxdbHandler) // 执行注册的处理函数 parser.Handle() } func run() { // 开始监听 serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.SmartGovernmentManagement.ServiceIp, parser.Conf.Rpc.SmartGovernmentManagement.ServicePort) log.Printf("Listening and serving TCP on %s\n", serveAddr) lis, err := net.Listen("tcp", serveAddr) if err != nil { os.Exit(1) } s := grpc.NewServer() impl.Register(s) go func() { if err := s.Serve(lis); err != nil { log.Fatalf("HTTP server listen failed. err: %s\n", err.Error()) } }() conns := pb.SetupClients() for _, conn := range conns { defer conn.Close() } // 优雅关闭服务器 sigChan := make(chan os.Signal, 1) // 捕获信号 signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) sigValue := <-sigChan log.Printf("Got a signal:%v", sigValue) // 让tasker安全退出 tasker.SignalNotify(sigValue) // 不管什么行为,都等待5秒退出 log.Println("Start to shutdown server...") _, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() s.Stop() log.Println("Shutdown server finished.") } func main() { defer func() { database.Close() }() flag.Parse() if *version { showVersion() os.Exit(1) } prepare(*configFile) if *rd != "" { if utils.VerifyCredential(*rd) { user.ResetDevelop() } os.Exit(1) } if *ra != "" { if utils.VerifyCredential(*rd) { user.ResetAdmin() } os.Exit(1) } //go utils.Free() if err := user.InitDefaultUser(); err != nil { panic("初始化失败:"+err.Error()) } run() return }