Simplify - remove rove-accountant
This was a fun little gRPC experiment but it's simply not needed
This commit is contained in:
parent
984ff56664
commit
abcebcebb6
12 changed files with 20 additions and 930 deletions
|
@ -1,80 +0,0 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const kAccountsFileName = "rove-accounts.json"
|
||||
|
||||
// Account represents a registered user
|
||||
type Account struct {
|
||||
// Name simply describes the account and must be unique
|
||||
Name string `json:"name"`
|
||||
|
||||
// Data represents internal account data
|
||||
Data map[string]string `json:"data"`
|
||||
}
|
||||
|
||||
// Represents the accountant data to store
|
||||
type accountantData struct {
|
||||
}
|
||||
|
||||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
Accounts map[string]Account `json:"accounts"`
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{
|
||||
Accounts: make(map[string]Account),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterAccount adds an account to the set of internal accounts
|
||||
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
||||
|
||||
// Set the account name
|
||||
acc.Name = name
|
||||
acc.Data = make(map[string]string)
|
||||
|
||||
// Verify this acount isn't already registered
|
||||
for _, a := range a.Accounts {
|
||||
if a.Name == acc.Name {
|
||||
return Account{}, fmt.Errorf("account name already registered: %s", a.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the creation time
|
||||
acc.Data["created"] = time.Now().String()
|
||||
|
||||
// Simply add the account to the map
|
||||
a.Accounts[acc.Name] = acc
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AssignRover assigns data to an account
|
||||
func (a *Accountant) AssignData(account string, key string, value string) error {
|
||||
|
||||
// Find the account matching the ID
|
||||
if this, ok := a.Accounts[account]; ok {
|
||||
this.Data[key] = value
|
||||
a.Accounts[account] = this
|
||||
} else {
|
||||
return fmt.Errorf("no account found for id: %s", account)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRover gets the rover rover for the account
|
||||
func (a *Accountant) GetValue(account string, key string) (string, error) {
|
||||
// Find the account matching the ID
|
||||
if this, ok := a.Accounts[account]; !ok {
|
||||
return "", fmt.Errorf("no account found for id: %s", account)
|
||||
} else {
|
||||
return this.Data[key], nil
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestNewAccountant(t *testing.T) {
|
||||
// Very basic verify here for now
|
||||
accountant := NewAccountant()
|
||||
if accountant == nil {
|
||||
t.Error("Failed to create accountant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||
|
||||
accountant := NewAccountant()
|
||||
|
||||
// Start by making two accounts
|
||||
|
||||
namea := uuid.New().String()
|
||||
acca, err := accountant.RegisterAccount(namea)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if acca.Name != namea {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
|
||||
}
|
||||
|
||||
nameb := uuid.New().String()
|
||||
accb, err := accountant.RegisterAccount(nameb)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if accb.Name != nameb {
|
||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
||||
}
|
||||
|
||||
// Verify another request gets rejected
|
||||
_, err = accountant.RegisterAccount(namea)
|
||||
if err == nil {
|
||||
t.Error("Duplicate account name did not produce error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountant_AssignGetData(t *testing.T) {
|
||||
accountant := NewAccountant()
|
||||
if len(accountant.Accounts) != 0 {
|
||||
t.Error("New accountant created with non-zero account number")
|
||||
}
|
||||
|
||||
name := uuid.New().String()
|
||||
a, err := accountant.RegisterAccount(name)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = accountant.AssignData(a.Name, "key", "value")
|
||||
if err != nil {
|
||||
t.Error("Failed to set data for created account")
|
||||
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
|
||||
t.Error("Failed to get data for account")
|
||||
} else if id != "value" {
|
||||
t.Error("Fetched data is incorrect for account")
|
||||
}
|
||||
}
|
|
@ -1,134 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/mdiluz/rove/cmd/rove-accountant/internal"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/persistence"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var data = os.Getenv("DATA_PATH")
|
||||
|
||||
// accountantServer is the internal object to manage the requests
|
||||
type accountantServer struct {
|
||||
accountant *internal.Accountant
|
||||
sync sync.RWMutex
|
||||
}
|
||||
|
||||
// Register will register an account
|
||||
func (a *accountantServer) Register(ctx context.Context, in *accounts.RegisterInfo) (*accounts.RegisterResponse, error) {
|
||||
a.sync.Lock()
|
||||
defer a.sync.Unlock()
|
||||
|
||||
// Try and register the account itself
|
||||
log.Printf("Registering account: %s\n", in.Name)
|
||||
if _, err := a.accountant.RegisterAccount(in.Name); err != nil {
|
||||
log.Printf("Error: %s\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Save out the accounts
|
||||
if err := persistence.Save("accounts", a.accountant); err != nil {
|
||||
log.Printf("Error: %s\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accounts.RegisterResponse{}, nil
|
||||
}
|
||||
|
||||
// AssignData assigns a key value pair to an account
|
||||
func (a *accountantServer) AssignValue(_ context.Context, in *accounts.DataKeyValue) (*accounts.DataKeyResponse, error) {
|
||||
a.sync.RLock()
|
||||
defer a.sync.RUnlock()
|
||||
|
||||
// Try and assign the data
|
||||
log.Printf("Assigning value for account %s: %s->%s\n", in.Account, in.Key, in.Value)
|
||||
err := a.accountant.AssignData(in.Account, in.Key, in.Value)
|
||||
if err != nil {
|
||||
log.Printf("Error: %s\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accounts.DataKeyResponse{}, nil
|
||||
|
||||
}
|
||||
|
||||
// GetData gets the value for a key
|
||||
func (a *accountantServer) GetValue(_ context.Context, in *accounts.DataKey) (*accounts.DataResponse, error) {
|
||||
a.sync.RLock()
|
||||
defer a.sync.RUnlock()
|
||||
|
||||
// Try and fetch the value
|
||||
data, err := a.accountant.GetValue(in.Account, in.Key)
|
||||
if err != nil {
|
||||
log.Printf("Error: %s\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &accounts.DataResponse{Value: data}, nil
|
||||
|
||||
}
|
||||
|
||||
// main
|
||||
func main() {
|
||||
// Get the port
|
||||
var iport int
|
||||
var port = os.Getenv("PORT")
|
||||
if len(port) == 0 {
|
||||
iport = 9091
|
||||
} else {
|
||||
var err error
|
||||
iport, err = strconv.Atoi(port)
|
||||
if err != nil {
|
||||
log.Fatal("$PORT not valid int")
|
||||
}
|
||||
}
|
||||
|
||||
persistence.SetPath(data)
|
||||
|
||||
// Initialise and load the accountant
|
||||
accountant := internal.NewAccountant()
|
||||
if err := persistence.Load("accounts", accountant); err != nil {
|
||||
log.Fatalf("failed to load account data: %s", err)
|
||||
}
|
||||
|
||||
// Set up the RPC server and register
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", iport))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
grpcServer := grpc.NewServer()
|
||||
accounts.RegisterAccountantServer(grpcServer, &accountantServer{
|
||||
accountant: accountant,
|
||||
})
|
||||
|
||||
// Set up the close handler
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
log.Println("Quit requested, exiting...")
|
||||
grpcServer.Stop()
|
||||
}()
|
||||
|
||||
// Serve the RPC server
|
||||
log.Printf("Serving accountant on %s\n", port)
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve gRPC: %s", err)
|
||||
}
|
||||
|
||||
// Save out the accountant data
|
||||
if err := persistence.Save("accounts", accountant); err != nil {
|
||||
log.Fatalf("failed to save accounts: %s", err)
|
||||
}
|
||||
}
|
|
@ -4,11 +4,9 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/version"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func (s *Server) Status(context.Context, *rove.StatusRequest) (*rove.StatusResponse, error) {
|
||||
|
@ -33,7 +31,7 @@ func (s *Server) Register(ctx context.Context, req *rove.RegisterRequest) (*rove
|
|||
return nil, fmt.Errorf("empty account name")
|
||||
}
|
||||
|
||||
if _, err := s.accountant.Register(ctx, &accounts.RegisterInfo{Name: req.Name}, grpc.WaitForReady(true)); err != nil {
|
||||
if _, err := s.accountant.RegisterAccount(req.Name); err != nil {
|
||||
return nil, err
|
||||
|
||||
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
|
||||
|
@ -51,10 +49,10 @@ func (s *Server) Rover(ctx context.Context, req *rove.RoverRequest) (*rove.Rover
|
|||
if len(req.Account) == 0 {
|
||||
return nil, fmt.Errorf("empty account name")
|
||||
|
||||
} else if resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"}); err != nil {
|
||||
return nil, fmt.Errorf("gRPC failed to contact accountant: %s", err)
|
||||
} else if resp, err := s.accountant.GetValue(req.Account, "rover"); err != nil {
|
||||
return nil, err
|
||||
|
||||
} else if rover, err := s.world.GetRover(resp.Value); err != nil {
|
||||
} else if rover, err := s.world.GetRover(resp); err != nil {
|
||||
return nil, fmt.Errorf("error getting rover: %s", err)
|
||||
|
||||
} else {
|
||||
|
@ -79,14 +77,14 @@ func (s *Server) Radar(ctx context.Context, req *rove.RadarRequest) (*rove.Radar
|
|||
|
||||
response := &rove.RadarResponse{}
|
||||
|
||||
resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"})
|
||||
resp, err := s.accountant.GetValue(req.Account, "rover")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gRPC failed to contact accountant: %s", err)
|
||||
return nil, err
|
||||
|
||||
} else if rover, err := s.world.GetRover(resp.Value); err != nil {
|
||||
} else if rover, err := s.world.GetRover(resp); err != nil {
|
||||
return nil, fmt.Errorf("error getting rover attributes: %s", err)
|
||||
|
||||
} else if radar, err := s.world.RadarFromRover(resp.Value); err != nil {
|
||||
} else if radar, err := s.world.RadarFromRover(resp); err != nil {
|
||||
return nil, fmt.Errorf("error getting radar from rover: %s", err)
|
||||
|
||||
} else {
|
||||
|
@ -101,8 +99,7 @@ func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove
|
|||
if len(req.Account) == 0 {
|
||||
return nil, fmt.Errorf("empty account")
|
||||
}
|
||||
resp, err := s.accountant.GetValue(ctx, &accounts.DataKey{Account: req.Account, Key: "rover"})
|
||||
|
||||
resp, err := s.accountant.GetValue(req.Account, "rover")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -114,7 +111,7 @@ func (s *Server) Commands(ctx context.Context, req *rove.CommandsRequest) (*rove
|
|||
Command: c.Command})
|
||||
}
|
||||
|
||||
if err := s.world.Enqueue(resp.Value, cmds...); err != nil {
|
||||
if err := s.world.Enqueue(resp, cmds...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
|
@ -30,9 +28,8 @@ type Server struct {
|
|||
// Internal state
|
||||
world *game.World
|
||||
|
||||
// Accountant server
|
||||
accountant accounts.AccountantClient
|
||||
clientConn *grpc.ClientConn
|
||||
// Accountant
|
||||
accountant *accounts.Accountant
|
||||
|
||||
// gRPC server
|
||||
netListener net.Listener
|
||||
|
@ -84,6 +81,7 @@ func NewServer(opts ...ServerOption) *Server {
|
|||
persistence: EphemeralData,
|
||||
schedule: cron.New(),
|
||||
world: game.NewWorld(32),
|
||||
accountant: accounts.NewAccountant(),
|
||||
}
|
||||
|
||||
// Apply all options
|
||||
|
@ -100,18 +98,6 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
|
|||
// Add to our sync
|
||||
s.sync.Add(1)
|
||||
|
||||
// Connect to the accountant
|
||||
accountantAddress := os.Getenv("ROVE_ACCOUNTANT_GRPC")
|
||||
if len(accountantAddress) == 0 {
|
||||
accountantAddress = "localhost:9091"
|
||||
}
|
||||
log.Printf("Dialing accountant on %s\n", accountantAddress)
|
||||
s.clientConn, err = grpc.Dial(accountantAddress, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.accountant = accounts.NewAccountantClient(s.clientConn)
|
||||
|
||||
// Load the world file
|
||||
if err := s.LoadWorld(); err != nil {
|
||||
return err
|
||||
|
@ -173,11 +159,6 @@ func (s *Server) Stop() error {
|
|||
// Stop the gRPC
|
||||
s.grpcServ.Stop()
|
||||
|
||||
// Close the accountant connection
|
||||
if err := s.clientConn.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -190,7 +171,7 @@ func (s *Server) Close() error {
|
|||
return s.SaveWorld()
|
||||
}
|
||||
|
||||
// Close waits until the server is finished and closes up shop
|
||||
// StopAndClose waits until the server is finished and closes up shop
|
||||
func (s *Server) StopAndClose() error {
|
||||
// Stop the server
|
||||
if err := s.Stop(); err != nil {
|
||||
|
@ -225,19 +206,12 @@ func (s *Server) LoadWorld() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// used as the type for the return struct
|
||||
type BadRequestError struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// SpawnRoverForAccount spawns the rover rover for an account
|
||||
func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
||||
if inst, err := s.world.SpawnRover(); err != nil {
|
||||
return "", err
|
||||
|
||||
} else {
|
||||
keyval := accounts.DataKeyValue{Account: account, Key: "rover", Value: inst}
|
||||
_, err := s.accountant.AssignValue(context.Background(), &keyval)
|
||||
err := s.accountant.AssignData(account, "rover", inst)
|
||||
if err != nil {
|
||||
log.Printf("Failed to assign rover to account, %s", err)
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -31,7 +30,6 @@ func TestNewServer_OptionPersistentData(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestServer_Run(t *testing.T) {
|
||||
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
|
||||
server := NewServer()
|
||||
if server == nil {
|
||||
t.Error("Failed to create server")
|
||||
|
@ -47,7 +45,6 @@ func TestServer_Run(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestServer_RunPersistentData(t *testing.T) {
|
||||
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
|
||||
server := NewServer(OptionPersistentData())
|
||||
if server == nil {
|
||||
t.Error("Failed to create server")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue