-
Notifications
You must be signed in to change notification settings - Fork 1
/
runprox.go
163 lines (138 loc) · 4.64 KB
/
runprox.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
package grpcproxy
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"aduu.dev/utils/runmanager"
)
// RegisterGRPCService registers a grpc server to proxy through mux.
type RegisterGRPCService func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error
// Runnable proxies a grpc service through http.
type Runnable struct {
Register RegisterGRPCService
FromAddress string
ToAddress string
AllowedOrigins []string
}
// Run runs the given grpc proxy as a runnable service.
func (s *Runnable) Run(m *runmanager.RunManager) {
// Register gRPC server Endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
// Registering _myservice_ and setting to which grpc-Endpoint to connect to.
if err := s.Register(m.Context, mux, s.FromAddress, opts); err != nil {
m.ErrChan <- err
}
server := http.Server{
Addr: s.ToAddress,
// Handler: allowAllCORS(mux),
Handler: s.allowCORSFrom(mux, s.AllowedOrigins),
}
m.Run(&runmanager.Runner{
Run: func() error {
fmt.Println("Running grpc proxy on", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
},
Shutdown: func() error {
fmt.Println("Shutting down grpc proxy")
return server.Shutdown(m.Context)
},
})
}
/*
func (s *SingleFolderAPI) Run(m *RunManager) {
// Register gRPC server Endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
// Registering _myservice_ and setting to which grpc-Endpoint to connect to.
if err := myservice.RegisterYourServiceHandlerFromEndpoint(m.Context, mux, s.GrpcServerEndpoint, opts); err != nil {
m.ErrChan <- err
}
server := http.Server{
Addr: s.HTTPProxyAddress(),
// Handler: allowAllCORS(mux),
Handler: s.allowCORSFRomInternalHTTPServer(mux),
}
m.Run(&Runner{
Run: func() error {
fmt.Println("Running grpc proxy on", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
},
Shutdown: func() error {
fmt.Println("Shutting down grpc proxy")
return server.Shutdown(m.Context)
},
})
}
func (s *SingleFolderAPI) allowCORSFRomInternalHTTPServer(h http.Handler) http.Handler {
return s.allowCORSFrom(h, s.HTTPServerAddress())
}
func (s *SingleFolderAPI) allowCORSFrom(h http.Handler, allowedOrigin string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
if origin == allowedOrigin {
preflightHandler(w, r)
return
}
}
}
h.ServeHTTP(w, r)
})
}
*/
func (s *Runnable) allowCORSFrom(h http.Handler, allowedOrigins []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
for _, allowedOrigin := range allowedOrigins {
if origin == allowedOrigin {
preflightHandler(w, r)
return
}
}
}
}
h.ServeHTTP(w, r)
})
}
/*
// allowAllCORS allows Cross Origin Resoruce Sharing from any origin.
// Don't do this without consideration in production systems.
func allowAllCORS(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
preflightHandler(w, r)
return
}
}
h.ServeHTTP(w, r)
})
}
*/
// preflightHandler adds the necessary headers in order to serve
// CORS from any origin using the methods "GET", "HEAD", "POST", "PUT", "DELETE"
// We insist, don't do this without consideration in production systems.
func preflightHandler(w http.ResponseWriter, r *http.Request) {
headers := []string{"Content-Type", "Accept", "Authorization"}
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
glog.Infof("preflight request for %s", r.URL.Path)
}