-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
235 lines (217 loc) · 7.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"crypto/tls"
"flag"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"golang.org/x/crypto/acme/autocert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
abepb "github.com/grpc-ecosystem/grpc-gateway/examples/proto/examplepb"
addsvcpb "github.com/moul/pb/addsvc/go-grpc"
grpcbinpb "github.com/moul/pb/grpcbin/go-grpc"
hellopb "github.com/moul/pb/hello/go-grpc"
abehandler "github.com/grpc-ecosystem/grpc-gateway/examples/server"
addsvchandler "moul.io/grpcbin/handler/addsvc"
grpcbinhandler "moul.io/grpcbin/handler/grpcbin"
hellohandler "moul.io/grpcbin/handler/hello"
)
var (
insecureAddr = flag.String("insecure-addr", ":9000", "The ip:port combination to listen on for insecure connections")
secureAddr = flag.String("metrics-addr", ":9001", "The ip:port combination to listen on for secure connections")
keyFile = flag.String("tls-key", "cert/server.key", "TLS private key file")
certFile = flag.String("tls-cert", "cert/server.crt", "TLS cert file")
inProduction = flag.Bool("production", false, "Production mode")
productionHTTPAddr = flag.String("production-http-addr", ":80", "The ip:port combination to listen on for production HTTP server")
autocertDir = flag.String("autocert-dir", "./autocert", "Autocert (let's encrypt) caching directory")
)
var index = `<!DOCTYPE html>
<html>
<body>
<h1>grpcbin: gRPC Request & Response Service</h1>
<h2>Endpoints</h2>
<ul>
<li><a href="http://grpcb.in:9000">grpc://grpcb.in:9000 (without TLS)</a></li>
<li><a href="https://grpcb.in:9001">grpc://grpcb.in:9001 (with TLS)</a></li>
</ul>
<h2>Methods</h2>
<ul>
<li>
<a href="https://github.com/moul/pb/blob/master/grpcbin/grpcbin.proto">grpcbin.proto</a>
<ul>
{{- range .}}
<li>{{.MethodName}}</li>
{{- end}}
</ul>
</li>
<li>
<a href="https://github.com/moul/pb/blob/master/hello/hello.proto">hello.proto</a>
<ul>
<li>SayHello</li>
<li>LotsOfReplies</li>
<li>LotsOfGreetings</li>
<li>BidiHello</li>
</ul>
</li>
<li>
<a href="https://github.com/moul/pb/blob/master/addsvc/addsvc.proto">addsvc.proto</a>
<ul>
<li>Sum</li>
<li>Concat</li>
</ul>
</li>
<li>
<a href="https://github.com/moul/pb/blob/master/a_bit_of_everything/lib/examples/examplepb/a_bit_of_everything.proto">a_bit_of_everything.proto</a>
<ul>
<li>Create</li>
<li>CreateBody</li>
<li>Lookup</li>
<li>Update</li>
<li>Delete</li>
<li>GetQuery</li>
<li>Echo</li>
<li>DeepPathEcho</li>
<li>NoBindings</li>
<li>Timeout</li>
<li>ErrorWithDetails</li>
<li>GetMessageWithBody</li>
<li>PostWithEmptyBody</li>
</ul>
</li>
</ul>
<h2>Examples</h2>
<ul>
<li><a href="https://github.com/moul/grpcbin-example">https://github.com/moul/grpcbin-example</a> (multiple languages)</li>
<li><a href="https://github.com/lucasdicioccio/http2-client-grpc-example/">https://github.com/lucasdicioccio/http2-client-grpc-example/</a> (haskell)</li>
</ul>
<h2>About</h2>
<a href="https://github.com/moul/grpcbin">Developed</a> by <a href="https://manfred.life">Manfred Touron</a>, inspired by <a href="https://httpbin.org/">https://httpbin.org/</a>
<!-- 100% privacy friendly analytics -->
<script async defer src="https://sa.moul.io/latest.js"></script>
<noscript><img src="https://queue.simpleanalyticscdn.com/noscript.gif" alt="" referrerpolicy="no-referrer-when-downgrade" /></noscript>
</body>
</html>
`
func main() {
// parse flags
flag.Parse()
// insecure listener
go func() {
listener, err := net.Listen("tcp", *insecureAddr)
if err != nil {
log.Fatalf("failted to listen: %v", err)
}
// create gRPC server
s := grpc.NewServer()
grpcbinpb.RegisterGRPCBinServer(s, &grpcbinhandler.Handler{})
hellopb.RegisterHelloServiceServer(s, &hellohandler.Handler{})
addsvcpb.RegisterAddServer(s, &addsvchandler.Handler{})
abepb.RegisterABitOfEverythingServiceServer(s, abehandler.NewHandler())
// register reflection service on gRPC server
reflection.Register(s)
// serve
log.Printf("listening on %s (insecure gRPC)\n", *insecureAddr)
if err := s.Serve(listener); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
// secure listener
go func() {
var (
creds credentials.TransportCredentials
httpSrv = &http.Server{
Addr: *secureAddr,
}
)
// initialize tls configuration and grpc credentials based on production/development environment
if *inProduction {
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("grpcb.in"),
Cache: autocert.DirCache(*autocertDir),
}
httpSrv.TLSConfig = m.TLSConfig()
creds = credentials.NewTLS(httpSrv.TLSConfig)
} else {
var err error
creds, err = credentials.NewServerTLSFromFile(*certFile, *keyFile)
if err != nil {
log.Fatalf("failed to load TLS keys: %v", err)
}
cert, err := tls.LoadX509KeyPair(*certFile, *keyFile)
if err != nil {
log.Fatalf("failed to laod TLS keys: %v", err)
}
httpSrv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}
// setup grpc servef
s := grpc.NewServer(grpc.Creds(creds))
grpcbinpb.RegisterGRPCBinServer(s, &grpcbinhandler.Handler{})
hellopb.RegisterHelloServiceServer(s, &hellohandler.Handler{})
addsvcpb.RegisterAddServer(s, &addsvchandler.Handler{})
abepb.RegisterABitOfEverythingServiceServer(s, abehandler.NewHandler())
// register reflection service on gRPC server
reflection.Register(s)
// initilaize HTTP routing based on production/development environment
if *inProduction {
mux := http.NewServeMux()
t := template.New("")
var err error
t, err = t.Parse(index)
if err != nil {
log.Fatalf("failt to parse template: %v", err)
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err2 := t.Execute(w, grpcbinpb.GRPCBin_serviceDesc.Methods); err != nil {
http.Error(w, err2.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("Cache-Control", "public, max-age=7776000")
if _, err = fmt.Fprintln(w, "data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII="); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
httpSrv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
s.ServeHTTP(w, r)
} else {
mux.ServeHTTP(w, r)
}
})
} else {
httpSrv.Handler = s
}
// listen and serve
log.Printf("listening on %s (secure gRPC + secure HTTP/2)\n", *secureAddr)
if err := httpSrv.ListenAndServeTLS("", ""); err != nil {
log.Fatalf("failed to listen: %v", err)
}
}()
if *inProduction {
// production HTTP server (redirect to https)
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://grpcb.in", 301)
})
log.Printf("listening on %s (production HTTP)\n", *productionHTTPAddr)
if err := http.ListenAndServe(*productionHTTPAddr, mux); err != nil {
log.Fatalf("failed to listen: %v", err)
}
}()
}
// handle Ctrl+C
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
log.Fatalf("%s", <-c)
}