-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_apigateway.go
71 lines (54 loc) · 1.48 KB
/
lambda_apigateway.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
package server
import (
"context"
"fmt"
_ "log"
"net/http"
"net/url"
"github.com/akrylysov/algnhsa"
)
func init() {
ctx := context.Background()
RegisterServer(ctx, "lambda", NewLambdaServer)
}
// LambdaServer implements the `Server` interface for a use in a AWS Lambda + API Gateway context.
type LambdaServer struct {
Server
url *url.URL
binary_types []string
}
// NewLambdaServer returns a new `LambdaServer` instance configured by 'uri' which is
// expected to be defined in the form of:
//
// lambda://?{PARAMETERS}
//
// Valid parameters are:
// * `binary_type={MIMETYPE}` One or more mimetypes to be served by AWS API Gateway as binary content types.
func NewLambdaServer(ctx context.Context, uri string) (Server, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}
server := LambdaServer{
url: u,
}
q := u.Query()
binary_types, ok := q["binary_type"]
if ok {
server.binary_types = binary_types
}
return &server, nil
}
// Address returns the fully-qualified URL used to instantiate 's'.
func (s *LambdaServer) Address() string {
return s.url.String()
}
// ListenAndServe starts the serve and listens for requests using 'mux' for routing.
func (s *LambdaServer) ListenAndServe(ctx context.Context, mux http.Handler) error {
lambda_opts := new(algnhsa.Options)
if len(s.binary_types) > 0 {
lambda_opts.BinaryContentTypes = s.binary_types
}
algnhsa.ListenAndServe(mux, lambda_opts)
return nil
}