-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.go
96 lines (75 loc) · 2.73 KB
/
template.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
package main
import (
"text/template"
)
var fileTemplate = template.Must(template.New("file").Parse(`// Generated by protoc-gen-gokit DO NOT EDIT.
package {{.Package}}
import (
"net/http"
"errors"
"strings"
"io/ioutil"
"golang.org/x/net/context"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/go-kit/kit/endpoint"
"github.com/AmandaCameron/protoc-gen-gokit/runtime"
{{ range $k, $pkg := .Imports }}
. "{{ $pkg }}"{{ end }}
)
var _ = ioutil.ReadAll
{{ range $svc := .Services }}
// MakeMux_{{$svc.GoName}} creates a server mux for the {{ $svc.GoName }} service,
// using the passed kithttp.Server as a template for the parameters of the endpoints.
func MakeMux_{{ $svc.GoName }}(cli {{ $svc.GoName }}Client, mw endpoint.Middleware, responseEncoder kithttp.EncodeResponseFunc, options ...kithttp.ServerOption) (http.Handler, error) {
ret := runtime.NewMux()
{{ range $endp := $svc.Methods }}
ret.AddEndpoint("{{ $endp.Method }}", "{{ $endp.Path }}", kithttp.NewServer(
context.Background(),
mw(MakeEndpoint_{{ $svc.GoName }}_{{ $endp.GoName }}(cli)),
Decode_{{ $svc.GoName }}_{{ $endp.GoName }},
responseEncoder, options...)){{end}}
return ret, nil
}
{{ range $method := $svc.Methods }}
// Decode_{{ $svc.GoName }}_{{ $method.GoName }} decodes an http.Request into a {{ $method.GoInputType }}.
func Decode_{{ $svc.GoName }}_{{ $method.GoName }}(ctx context.Context, req *http.Request) (interface{}, error) {
var ret {{ $method.GoInputType }}
qry := req.URL.Query()
_ = qry
{{ if $method.Body }}
if buff, err := ioutil.ReadAll(req.Body); err == nil {
if err := runtime.Decode(ctx, &ret.{{ $method.GoBodyName }}, string(buff)); err != nil {
return nil, err
}
}
{{- end }}
{{- range $i, $field := $method.Input.Fields }}
if val := qry.Get("{{ $field.ProtoName }}"); val != "" {
if err := runtime.Decode(ctx, &ret.{{ $field.GoName }}, val); err != nil {
return nil, err
}
}
{{- end }}
parts := strings.Split(req.URL.Path, "/")
if len(parts) < {{ len $method.PathArgs }} {
return nil, errors.New("Missing Parameters.")
}
{{- range $i, $field := $method.PathArgs }}
{{- if len $field.GoName }}
if err := runtime.Decode(ctx, &ret.{{ $field.GoName}}, parts[{{ $i }}]); err != nil {
return nil, err
}
{{- end }}
{{- end }}
return &ret, nil
}
// MakeEndpoint_{{ $svc.GoName }}_{{ $method.GoName }} creates an endpoint function for Go-kit
// that runs the specified service / endpoint on the specified grpc endpoint.
func MakeEndpoint_{{ $svc.GoName }}_{{ $method.GoName }}(cli {{ $svc.GoName }}Client) endpoint.Endpoint {
endp := func (ctx context.Context, inp interface{}) (interface{}, error) {
return cli.{{ $method.GoName }}(ctx, inp.(*{{ $method.GoInputType }}))
}
return endp
}
{{ end -}}
{{ end }}`))