-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.tpl
76 lines (70 loc) · 1.89 KB
/
template.tpl
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
{{$svrType := .ServiceType}}
{{$svrName := .ServiceName}}
const (
{{- range .MethodSets}}
Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}"
{{- end}}
)
type {{.ServiceType}}HTTPServer interface {
{{- range .MethodSets}}
{{- if ne .Comment ""}}
{{.Comment}}
{{- end}}
{{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error)
{{- end}}
}
func Register{{.ServiceType}}HTTPServer(app *fiber.App, srv {{.ServiceType}}HTTPServer) {
r := app.Group("/")
{{- range .Methods}}
r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv))
{{- end}}
}
{{range .Methods}}
func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
var in {{.Request}}
{{- if .HasBody}}
if err := c.BodyParser(&in{{.Body}}); err != nil {
return err
}
{{- end}}
if err := c.QueryParser(&in); err != nil {
return err
}
{{- if .HasVars}}
if err := c.ParamsParser(&in); err != nil {
return err
}
{{- end}}
v, err := protovalidate.New(
protovalidate.WithFailFast(true),
)
if err != nil {
return err
}
if err = v.Validate(&in); err != nil {
return err
}
out, err := srv.{{.Name}}(c.Context(), &in)
if err != nil {
return err
}
return c.JSON(fiber.Map{
"code": fiber.StatusOK,
"message": "success",
"data": out,
})
}
}
{{end}}
// Unimplemented{{.ServiceType}}Server must be embedded to have forward compatible implementations.
type Unimplemented{{.ServiceType}}Server struct {
}
{{- range .MethodSets}}
{{- if ne .Comment ""}}
{{.Comment}}
{{- end}}
func (Unimplemented{{$svrType}}Server) {{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) {
return nil, fiber.NewError(fiber.StatusNotImplemented, "method {{.Name}} not implemented")
}
{{- end}}