-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ridge.go
284 lines (254 loc) · 7.23 KB
/
ridge.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package ridge
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"log"
"mime"
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
proxyproto "github.com/pires/go-proxyproto"
)
// ProxyProtocol is a flag to support PROXY Protocol
var ProxyProtocol bool
// TextMimeTypes is a list of identified as text.
var TextMimeTypes = []string{"image/svg+xml", "application/json", "application/xml"}
// DefaultContentType is a default content-type when missing in response.
var DefaultContentType = "text/plain; charset=utf-8"
// Response represents a response for API Gateway proxy integration.
type Response struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
MultiValueHeaders http.Header `json:"multiValueHeaders"`
Cookies []string `json:"cookies"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded"`
}
// WriteTo writes response to http.ResponseWriter.
func (r *Response) WriteTo(w http.ResponseWriter) (int64, error) {
for k, vs := range r.MultiValueHeaders {
for _, v := range vs {
w.Header().Add(k, v)
}
}
for _, c := range r.Cookies {
w.Header().Add("Set-Cookie", c)
}
w.WriteHeader(r.StatusCode)
if r.IsBase64Encoded {
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(r.Body))
return io.Copy(w, dec)
}
n, err := io.WriteString(w, r.Body)
return int64(n), err
}
// NewResponseWriter creates ResponseWriter
func NewResponseWriter() *ResponseWriter {
w := &ResponseWriter{
Buffer: bytes.Buffer{},
statusCode: http.StatusOK,
header: make(http.Header),
}
return w
}
// ResponseWriter represents a response writer implements http.ResponseWriter.
type ResponseWriter struct {
bytes.Buffer
header http.Header
statusCode int
}
func (w *ResponseWriter) Header() http.Header {
return w.header
}
func (w *ResponseWriter) WriteHeader(code int) {
w.statusCode = code
}
func (w *ResponseWriter) Response() Response {
body := w.String()
isBase64Encoded := false
if t := w.header.Get("Content-Type"); t == "" {
w.header.Set("Content-Type", DefaultContentType)
}
h := make(map[string]string, len(w.header))
for key := range w.header {
v := w.header.Get(key)
if isBinary(key, v) {
isBase64Encoded = true
}
h[key] = v
}
if isBase64Encoded {
body = base64.StdEncoding.EncodeToString(w.Bytes())
}
return Response{
StatusCode: w.statusCode,
Headers: h,
MultiValueHeaders: w.header,
Cookies: w.header.Values("Set-Cookie"),
Body: body,
IsBase64Encoded: isBase64Encoded,
}
}
func isBinary(k, v string) bool {
if k == "Content-Type" && !isTextMime(v) {
return true
}
if k == "Content-Encoding" && v == "gzip" {
return true
}
return false
}
func isTextMime(kind string) bool {
mt, _, err := mime.ParseMediaType(kind)
if err != nil {
return false
}
if strings.HasPrefix(mt, "text/") {
return true
}
isText := false
for _, tmt := range TextMimeTypes {
if mt == tmt {
isText = true
break
}
}
return isText
}
// Run runs http handler on AWS Lambda runtime or net/http's server.
func Run(address, prefix string, mux http.Handler) {
RunWithContext(context.Background(), address, prefix, mux)
}
// RunWithContext runs http handler on AWS Lambda runtime or net/http's server with context.
func RunWithContext(ctx context.Context, address, prefix string, mux http.Handler) {
r := New(address, prefix, mux)
r.RunWithContext(ctx)
}
// Ridge is a struct to run http handler on AWS Lambda runtime or net/http's server.
type Ridge struct {
Address string
Prefix string
Mux http.Handler
RequestBuilder func(json.RawMessage) (*http.Request, error)
TermHandler func()
ProxyProtocol bool
}
// New creates a new Ridge.
func New(address, prefix string, mux http.Handler) *Ridge {
return &Ridge{
Address: address,
Prefix: prefix,
Mux: mux,
RequestBuilder: NewRequest,
ProxyProtocol: ProxyProtocol,
}
}
// Run runs http handler on AWS Lambda runtime or net/http's server.
func (r *Ridge) Run() {
r.RunWithContext(context.Background())
}
// RunWithContext runs http handler on AWS Lambda runtime or net/http's server with context.
func (r *Ridge) RunWithContext(ctx context.Context) {
if AsLambdaHandler() {
r.runAsLambdaHandler(ctx)
} else {
// If it is not running on the AWS Lambda runtime or running as a Lambda extension,
// runs a net/http server.
r.runOnNetHTTPServer(ctx)
}
}
// OnLambdaRuntime returns true if running on AWS Lambda runtime
// - AWS_EXECUTION_ENV is set on AWS Lambda runtime (go1.x)
// - AWS_LAMBDA_RUNTIME_API is set on custom runtime (provided.*)
func OnLambdaRuntime() bool {
return (strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda") || os.Getenv("AWS_LAMBDA_RUNTIME_API") != "")
}
// AsLambdaExtension returns true if running on AWS Lambda runtime and run as a Lambda extension
func AsLambdaExtension() bool {
return OnLambdaRuntime() && os.Getenv("_HANDLER") == ""
}
// AsLambdaHandler returns true if running on AWS Lambda runtime and run as a Lambda handler
func AsLambdaHandler() bool {
return OnLambdaRuntime() && os.Getenv("_HANDLER") != ""
}
func (r *Ridge) mountMux() http.Handler {
m := http.NewServeMux()
switch {
case r.Prefix == "/", r.Prefix == "":
m.Handle("/", r.Mux)
case !strings.HasSuffix(r.Prefix, "/"):
m.Handle(r.Prefix+"/", http.StripPrefix(r.Prefix, r.Mux))
default:
m.Handle(r.Prefix, http.StripPrefix(strings.TrimSuffix(r.Prefix, "/"), r.Mux))
}
return m
}
func (r *Ridge) runAsLambdaHandler(ctx context.Context) {
handler := func(ctx context.Context, event json.RawMessage) (interface{}, error) {
req, err := r.RequestBuilder(event)
if err != nil {
log.Println(err)
return nil, err
}
if lc, ok := lambdacontext.FromContext(ctx); ok {
req.Header.Set("Lambda-Runtime-Aws-Request-Id", lc.AwsRequestID)
req.Header.Set("Lambda-Runtime-Invoked-Function-Arn", lc.InvokedFunctionArn)
}
w := NewResponseWriter()
r.mountMux().ServeHTTP(w, req.WithContext(ctx))
return w.Response(), nil
}
opts := []lambda.Option{lambda.WithContext(ctx)}
if r.TermHandler != nil {
opts = append(opts, lambda.WithEnableSIGTERM(r.TermHandler))
}
lambda.StartWithOptions(handler, opts...)
}
func (r *Ridge) runOnNetHTTPServer(ctx context.Context) {
log.Println("starting up with local httpd", r.Address)
listener, err := net.Listen("tcp", r.Address)
if err != nil {
log.Fatalf("couldn't listen to %s: %s", r.Address, err.Error())
}
if r.ProxyProtocol {
log.Println("enables to PROXY protocol")
listener = &proxyproto.Listener{Listener: listener}
}
srv := http.Server{Handler: r.mountMux()}
var wg sync.WaitGroup
wg.Add(3)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
go func() {
select {
case <-ch:
case <-ctx.Done():
}
if r.TermHandler != nil {
r.TermHandler()
}
wg.Done()
}()
go func() {
defer wg.Done()
<-ctx.Done()
log.Println("shutting down local httpd", r.Address)
srv.Shutdown(ctx)
}()
if err := srv.Serve(listener); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
wg.Done()
}
wg.Wait()
}