-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_test.go
More file actions
33 lines (28 loc) · 871 Bytes
/
example_test.go
File metadata and controls
33 lines (28 loc) · 871 Bytes
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
package kitty_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/objenious/kitty"
"github.com/objenious/kitty/gorilla"
)
func ExampleServer() {
type fooRequest struct{ Name string }
type fooResponse struct{ Message string }
foo := func(ctx context.Context, request interface{}) (interface{}, error) {
fr := request.(fooRequest)
return fooResponse{Message: fmt.Sprintf("Good morning %s !", fr.Name)}, nil
}
decodeFooRequest := func(ctx context.Context, r *http.Request) (interface{}, error) {
var request fooRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
t := kitty.NewHTTPTransport(kitty.Config{HTTPPort: 8081}).
Router(gorilla.Router()).
Endpoint("POST", "/foo", foo, kitty.Decoder(decodeFooRequest))
kitty.NewServer(t).Run(context.Background())
}