Skip to content

Commit

Permalink
Merge f6fb2bd into dbdff0b
Browse files Browse the repository at this point in the history
  • Loading branch information
brotherlogic committed Dec 31, 2018
2 parents dbdff0b + f6fb2bd commit 2d48835
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 60 deletions.
132 changes: 74 additions & 58 deletions proto/wantslist.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions proto/wantslist.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ syntax = "proto3";

package wantslist;

message Config {
repeated WantList lists = 1;
}

message WantListEntry {
int32 want = 1;
int32 index = 2;
Expand All @@ -13,7 +17,9 @@ message WantList {
}


message AddWantListRequest {}
message AddWantListRequest {
WantList add = 1;
}

message AddWantListResponse {}

Expand All @@ -23,5 +29,5 @@ message GetWantListResponse {}

service WantService {
rpc AddWantList(AddWantListRequest) returns (AddWantListResponse) {};
rpc GetWantList(GetWantListRequest) returns (GetWantListResponse) {};
//rpc GetWantList(GetWantListRequest) returns (GetWantListResponse) {};
}
29 changes: 29 additions & 0 deletions wantslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ import (
"google.golang.org/grpc"

pbg "github.com/brotherlogic/goserver/proto"
pb "github.com/brotherlogic/wantslist/proto"
)

const (
// KEY - where the wantslists are stored
KEY = "/github.com/brotherlogic/wantslist/config"
)

//Server main server type
type Server struct {
*goserver.GoServer
config *pb.Config
}

// Init builds the server
func Init() *Server {
s := &Server{
&goserver.GoServer{},
&pb.Config{},
}
return s
}
Expand All @@ -36,8 +44,29 @@ func (s *Server) ReportHealth() bool {
return true
}

func (s *Server) save(ctx context.Context) {
s.KSclient.Save(ctx, KEY, s.config)
}

func (s *Server) load(ctx context.Context) error {
config := &pb.Config{}
data, _, err := s.KSclient.Read(ctx, KEY, config)

if err != nil {
return err
}

s.config = data.(*pb.Config)
return nil
}

// Mote promotes/demotes this server
func (s *Server) Mote(ctx context.Context, master bool) error {
if master {
err := s.load(ctx)
return err
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions wantslistapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "golang.org/x/net/context"
import pb "github.com/brotherlogic/wantslist/proto"

//AddWantList adds a want list
func (s *Server) AddWantList(ctx context.Context, req *pb.AddWantListRequest) (*pb.AddWantListResponse, error) {
s.config.Lists = append(s.config.Lists, req.Add)
return &pb.AddWantListResponse{}, nil
}
18 changes: 18 additions & 0 deletions wantslistapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"context"
"testing"

pb "github.com/brotherlogic/wantslist/proto"
)

func TestAddWantsList(t *testing.T) {
s := InitTestServer()

s.AddWantList(context.Background(), &pb.AddWantListRequest{Add: &pb.WantList{Name: "hello"}})

if len(s.config.Lists) != 1 {
t.Errorf("Wrong number of lists: %v", len(s.config.Lists))
}
}

0 comments on commit 2d48835

Please sign in to comment.