From 5033ec4e63623c8c29cf36105ae35e89d2beb48c Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 2 Jun 2020 16:10:45 +0100 Subject: [PATCH] Specify the persistence path using the command line --- main.go | 3 ++- pkg/accounts/accounts.go | 25 +++++++++++++++++-------- pkg/accounts/accounts_test.go | 9 +++++---- pkg/server/server.go | 10 +++++++--- pkg/server/server_test.go | 7 +++++-- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index fabe2ce..ccc2f93 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( var ver = flag.Bool("version", false, "Display version number") var port = flag.Int("port", 8080, "The port to host on") +var data = flag.String("data", "/tmp/", "Directory to store persistant data") func main() { flag.Parse() @@ -24,7 +25,7 @@ func main() { s := server.NewServer( server.OptionPort(*port), - server.OptionPersistentData()) + server.OptionPersistentData(*data)) fmt.Println("Initialising...") if err := s.Initialise(); err != nil { diff --git a/pkg/accounts/accounts.go b/pkg/accounts/accounts.go index efcf976..603effa 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/accounts/accounts.go @@ -5,11 +5,12 @@ import ( "fmt" "io/ioutil" "os" + "path" "github.com/google/uuid" ) -const kDefaultSavePath = "/tmp/accounts.json" +const kAccountsFileName = "rove-accounts.json" // Account represents a registered user type Account struct { @@ -27,12 +28,15 @@ type accountantData struct { // Accountant manages a set of accounts type Accountant struct { - data accountantData + data accountantData + dataPath string } // NewAccountant creates a new accountant -func NewAccountant() *Accountant { - return &Accountant{} +func NewAccountant(dataPath string) *Accountant { + return &Accountant{ + dataPath: dataPath, + } } // RegisterAccount adds an account to the set of internal accounts @@ -56,16 +60,21 @@ func (a *Accountant) RegisterAccount(acc Account) (Account, error) { return acc, nil } +// path returns the full path to the data file +func (a Accountant) path() string { + return path.Join(a.dataPath, kAccountsFileName) +} + // Load will load the accountant from data func (a *Accountant) Load() error { // Don't load anything if the file doesn't exist - _, err := os.Stat(kDefaultSavePath) + _, err := os.Stat(a.path()) if os.IsNotExist(err) { - fmt.Printf("File %s didn't exist, loading with fresh accounts data\n", kDefaultSavePath) + fmt.Printf("File %s didn't exist, loading with fresh accounts data\n", a.path()) return nil } - if b, err := ioutil.ReadFile(kDefaultSavePath); err != nil { + if b, err := ioutil.ReadFile(a.path()); err != nil { return err } else if err := json.Unmarshal(b, &a.data); err != nil { return err @@ -78,7 +87,7 @@ func (a *Accountant) Save() error { if b, err := json.Marshal(a.data); err != nil { return err } else { - if err := ioutil.WriteFile(kDefaultSavePath, b, os.ModePerm); err != nil { + if err := ioutil.WriteFile(a.path(), b, os.ModePerm); err != nil { return err } } diff --git a/pkg/accounts/accounts_test.go b/pkg/accounts/accounts_test.go index 6d319a2..99b0b2d 100644 --- a/pkg/accounts/accounts_test.go +++ b/pkg/accounts/accounts_test.go @@ -1,12 +1,13 @@ package accounts import ( + "os" "testing" ) func TestNewAccountant(t *testing.T) { // Very basic verify here for now - accountant := NewAccountant() + accountant := NewAccountant(os.TempDir()) if accountant == nil { t.Error("Failed to create accountant") } @@ -14,7 +15,7 @@ func TestNewAccountant(t *testing.T) { func TestAccountant_RegisterAccount(t *testing.T) { - accountant := NewAccountant() + accountant := NewAccountant(os.TempDir()) // Start by making two accounts @@ -49,7 +50,7 @@ func TestAccountant_RegisterAccount(t *testing.T) { } func TestAccountant_LoadSave(t *testing.T) { - accountant := NewAccountant() + accountant := NewAccountant(os.TempDir()) if len(accountant.data.Accounts) != 0 { t.Error("New accountant created with non-zero account number") } @@ -73,7 +74,7 @@ func TestAccountant_LoadSave(t *testing.T) { } // Re-create the accountant - accountant = NewAccountant() + accountant = NewAccountant(os.TempDir()) if len(accountant.data.Accounts) != 0 { t.Error("New accountant created with non-zero account number") } diff --git a/pkg/server/server.go b/pkg/server/server.go index 7e07149..9b274bd 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -31,7 +31,8 @@ type Server struct { server *http.Server router *mux.Router - persistence int + persistence int + persistenceLocation string sync sync.WaitGroup } @@ -47,9 +48,10 @@ func OptionPort(port int) ServerOption { } // OptionPersistentData sets the server data to be persistent -func OptionPersistentData() ServerOption { +func OptionPersistentData(loc string) ServerOption { return func(s *Server) { s.persistence = PersistentData + s.persistenceLocation = loc } } @@ -61,7 +63,6 @@ func NewServer(opts ...ServerOption) *Server { // Set up the default server s := &Server{ port: 8080, - accountant: accounts.NewAccountant(), world: game.NewWorld(), persistence: EphemeralData, router: router, @@ -75,6 +76,9 @@ func NewServer(opts ...ServerOption) *Server { // Set up the server object s.server = &http.Server{Addr: fmt.Sprintf(":%d", s.port), Handler: router} + // Create the accountant + s.accountant = accounts.NewAccountant(s.persistenceLocation) + return s } diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 619ebbe..f4598d7 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -1,6 +1,7 @@ package server import ( + "os" "testing" ) @@ -21,11 +22,13 @@ func TestNewServer_OptionPort(t *testing.T) { } func TestNewServer_OptionPersistentData(t *testing.T) { - server := NewServer(OptionPersistentData()) + server := NewServer(OptionPersistentData(os.TempDir())) if server == nil { t.Error("Failed to create server") } else if server.persistence != PersistentData { t.Error("Failed to set server persistent data") + } else if server.persistenceLocation != os.TempDir() { + t.Error("Failed to set server persistent data path") } } @@ -44,7 +47,7 @@ func TestServer_Run(t *testing.T) { } func TestServer_RunPersistentData(t *testing.T) { - server := NewServer(OptionPersistentData()) + server := NewServer(OptionPersistentData(os.TempDir())) if server == nil { t.Error("Failed to create server") } else if err := server.Initialise(); err != nil {