-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (68 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/palp1tate/brevinect/api/global"
"github.com/palp1tate/brevinect/api/initialize"
"github.com/palp1tate/brevinect/util"
"go.uber.org/zap"
)
func main() {
initialize.InitConfig()
initialize.InitLogger()
router, closer := initialize.Router()
if err := initialize.InitTranslator("zh"); err != nil {
zap.S().Warn("初始化翻译器失败:", err.Error())
panic(err)
}
initialize.InitFilter()
initialize.InitSentinel()
initialize.InitValidator()
initialize.InitServiceConn()
host := global.ServerConfig.Api.Host
port := flag.Int("p", 0, "端口号")
flag.Parse()
if *port == 0 {
*port, _ = util.GetFreePort()
}
zap.S().Info("host: ", host)
zap.S().Info("port: ", *port)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
zap.S().Panic("网关启动失败:", err.Error())
}
}()
client := initialize.NewRegistryClient(global.ServerConfig.Consul.Host, global.ServerConfig.Consul.Port)
apiName := global.ServerConfig.Api.Name
apiTags := global.ServerConfig.Api.Tags
apiId := util.GenerateUUID()
err := client.Register(host, *port, apiName, apiTags, apiId)
if err != nil {
zap.S().Panic("服务注册失败:", err.Error())
}
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
defer closer.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = srv.Shutdown(ctx); err != nil {
zap.S().Error("Server forced to shutdown:", err)
}
if err = client.DeRegister(apiId); err != nil {
zap.S().Warnf("%s注销失败", apiName)
} else {
zap.S().Infof("%s注销成功", apiName)
}
}