-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathexample_test.go
More file actions
102 lines (91 loc) · 2.17 KB
/
example_test.go
File metadata and controls
102 lines (91 loc) · 2.17 KB
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
package lambda_test
import (
"context"
"io"
"log"
"time"
"github.com/aws/aws-lambda-go/lambda"
)
func Example() {
lambda.Start(func() (string, error) {
return "Hello λ!", nil
})
}
// Handlers can return io.Reader to stream response data.
// This example uses a pipe to send data in chunks with delays.
//
// See https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html
func Example_ioReader() {
lambda.Start(func() (io.Reader, error) {
r, w := io.Pipe()
go func() {
defer w.Close()
_, _ = w.Write([]byte("<html><body>"))
time.Sleep(100 * time.Millisecond)
_, _ = w.Write([]byte("<h1>Hello</h1>"))
time.Sleep(100 * time.Millisecond)
_, _ = w.Write([]byte("<p>World!</p>"))
time.Sleep(100 * time.Millisecond)
_, _ = w.Write([]byte("</body></html>"))
}()
return r, nil
})
}
func ExampleWithContext() {
lambda.StartWithOptions(
func(ctx context.Context) (string, error) {
return ctx.Value("foo").(string), nil
},
lambda.WithContext(context.WithValue(context.Background(), "foo", "bar")),
)
}
func ExampleWithContextValue() {
lambda.StartWithOptions(
func(ctx context.Context) (string, error) {
return ctx.Value("foo").(string), nil
},
lambda.WithContextValue("foo", "bar"),
)
}
func ExampleWithSetEscapeHTML() {
lambda.StartWithOptions(
func() (string, error) {
return "<html><body>hello!</body></html>", nil
},
lambda.WithSetEscapeHTML(true),
)
}
func ExampleWithSetIndent() {
lambda.StartWithOptions(
func(event interface{}) (interface{}, error) {
return event, nil
},
lambda.WithSetIndent(">", " "),
)
}
func ExampleWithUseNumber() {
lambda.StartWithOptions(
func(event interface{}) (interface{}, error) {
return event, nil
},
lambda.WithUseNumber(true),
)
}
func ExampleWithDisallowUnknownFields() {
lambda.StartWithOptions(
func(event interface{}) (interface{}, error) {
return event, nil
},
lambda.WithDisallowUnknownFields(true),
)
}
func ExampleWithEnableSIGTERM() {
lambda.StartWithOptions(
func(event interface{}) (interface{}, error) {
return event, nil
},
lambda.WithEnableSIGTERM(func() {
log.Print("function container shutting down...")
}),
)
}