-
Notifications
You must be signed in to change notification settings - Fork 17
/
http.go
58 lines (46 loc) · 1.42 KB
/
http.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
// Code generated by kun; DO NOT EDIT.
// github.com/RussellLuo/kun
package helloworld
import (
"context"
"net/http"
"github.com/RussellLuo/kun/pkg/httpcodec"
"github.com/RussellLuo/kun/pkg/httpoption"
"github.com/RussellLuo/kun/pkg/oas2"
"github.com/go-chi/chi"
kithttp "github.com/go-kit/kit/transport/http"
)
func NewHTTPRouter(svc Service, codecs httpcodec.Codecs, opts ...httpoption.Option) chi.Router {
r := chi.NewRouter()
options := httpoption.NewOptions(opts...)
r.Method("GET", "/api", oas2.Handler(OASv2APIDoc, options.ResponseSchema()))
var codec httpcodec.Codec
var validator httpoption.Validator
var kitOptions []kithttp.ServerOption
codec = codecs.EncodeDecoder("SayHello")
validator = options.RequestValidator("SayHello")
r.Method(
"POST", "/messages",
kithttp.NewServer(
MakeEndpointOfSayHello(svc),
decodeSayHelloRequest(codec, validator),
httpcodec.MakeResponseEncoder(codec, 200),
append(kitOptions,
kithttp.ServerErrorEncoder(httpcodec.MakeErrorEncoder(codec)),
)...,
),
)
return r
}
func decodeSayHelloRequest(codec httpcodec.Codec, validator httpoption.Validator) kithttp.DecodeRequestFunc {
return func(_ context.Context, r *http.Request) (interface{}, error) {
var _req SayHelloRequest
if err := codec.DecodeRequestBody(r, &_req); err != nil {
return nil, err
}
if err := validator.Validate(&_req); err != nil {
return nil, err
}
return &_req, nil
}
}