-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
106 lines (86 loc) · 1.96 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-playground/validator/v10"
rpc "github.com/si3nloong/webhook/app/server/grpc"
rest "github.com/si3nloong/webhook/app/server/rest"
"github.com/si3nloong/webhook/app/shared"
"github.com/si3nloong/webhook/app/util"
"github.com/si3nloong/webhook/cmd"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
func main() {
var (
grpcSvr *grpc.Server
quit = make(chan os.Signal, 1)
v = validator.New()
wsCfg = new(cmd.Config)
)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// get current path
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
viper.AddConfigPath(pwd)
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.SetEnvPrefix("webhook")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
// set the default value for configuration
wsCfg.SetDefault()
// read config into struct
if err := viper.Unmarshal(&wsCfg); err != nil {
panic(err)
}
// validate yaml value
if err := v.StructCtx(ctx, wsCfg); err != nil {
panic(err)
}
ws := shared.NewServer(wsCfg)
// serve HTTP
if wsCfg.Enabled {
go func() error {
log.Printf("HTTP server serve at %v", wsCfg.Port)
log.Fatal(http.ListenAndServe(util.FormatPort(wsCfg.Port), rest.NewServer(ws)))
return nil
}()
}
// serve gRPC
if wsCfg.GRPC.Enabled {
grpcSvr = rpc.NewServer(wsCfg, ws)
go func() error {
lis, err := net.Listen("tcp", util.FormatPort(wsCfg.GRPC.Port))
if err != nil {
return err
}
log.Printf("gRPC server serve at %v", wsCfg.GRPC.Port)
if err := grpcSvr.Serve(lis); err != nil {
return err
}
return nil
}()
}
select {
case <-quit:
// close gRPC server if it's exists
if grpcSvr != nil {
grpcSvr.GracefulStop()
}
log.Println("Quit")
case <-ctx.Done():
log.Println("ctx.Done!")
}
}