-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (67 loc) · 1.77 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
package main
import (
"encoding/json"
"flag"
"github.com/mafengwo/grpc-fcgi/grpc"
"github.com/mafengwo/grpc-fcgi/log"
"go.uber.org/zap"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
)
var (
configFile = flag.String("f", "", "config file path")
)
func main() {
flag.Parse()
// load config items
options, loadErr := grpc.LoadConfig(*configFile)
if loadErr != nil {
panic("cannot load config file: " + loadErr.Error())
}
if err := options.Validate(); err != nil {
panic("config item is illegal: " + err.Error())
}
// initialize logger
logger, err := log.NewLogger(&log.Options{
AccessLogPath: options.Log.AccessLogPath,
ErrorLogPath: options.Log.ErrorLogPath,
ErrorLogLevel: options.Log.ErrorLogLevel,
})
if err != nil {
panic("cannot build logger: " + err.Error())
}
// logging config for feedback
optjson, err := json.MarshalIndent(options, "", " ")
if err != nil {
panic("marshal config failed:" + err.Error())
}
logger.ErrorLogger().Info(string(optjson), zap.String("type", "config_items"))
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
s := grpc.NewProxy(options, logger)
go func() {
<-sigs
logger.ErrorLogger().Info("signal received, prepare to shutdown...")
s.GracefulStop()
logger.ErrorLogger().Info("server has been shutdown")
}()
// pprof
go func() {
// for healthy check
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Write([]byte("ok"))
}
})
err := http.ListenAndServe(options.PprofAddress, nil)
logger.ErrorLogger().Info("serve pprof: " + err.Error())
}()
logger.ErrorLogger().Info("starting to serve")
if err := s.Serve(); err != nil {
logger.ErrorLogger().Error("serve failed: " + err.Error())
os.Exit(3)
}
}