/
main.go
171 lines (143 loc) · 4.28 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"flag"
"fmt"
//"go.opencensus.io/zpages"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
pb "github.com/leondevpt/wallet/trxservice/api/v1"
"github.com/leondevpt/wallet/trxservice/internal/logger"
"github.com/leondevpt/wallet/trxservice/internal/server"
"github.com/leondevpt/wallet/trxservice/internal/util"
"github.com/leondevpt/wallet/trxservice/pkg/setting"
"github.com/leondevpt/wallet/trxservice/version"
"github.com/leondevpt/wallet/trxservice/global"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
//"go.opencensus.io/zpages"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/resolver"
)
var (
flagconf string
versionShow bool
)
func init() {
flag.StringVar(&flagconf, "conf", "config.yaml", "config path, eg: -conf config.yaml")
flag.BoolVar(&versionShow, "version", false, "show version info, eg: -version")
}
func main() {
flag.Parse()
if versionShow {
version.ShowVersion()
return
}
setting.Init()
fmt.Printf("Cfg:%v\n", setting.Conf)
// 注册自定义的 静态resolver
builder := util.NewStaticResolverBuilder(map[string][]string{global.TronNode: setting.Conf.App.Node_Addr})
resolver.Register(builder)
lg := logger.NewZapLogger()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
app, err := wireApp(setting.Conf, lg)
if err != nil {
lg.Sugar().Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
if err := run(ctx, app); err != nil {
lg.Sugar().Fatal(err)
}
}()
<-done
cancel()
// 等待5秒后退出
c, newCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer newCancel()
<-c.Done()
fmt.Println("shutdown")
close(done)
fmt.Println("done")
}
// run starts the app, handling any REST or gRPC server error
// and as well as app shutdown
func run(ctx context.Context, app app) error {
if err := app.start(); err != nil {
return err
}
defer app.shutdown()
select {
case grpcErr := <-app.grpcServer.Error():
return grpcErr
case <-ctx.Done():
return nil
}
}
// app is a convenience wrapper for all things needed to start
// and shutdown the Order microservice
type app struct {
grpcServer *server.GrpcServer
}
// start starts the REST and gRPC Servers in the background
func (a app) start() error {
//a.grpcServer.Start() // also non blocking :-)
// 给客户端连接注册TrxServiceHandler 和Mux转发到grpc的端口
httpMux := runHttpServer()
grpcS := a.grpcServer.Server
gatewayMux := runGrpcGatewayServer()
httpMux.Handle("/", gatewayMux)
return http.ListenAndServe(fmt.Sprintf(":%d", setting.Conf.GrpcPort), grpcHandlerFunc(grpcS, httpMux))
}
// stop shuts down the servers
func (a app) shutdown() error {
a.grpcServer.Stop()
return nil
}
// newApp creates a new app with REST & gRPC servers
// this func performs all app related initialization
func newApp(gs *server.GrpcServer) (app, error) {
return app{
grpcServer: gs,
}, nil
}
// grpcHandlerFunc 根据请求头判断是grpc请求还是grpc-gateway请求
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
}
func runHttpServer() *http.ServeMux {
serveMux := http.NewServeMux()
serveMux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
hostName, _ := os.Hostname()
_, _ = w.Write([]byte(`pong` + hostName))
})
//zpages.Handle(serveMux, "/debug")
serveMux.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
return serveMux
}
func runGrpcGatewayServer() *gwruntime.ServeMux {
endpoint := fmt.Sprintf("0.0.0.0:%d", setting.Conf.GrpcPort)
gwmux := gwruntime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := pb.RegisterTrxServiceHandlerFromEndpoint(context.Background(), gwmux, endpoint, opts)
if err != nil {
log.Fatal(err)
}
return gwmux
}