-
Notifications
You must be signed in to change notification settings - Fork 116
/
shortlink.go
79 lines (63 loc) · 1.74 KB
/
shortlink.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
package shortlink
import (
"context"
"errors"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
shortlinkv1 "github.com/lyft/clutch/backend/api/shortlink/v1"
"github.com/lyft/clutch/backend/module"
"github.com/lyft/clutch/backend/service"
"github.com/lyft/clutch/backend/service/shortlink"
)
const (
Name = "clutch.module.shortlink"
)
func New(*any.Any, *zap.Logger, tally.Scope) (module.Module, error) {
client, ok := service.Registry["clutch.service.shortlink"]
if !ok {
return nil, errors.New("could not find shortlink service")
}
svc, ok := client.(shortlink.Service)
if !ok {
return nil, errors.New("service was not the correct type")
}
mod := &mod{
shortlink: newShortlinkAPI(svc),
}
return mod, nil
}
type mod struct {
shortlink shortlinkv1.ShortlinkAPIServer
}
func (m *mod) Register(r module.Registrar) error {
shortlinkv1.RegisterShortlinkAPIServer(r.GRPCServer(), m.shortlink)
return r.RegisterJSONGateway(shortlinkv1.RegisterShortlinkAPIHandler)
}
type shortlinkAPI struct {
shortlink shortlink.Service
}
func newShortlinkAPI(svc shortlink.Service) shortlinkv1.ShortlinkAPIServer {
return &shortlinkAPI{
shortlink: svc,
}
}
func (s *shortlinkAPI) Create(ctx context.Context, req *shortlinkv1.CreateRequest) (*shortlinkv1.CreateResponse, error) {
hash, err := s.shortlink.Create(ctx, req.Path, req.State)
if err != nil {
return nil, err
}
return &shortlinkv1.CreateResponse{
Hash: hash,
}, nil
}
func (s *shortlinkAPI) Get(ctx context.Context, req *shortlinkv1.GetRequest) (*shortlinkv1.GetResponse, error) {
path, state, err := s.shortlink.Get(ctx, req.Hash)
if err != nil {
return nil, err
}
return &shortlinkv1.GetResponse{
Path: path,
State: state,
}, nil
}