-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (57 loc) · 1.67 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"github.com/micro/go-micro"
pb "github.com/oentoro/vessel-service/proto/vessel"
)
//Repository - interface for repository
type Repository interface {
FindAvailable(*pb.Specification) (*pb.Vessel, error)
}
//VesselRepository - vessel repository object
type VesselRepository struct {
vessels []*pb.Vessel
}
// FindAvailable - checks a specification against a map of vessels,
// if capacity and max weight are below a vessels capacity and max weight,
// then return that vessel.
func (repo *VesselRepository) FindAvailable(spec *pb.Specification) (*pb.Vessel, error) {
for _, vessel := range repo.vessels {
if spec.Capacity <= vessel.Capacity && spec.MaxWeight <= vessel.MaxWeight {
return vessel, nil
}
}
return nil, errors.New("No vessel found by that spec")
}
// Our grpc service handler
type service struct {
repo Repository
}
func (s *service) FindAvailable(ctx context.Context, req *pb.Specification, res *pb.Response) error {
// Find the next available vessel
vessel, err := s.repo.FindAvailable(req)
if err != nil {
return err
}
// Set the vessel as part of the response message type
res.Vessel = vessel
return nil
}
func main() {
vessels := []*pb.Vessel{
&pb.Vessel{Id: "vessel001", Name: "Kane's Salty Secret", MaxWeight: 200000, Capacity: 500},
}
repo := &VesselRepository{vessels}
srv := micro.NewService(
micro.Name("vessel.service"),
micro.Version("latest"),
)
srv.Init()
// Register our implementation with
pb.RegisterVesselServiceHandler(srv.Server(), &service{repo})
if err := srv.Run(); err != nil {
fmt.Println(err)
}
}