Move accountant to it's own deployment using gRCP
This commit is contained in:
parent
8f25f55658
commit
99da6c5d67
14 changed files with 868 additions and 64 deletions
|
@ -1,12 +1,14 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/version"
|
||||
)
|
||||
|
@ -84,14 +86,19 @@ func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Wri
|
|||
} else if len(data.Name) == 0 {
|
||||
response.Error = "Cannot register empty name"
|
||||
|
||||
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
|
||||
}
|
||||
reg := accounts.RegisterInfo{Name: data.Name}
|
||||
if acc, err := s.accountant.Register(context.Background(), ®); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if _, _, err := s.SpawnRoverForAccount(acc.Name); err != nil {
|
||||
} else if !acc.Success {
|
||||
response.Error = acc.Error
|
||||
|
||||
} else if _, _, err := s.SpawnRoverForAccount(data.Name); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if err := s.SaveAll(); err != nil {
|
||||
response.Error = fmt.Sprintf("Internal server error when saving: %s", err)
|
||||
} else if err := s.SaveWorld(); err != nil {
|
||||
response.Error = fmt.Sprintf("Internal server error when saving world: %s", err)
|
||||
|
||||
} else {
|
||||
// Save out the new accounts
|
||||
|
@ -116,13 +123,19 @@ func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writ
|
|||
fmt.Printf("Failed to decode json: %s\n", err)
|
||||
response.Error = err.Error()
|
||||
|
||||
} else if len(id) == 0 {
|
||||
}
|
||||
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if id, err := uuid.Parse(inst); err != nil {
|
||||
} else if !resp.Success {
|
||||
response.Error = resp.Error
|
||||
|
||||
} else if id, err := uuid.Parse(resp.Value); err != nil {
|
||||
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||
|
||||
} else if err := s.world.Enqueue(id, data.Commands...); err != nil {
|
||||
|
@ -143,13 +156,17 @@ func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
}
|
||||
|
||||
id := vars["account"]
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if id, err := uuid.Parse(inst); err != nil {
|
||||
} else if !resp.Success {
|
||||
response.Error = resp.Error
|
||||
|
||||
} else if id, err := uuid.Parse(resp.Value); err != nil {
|
||||
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||
|
||||
} else if attrib, err := s.world.RoverAttributes(id); err != nil {
|
||||
|
@ -175,13 +192,17 @@ func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer
|
|||
}
|
||||
|
||||
id := vars["account"]
|
||||
key := accounts.DataKey{Account: id, Key: "rover"}
|
||||
if len(id) == 0 {
|
||||
response.Error = "No account ID provided"
|
||||
|
||||
} else if inst, err := s.accountant.GetData(id, "rover"); err != nil {
|
||||
} else if resp, err := s.accountant.GetValue(context.Background(), &key); err != nil {
|
||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
||||
|
||||
} else if id, err := uuid.Parse(inst); err != nil {
|
||||
} else if !resp.Success {
|
||||
response.Error = resp.Error
|
||||
|
||||
} else if id, err := uuid.Parse(resp.Value); err != nil {
|
||||
response.Error = fmt.Sprintf("Account had invalid rover id: %s", err)
|
||||
|
||||
} else if attribs, err := s.world.RoverAttributes(id); err != nil {
|
||||
|
|
|
@ -2,12 +2,14 @@ package internal
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/mdiluz/rove/pkg/rove"
|
||||
"github.com/mdiluz/rove/pkg/vector"
|
||||
|
@ -62,11 +64,15 @@ func TestHandleRegister(t *testing.T) {
|
|||
func TestHandleCommand(t *testing.T) {
|
||||
s := NewServer()
|
||||
s.Initialise(false) // Leave the world empty with no obstacles
|
||||
a, err := s.accountant.RegisterAccount("test")
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
_, inst, err := s.SpawnRoverForAccount(a.Name)
|
||||
_, inst, err := s.SpawnRoverForAccount("test")
|
||||
assert.NoError(t, s.world.WarpRover(inst, vector.Vector{}))
|
||||
|
||||
attribs, err := s.world.RoverAttributes(inst)
|
||||
|
@ -85,7 +91,7 @@ func TestHandleCommand(t *testing.T) {
|
|||
b, err := json.Marshal(data)
|
||||
assert.NoError(t, err, "Error marshalling data")
|
||||
|
||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", a.Name, "/command"), bytes.NewReader(b))
|
||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", "test", "/command"), bytes.NewReader(b))
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
@ -114,11 +120,14 @@ func TestHandleCommand(t *testing.T) {
|
|||
func TestHandleRadar(t *testing.T) {
|
||||
s := NewServer()
|
||||
s.Initialise(false) // Spawn a clean world
|
||||
a, err := s.accountant.RegisterAccount("test")
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
|
||||
// Spawn the rover rover for the account
|
||||
attrib, id, err := s.SpawnRoverForAccount(a.Name)
|
||||
attrib, id, err := s.SpawnRoverForAccount("test")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Warp this rover to 0,0
|
||||
|
@ -134,7 +143,7 @@ func TestHandleRadar(t *testing.T) {
|
|||
assert.NoError(t, s.world.Atlas.SetTile(rockPos, game.TileRock))
|
||||
assert.NoError(t, s.world.Atlas.SetTile(emptyPos, game.TileEmpty))
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Name, "/radar"), nil)
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/radar"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
@ -168,14 +177,16 @@ func TestHandleRadar(t *testing.T) {
|
|||
func TestHandleRover(t *testing.T) {
|
||||
s := NewServer()
|
||||
s.Initialise(true)
|
||||
a, err := s.accountant.RegisterAccount("test")
|
||||
assert.NoError(t, err, "Error registering account")
|
||||
reg := accounts.RegisterInfo{Name: "test"}
|
||||
acc, err := s.accountant.Register(context.Background(), ®)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, acc.Success)
|
||||
|
||||
// Spawn one rover for the account
|
||||
attribs, _, err := s.SpawnRoverForAccount(a.Name)
|
||||
attribs, _, err := s.SpawnRoverForAccount("test")
|
||||
assert.NoError(t, err)
|
||||
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Name, "/rover"), nil)
|
||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", "test", "/rover"), nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
s.router.ServeHTTP(response, request)
|
||||
|
|
|
@ -3,6 +3,7 @@ package internal
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
@ -16,8 +17,11 @@ import (
|
|||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/mdiluz/rove/pkg/persistence"
|
||||
"github.com/robfig/cron"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var accountantAddress = flag.String("accountant", "", "address of the accountant to connect to")
|
||||
|
||||
const (
|
||||
// PersistentData will allow the server to load and save it's state
|
||||
PersistentData = iota
|
||||
|
@ -30,8 +34,11 @@ const (
|
|||
type Server struct {
|
||||
|
||||
// Internal state
|
||||
accountant *accounts.Accountant
|
||||
world *game.World
|
||||
world *game.World
|
||||
|
||||
// Accountant server
|
||||
accountant accounts.AccountantClient
|
||||
clientConn *grpc.ClientConn
|
||||
|
||||
// HTTP server
|
||||
listener net.Listener
|
||||
|
@ -96,9 +103,6 @@ func NewServer(opts ...ServerOption) *Server {
|
|||
// Set up the server object
|
||||
s.server = &http.Server{Addr: s.address, Handler: s.router}
|
||||
|
||||
// Create the accountant
|
||||
s.accountant = accounts.NewAccountant()
|
||||
|
||||
// Start small, we can grow the world later
|
||||
s.world = game.NewWorld(4, 8)
|
||||
|
||||
|
@ -111,13 +115,21 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
|
|||
// Add to our sync
|
||||
s.sync.Add(1)
|
||||
|
||||
// Connect to the accountant
|
||||
fmt.Printf("Dialing accountant on %s\n", *accountantAddress)
|
||||
clientConn, err := grpc.Dial(*accountantAddress, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.accountant = accounts.NewAccountantClient(clientConn)
|
||||
|
||||
// Spawn a border on the default world
|
||||
if err := s.world.SpawnWorld(fillWorld); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Load the accounts if requested
|
||||
if err := s.LoadAll(); err != nil {
|
||||
// Load the world file
|
||||
if err := s.LoadWorld(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -183,6 +195,11 @@ func (s *Server) Stop() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Close the accountant connection
|
||||
if err := s.clientConn.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -192,7 +209,7 @@ func (s *Server) Close() error {
|
|||
s.sync.Wait()
|
||||
|
||||
// Save and return
|
||||
return s.SaveAll()
|
||||
return s.SaveWorld()
|
||||
}
|
||||
|
||||
// Close waits until the server is finished and closes up shop
|
||||
|
@ -218,36 +235,12 @@ func (s *Server) SaveWorld() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SaveAccounts will save out the accounts file
|
||||
func (s *Server) SaveAccounts() error {
|
||||
if s.persistence == PersistentData {
|
||||
if err := persistence.SaveAll("accounts", s.accountant); err != nil {
|
||||
return fmt.Errorf("failed to save out persistent data: %s", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveAll will save out all server files
|
||||
func (s *Server) SaveAll() error {
|
||||
// Save the accounts if requested
|
||||
if s.persistence == PersistentData {
|
||||
s.world.RLock()
|
||||
defer s.world.RUnlock()
|
||||
|
||||
if err := persistence.SaveAll("accounts", s.accountant, "world", s.world); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadAll will load all persistent data
|
||||
func (s *Server) LoadAll() error {
|
||||
// LoadWorld will load all persistent data
|
||||
func (s *Server) LoadWorld() error {
|
||||
if s.persistence == PersistentData {
|
||||
s.world.Lock()
|
||||
defer s.world.Unlock()
|
||||
if err := persistence.LoadAll("accounts", &s.accountant, "world", &s.world); err != nil {
|
||||
if err := persistence.LoadAll("world", &s.world); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -289,7 +282,11 @@ func (s *Server) SpawnRoverForAccount(account string) (game.RoverAttributes, uui
|
|||
return game.RoverAttributes{}, uuid.UUID{}, fmt.Errorf("No attributes found for created rover: %s", err)
|
||||
|
||||
} else {
|
||||
if err := s.accountant.AssignData(account, "rover", inst.String()); err != nil {
|
||||
keyval := accounts.DataKeyValue{Account: account, Key: "rover", Value: inst.String()}
|
||||
resp, err := s.accountant.AssignValue(context.Background(), &keyval)
|
||||
if err != nil || !resp.Success {
|
||||
fmt.Printf("Failed to assign rover to account, %s, %s", err, resp.Error)
|
||||
|
||||
// Try and clear up the rover
|
||||
if err := s.world.DestroyRover(inst); err != nil {
|
||||
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue