-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (53 loc) · 1.38 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
package main
import (
"demoless/ingress"
"demoless/provider"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
var (
port = pflag.IntP("port", "p", 8080, "ingress listen port")
prov = pflag.String("provider", "docker", "backend service provider: docker/kubernetes")
level = pflag.IntP("level", "v", 4, "logging level 4-info 5-debug")
timout = pflag.Duration("timeout", 30*time.Second, "scale from zero request timeout")
)
func init() {
pflag.Parse()
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.StampMilli,
})
logrus.SetLevel(logrus.Level(*level))
}
func main() {
rand.Seed(time.Now().Unix())
// 初始化 ingress 实例
i, err := ingress.NewIngress(*port, *timout, provider.ProviderType(*prov))
if err != nil {
logrus.Fatalf("create ingress proxy with err: %v", err)
}
// 设置优雅退出信号
stopCh := make(chan struct{})
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
logrus.Infof("receive signal: %v, gracefully shutdown", s)
close(stopCh)
if err := i.Stop(); err != nil {
logrus.Warnf("shutdown http server with error: %v", err)
}
<-c
logrus.Fatal("force exit with code 1")
}()
// 开启运行
err = i.Run(stopCh)
if err != nil {
logrus.Fatalf("start ingress proxy with err: %v", err)
}
}