forked from yarpc/yarpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
154 lines (133 loc) · 4.99 KB
/
server.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"path/filepath"
"go.uber.org/thriftrw/plugin"
)
const serverTemplate = `
// Code generated by thriftrw-plugin-yarpc
// @generated
<$pkgname := printf "%sserver" (lower .Name)>
package <$pkgname>
<$thrift := import "go.uber.org/yarpc/encoding/thrift">
<$transport := import "go.uber.org/yarpc/api/transport">
<$contextImportPath := .ContextImportPath>
<$onewayWrapperImport := .OnewayWrapperImport>
<$onewayWrapperFunc := .OnewayWrapperFunc>
<$unaryWrapperImport := .UnaryWrapperImport>
<$unaryWrapperFunc := .UnaryWrapperFunc>
</* Note that we import things like "context" inside loops rather than at the
top-level because they will end up unused if the service does not have any
functions.
*/>
// Interface is the server-side interface for the <.Name> service.
type Interface interface {
<if .Parent><import .ParentServerPackagePath>.Interface
<end>
<range .Functions>
<$context := import $contextImportPath>
<.Name>(
ctx <$context>.Context, <range .Arguments>
<.Name> <formatType .Type>,<end>
)<if .OneWay> error
<else if .ReturnType> (<formatType .ReturnType>, error)
<else> error
<end>
<end>
}
<$module := .Module>
// New prepares an implementation of the <.Name> service for
// registration.
//
// handler := <.Name>Handler{}
// dispatcher.Register(<$pkgname>.New(handler))
func New(impl Interface, opts ...<$thrift>.RegisterOption) []<$transport>.Procedure {
<if .Functions>h := handler{impl}<end>
service := <$thrift>.Service{
Name: "<.Name>",
Methods: []<$thrift>.Method{
<range .Functions>
<$thrift>.Method{
Name: "<.ThriftName>",
HandlerSpec: <$thrift>.HandlerSpec{
<if .OneWay>
Type: <$transport>.Oneway,
Oneway: <import $onewayWrapperImport>.<$onewayWrapperFunc>(h.<.Name>),
<else>
Type: <$transport>.Unary,
Unary: <import $unaryWrapperImport>.<$unaryWrapperFunc>(h.<.Name>),
<end>
},
Signature: "<.Name>(<range $i, $v := .Arguments><if ne $i 0>, <end><.Name> <formatType .Type><end>)<if not .OneWay | and .ReturnType> (<formatType .ReturnType>)<end>",
ThriftModule: <import $module.ImportPath>.ThriftModule,
},
<end>},
}
procedures := make([]<$transport>.Procedure, 0, <len .Functions>)
<if .Parent> procedures = append(procedures, <import .ParentServerPackagePath>.New(impl, opts...)...)
<end> procedures = append(procedures, <$thrift>.BuildProcedures(service, opts...)...)
return procedures
}
type handler struct{ impl Interface }
<$service := .>
<$module := .Module>
<range .Functions>
<$context := import $contextImportPath>
<$prefix := printf "%s.%s_%s_" (import $module.ImportPath) $service.Name .Name>
<$wire := import "go.uber.org/thriftrw/wire">
<if .OneWay>
func (h handler) <.Name>(ctx <$context>.Context, body <$wire>.Value) error {
var args <$prefix>Args
if err := args.FromWire(body); err != nil {
return err
}
return h.impl.<.Name>(ctx, <range .Arguments>args.<.Name>,<end>)
}
<else>
func (h handler) <.Name>(ctx <$context>.Context, body <$wire>.Value) (<$thrift>.Response, error) {
var args <$prefix>Args
if err := args.FromWire(body); err != nil {
return <$thrift>.Response{}, err
}
<if .ReturnType>
success, err := h.impl.<.Name>(ctx, <range .Arguments>args.<.Name>,<end>)
<else>
err := h.impl.<.Name>(ctx, <range .Arguments>args.<.Name>,<end>)
<end>
hadError := err != nil
result, err := <$prefix>Helper.WrapResponse(<if .ReturnType>success,<end> err)
var response <$thrift>.Response
if err == nil {
response.IsApplicationError = hadError
response.Body = result
}
return response, err
}
<end>
<end>
`
func serverGenerator(data *templateData, files map[string][]byte) (err error) {
packageName := filepath.Base(data.ServerPackagePath())
// kv.thrift => .../kv/keyvalueserver/server.go
path := filepath.Join(data.Module.Directory, packageName, "server.go")
files[path], err = plugin.GoFileFromTemplate(path, serverTemplate, data, templateOptions...)
return
}