-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
54 lines (45 loc) · 1.13 KB
/
response.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
package golamb
import (
"encoding/json"
"net/http"
"github.com/aws/aws-lambda-go/events"
)
// Responder responds to the request.
type Responder interface {
// Respond responds to the http request.
Respond() (*events.APIGatewayV2HTTPResponse, error)
// SetHeader sets a response header with the given key and value.
SetHeader(key string, value string) Responder
// SetCookie sets a response cookie with the given key and value.
SetCookie(cookie *http.Cookie) Responder
}
type response struct {
status int
body any
headers map[string]string
cookies []string
}
func (r *response) Respond() (*events.APIGatewayV2HTTPResponse, error) {
var body string
if r.body != nil {
b, err := json.Marshal(r.body)
if err != nil {
return nil, err
}
body = string(b)
}
return &events.APIGatewayV2HTTPResponse{
StatusCode: r.status,
Body: body,
Headers: r.headers,
Cookies: r.cookies,
}, nil
}
func (r *response) SetHeader(key string, value string) Responder {
r.headers[key] = value
return r
}
func (r *response) SetCookie(cookie *http.Cookie) Responder {
r.cookies = append(r.cookies, cookie.String())
return r
}