-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
133 lines (117 loc) · 3.66 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
// Copyright 2017 Johan Brandhorst. All Rights Reserved.
// See LICENSE for licensing terms.
package main
import (
"crypto/tls"
"flag"
"net/http"
"path"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/lpar/gzipped"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"github.com/johanbrandhorst/grpcweb-example/client/compiled"
"github.com/johanbrandhorst/grpcweb-example/server"
"github.com/johanbrandhorst/grpcweb-example/server/proto/library"
)
var logger *logrus.Logger
var host = flag.String("host", "", "host to get LetsEncrypt certificate for")
func init() {
logger = logrus.StandardLogger()
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
DisableSorting: true,
})
// Should only be done from init functions
grpclog.SetLogger(logger)
}
func main() {
flag.Parse()
gs := grpc.NewServer()
library.RegisterBookServiceServer(gs, &server.BookService{})
wrappedServer := grpcweb.WrapServer(gs, grpcweb.WithWebsockets(true))
httpsSrv := &http.Server{
// These interfere with websocket streams, disable for now
// ReadTimeout: 5 * time.Second,
// WriteTimeout: 10 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
Addr: ":https",
TLSConfig: &tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
},
Handler: hstsHandler(
grpcTrafficSplitter(
folderReader(
gzipped.FileServer(compiled.Assets).ServeHTTP,
),
wrappedServer,
),
),
}
// Serve on localhost with localhost certs if no host provided
if *host == "" {
httpsSrv.Addr = "localhost:10000"
logger.Info("Serving on https://localhost:10000")
logger.Fatal(httpsSrv.ListenAndServeTLS("./insecure/cert.pem", "./insecure/key.pem"))
}
// Create auto-certificate https server
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*host),
Cache: autocert.DirCache("/certs"),
}
// Create server for redirecting HTTP to HTTPS
httpSrv := &http.Server{
Addr: ":http",
ReadTimeout: httpsSrv.ReadTimeout,
WriteTimeout: httpsSrv.WriteTimeout,
IdleTimeout: httpsSrv.IdleTimeout,
Handler: m.HTTPHandler(nil),
}
go func() {
logger.Fatal(httpSrv.ListenAndServe())
}()
httpsSrv.TLSConfig = m.TLSConfig()
logger.Info("Serving on https://0.0.0.0:443, authenticating for https://", *host)
logger.Fatal(httpsSrv.ListenAndServeTLS("", ""))
}
// hstsHandler wraps an http.HandlerFunc such that it sets the HSTS header.
func hstsHandler(fn http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
fn(w, r)
})
}
func folderReader(fn http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
// Use contents of index.html for directory, if present.
r.URL.Path = path.Join(r.URL.Path, "index.html")
}
fn(w, r)
})
}
func grpcTrafficSplitter(fallback http.HandlerFunc, grpcHandler http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Redirect gRPC and gRPC-Web requests to the gRPC Server
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") ||
websocket.IsWebSocketUpgrade(r) {
grpcHandler.ServeHTTP(w, r)
} else {
fallback(w, r)
}
})
}