Specify the persistence path using the command line

This commit is contained in:
Marc Di Luzio 2020-06-02 16:10:45 +01:00
parent c085e56954
commit 5033ec4e63
5 changed files with 36 additions and 18 deletions

View file

@ -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
}

View file

@ -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 {