forked from pasztorpisti/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvc.go
44 lines (37 loc) · 915 Bytes
/
svc.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
package svc3
import (
"github.com/pasztorpisti/nano"
"github.com/pasztorpisti/nano/addons/util"
"github.com/pasztorpisti/nano/examples/example1/api_go/svc3"
"github.com/pasztorpisti/nano/examples/example1/api_go/svc4"
)
func New() nano.Service {
return &svc{}
}
type svc struct {
svc4 nano.Client
}
func (*svc) Name() string {
return "svc3"
}
func (p *svc) Init(cs nano.ClientSet) error {
p.svc4 = cs.LookupClient("svc4")
return nil
}
func (p *svc) Handle(c *nano.Ctx, req interface{}) (interface{}, error) {
switch r := req.(type) {
case *svc3.Req:
return p.handleReq(c, r)
default:
return nil, nano.BadReqTypeError
}
}
func (p *svc) handleReq(c *nano.Ctx, r *svc3.Req) (interface{}, error) {
resp, err := p.svc4.Request(c, &svc4.Req{Param: r.Param})
if err != nil {
return nil, util.Err(err, "svc4 failure")
}
return &svc3.Resp{
Value: "svc3_" + resp.(*svc4.Resp).Value,
}, nil
}