-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
270 lines (221 loc) · 5.66 KB
/
examples_test.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
package espresso_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"github.com/googollee/go-espresso"
)
func ExampleEspresso() {
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
}
type Books map[int]Book
books := make(Books)
books[1] = Book{
ID: 1,
Title: "The Espresso Book",
}
books[2] = Book{
ID: 2,
Title: "The Second Book",
}
espo := espresso.New()
// Log to stdout for Output
espo.AddModule(espresso.LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}
opt := slog.HandlerOptions{
ReplaceAttr: removeTime,
}
return slog.New(slog.NewTextHandler(os.Stdout, &opt)), nil
}))
espo.AddModule(espresso.ProvideCodecs)
router := espo.WithPrefix("/http")
router.HandleFunc(func(ctx espresso.Context) error {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return err
}
book, ok := books[id]
if !ok {
return espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}
if err := espresso.CodecsModule.Value(ctx).EncodeResponse(ctx, &book); err != nil {
return err
}
return nil
})
router.HandleFunc(func(ctx espresso.Context) error {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return err
}
codecs := espresso.CodecsModule.Value(ctx)
var book Book
if err := codecs.DecodeRequest(ctx, &book); err != nil {
return espresso.Error(http.StatusBadRequest, err)
}
book.ID = len(books)
books[book.ID] = book
if err := codecs.EncodeResponse(ctx, &book); err != nil {
return err
}
return nil
})
svr := httptest.NewServer(espo)
defer svr.Close()
func() {
var book Book
resp, err := http.Get(svr.URL + "/http/book/1")
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&book); err != nil {
panic(err)
}
fmt.Println("Book 1 title:", book.Title)
}()
func() {
arg := Book{Title: "The New Book"}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&arg); err != nil {
panic(err)
}
resp, err := http.Post(svr.URL+"/http/book", "application/json", &buf)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(resp.Status)
}
var ret Book
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
panic(err)
}
fmt.Println("The New Book id:", ret.ID)
}()
// Output:
// level=INFO msg="receive http" method=GET path=/http/book/1
// level=INFO msg="finish http" method=GET path=/http/book/1
// Book 1 title: The Espresso Book
// level=INFO msg="receive http" method=POST path=/http/book
// level=INFO msg="finish http" method=POST path=/http/book
// The New Book id: 2
}
func ExampleEspresso_rpc() {
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
}
type Books map[int]Book
books := make(Books)
books[1] = Book{
ID: 1,
Title: "The Espresso Book",
}
books[2] = Book{
ID: 2,
Title: "The Second Book",
}
espo := espresso.New()
// Log to stdout for Output
espo.AddModule(espresso.LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}
opt := slog.HandlerOptions{
ReplaceAttr: removeTime,
}
return slog.New(slog.NewTextHandler(os.Stdout, &opt)), nil
}))
espo.AddModule(espresso.ProvideCodecs)
router := espo.WithPrefix("/rpc")
router.HandleFunc(espresso.RPCRetrive(func(ctx espresso.Context) (*Book, error) {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return nil, err
}
book, ok := books[id]
if !ok {
return nil, espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}
return &book, nil
}))
router.HandleFunc(espresso.RPC(func(ctx espresso.Context, book *Book) (*Book, error) {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return nil, err
}
book.ID = len(books)
books[book.ID] = *book
return book, nil
}))
svr := httptest.NewServer(espo)
defer svr.Close()
func() {
var book Book
resp, err := http.Get(svr.URL + "/rpc/book/1")
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&book); err != nil {
panic(err)
}
fmt.Println("Book 1 title:", book.Title)
}()
func() {
arg := Book{Title: "The New Book"}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&arg); err != nil {
panic(err)
}
resp, err := http.Post(svr.URL+"/rpc/book", "application/json", &buf)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(resp.Status)
}
var ret Book
if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
panic(err)
}
fmt.Println("The New Book id:", ret.ID)
}()
// Output:
// level=INFO msg="receive http" method=GET path=/rpc/book/1
// level=INFO msg="finish http" method=GET path=/rpc/book/1
// Book 1 title: The Espresso Book
// level=INFO msg="receive http" method=POST path=/rpc/book
// level=INFO msg="finish http" method=POST path=/rpc/book
// The New Book id: 2
}