@@ -156,12 +156,20 @@ func HTTP(fns ...func()) {
156
156
case * expr.APIExpr :
157
157
eval .Execute (fn , expr .Root )
158
158
case * expr.ServiceExpr :
159
- res := expr .Root .API .HTTP .ServiceFor (actual )
160
- res .DSLFunc = fn
159
+ svc := expr .Root .API .HTTP .ServiceFor (actual )
160
+ if existing := svc .DSLFunc ; existing != nil {
161
+ svc .DSLFunc = func () { existing (); fn () }
162
+ } else {
163
+ svc .DSLFunc = fn
164
+ }
161
165
case * expr.MethodExpr :
162
- res := expr .Root .API .HTTP .ServiceFor (actual .Service )
163
- act := res .EndpointFor (actual .Name , actual )
164
- act .DSLFunc = fn
166
+ svc := expr .Root .API .HTTP .ServiceFor (actual .Service )
167
+ e := svc .EndpointFor (actual )
168
+ if existing := e .DSLFunc ; existing != nil {
169
+ e .DSLFunc = func () { existing (); fn () }
170
+ } else {
171
+ e .DSLFunc = fn
172
+ }
165
173
default :
166
174
eval .IncompatibleDSL ()
167
175
}
@@ -235,8 +243,7 @@ func Produces(args ...string) {
235
243
// As a special case, if you want to generate a path with a trailing slash, you can use
236
244
// GET("/./") to generate a path such as '/foo/'.
237
245
//
238
- // Path must appear in an API HTTP expression, a Service HTTP expression, or
239
- // a Service JSONRPC expression.
246
+ // Path must appear in an API HTTP expression or a Service HTTP expression.
240
247
//
241
248
// Path accepts one argument: the HTTP path prefix.
242
249
//
@@ -253,12 +260,6 @@ func Produces(args ...string) {
253
260
// Path("/{accountID}")
254
261
// })
255
262
// })
256
- //
257
- // var _ = Service("rpc", func() {
258
- // JSONRPC(func() {
259
- // Path("/rpc") // JSON-RPC endpoint path
260
- // })
261
- // })
262
263
func Path (val string ) {
263
264
switch def := eval .Current ().(type ) {
264
265
case * expr.RootExpr :
@@ -357,30 +358,32 @@ func PATCH(path string) *expr.RouteExpr {
357
358
358
359
func route (method , path string ) * expr.RouteExpr {
359
360
r := & expr.RouteExpr {Method : method , Path : path }
360
-
361
361
switch e := eval .Current ().(type ) {
362
362
case * expr.HTTPEndpointExpr :
363
- r .Endpoint = e
363
+ r .Parent = e
364
+ e .Routes = append (e .Routes , r )
365
+ case * expr.JSONRPCServiceExpr :
366
+ r .Parent = e
364
367
e .Routes = append (e .Routes , r )
365
368
default :
366
369
eval .IncompatibleDSL ()
367
370
}
368
-
371
+
369
372
return r
370
373
}
371
374
372
375
// Header describes a single HTTP header or gRPC metadata header. The properties
373
376
// (description, type, validation etc.) of a header are inherited from the
374
377
// request or response type attribute with the same name by default.
375
378
//
376
- // Header must appear in the API HTTP expression (to define request headers
377
- // common to all the API endpoints), a service HTTP expression (to define
378
- // request headers common to all the service endpoints) a specific method HTTP
379
- // expression (to define request headers) or a Response expression (to define
380
- // the response headers). Header may also appear in a method GRPC expression (to
381
- // define headers sent in message metadata), or in a Response expression (to
382
- // define headers sent in result metadata). Finally Header may also appear in a
383
- // Headers expression.
379
+ // Header must appear in the API HTTP or JSONRPC expression (to define request
380
+ // headers common to all the API endpoints), a service HTTP or JSONRPC
381
+ // expression (to define request headers common to all the service endpoints) a
382
+ // specific method HTTP or JSONRPC expression (to define request headers) or a
383
+ // Response expression (to define the response headers). Header may also appear
384
+ // in a method GRPC expression (to define headers sent in message metadata), or
385
+ // in a Response expression (to define headers sent in result metadata). Finally
386
+ // Header may also appear in a Headers expression.
384
387
//
385
388
// Header accepts the same arguments as the Attribute function. The header name
386
389
// may define a mapping between the attribute name and the HTTP header name when
@@ -419,11 +422,11 @@ func Header(name string, args ...any) {
419
422
// Cookie identifies a HTTP cookie. When used within a Response the Cookie DSL
420
423
// also makes it possible to define the cookie attributes.
421
424
//
422
- // Cookie must appear in the API HTTP expression (to define request cookies
423
- // common to all the API endpoints), a service HTTP expression (to define
424
- // request cookies common to all the service endpoints) a specific method HTTP
425
- // expression (to define request cookies) or a Response expression (to define
426
- // the response cookies).
425
+ // Cookie must appear in the API HTTP or JSONRPC expression (to define request
426
+ // cookies common to all the API endpoints), a service HTTP or JSONRPC
427
+ // expression (to define request cookies common to all the service endpoints) a
428
+ // specific method HTTP or JSONRPC expression (to define request cookies) or a
429
+ // Response expression (to define the response cookies).
427
430
//
428
431
// Cookie accepts the same arguments as the Attribute function. The cookie name
429
432
// may define a mapping between the attribute name and the cookie name. The
@@ -1154,6 +1157,21 @@ func headers(exp eval.Expression) *expr.MappedAttributeExpr {
1154
1157
e .Headers = expr .NewEmptyMappedAttributeExpr ()
1155
1158
}
1156
1159
return e .Headers
1160
+ case * expr.JSONRPCServiceExpr :
1161
+ if e .Headers == nil {
1162
+ e .Headers = expr .NewEmptyMappedAttributeExpr ()
1163
+ }
1164
+ return e .Headers
1165
+ case * expr.JSONRPCEndpointExpr :
1166
+ if e .Headers == nil {
1167
+ e .Headers = expr .NewEmptyMappedAttributeExpr ()
1168
+ }
1169
+ return e .Headers
1170
+ case * expr.JSONRPCResponseExpr :
1171
+ if e .Headers == nil {
1172
+ e .Headers = expr .NewEmptyMappedAttributeExpr ()
1173
+ }
1174
+ return e .Headers
1157
1175
case * expr.MappedAttributeExpr :
1158
1176
return e
1159
1177
default :
@@ -1185,6 +1203,21 @@ func cookies(exp eval.Expression) *expr.MappedAttributeExpr {
1185
1203
e .Cookies = expr .NewEmptyMappedAttributeExpr ()
1186
1204
}
1187
1205
return e .Cookies
1206
+ case * expr.JSONRPCServiceExpr :
1207
+ if e .Cookies == nil {
1208
+ e .Cookies = expr .NewEmptyMappedAttributeExpr ()
1209
+ }
1210
+ return e .Cookies
1211
+ case * expr.JSONRPCEndpointExpr :
1212
+ if e .Cookies == nil {
1213
+ e .Cookies = expr .NewEmptyMappedAttributeExpr ()
1214
+ }
1215
+ return e .Cookies
1216
+ case * expr.JSONRPCResponseExpr :
1217
+ if e .Cookies == nil {
1218
+ e .Cookies = expr .NewEmptyMappedAttributeExpr ()
1219
+ }
1220
+ return e .Cookies
1188
1221
case * expr.MappedAttributeExpr :
1189
1222
return e
1190
1223
default :
0 commit comments