Extract persistence code into own class
This commit is contained in:
parent
4c76530832
commit
c5ebbc3c40
9 changed files with 163 additions and 149 deletions
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
"github.com/mdiluz/rove/pkg/accounts"
|
||||
"github.com/mdiluz/rove/pkg/game"
|
||||
"github.com/mdiluz/rove/pkg/persistence"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -48,10 +49,9 @@ func OptionPort(port int) ServerOption {
|
|||
}
|
||||
|
||||
// OptionPersistentData sets the server data to be persistent
|
||||
func OptionPersistentData(loc string) ServerOption {
|
||||
func OptionPersistentData() ServerOption {
|
||||
return func(s *Server) {
|
||||
s.persistence = PersistentData
|
||||
s.persistenceLocation = loc
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,8 +76,8 @@ func NewServer(opts ...ServerOption) *Server {
|
|||
s.server = &http.Server{Addr: fmt.Sprintf(":%d", s.port), Handler: router}
|
||||
|
||||
// Create the accountant
|
||||
s.accountant = accounts.NewAccountant(s.persistenceLocation)
|
||||
s.world = game.NewWorld(s.persistenceLocation)
|
||||
s.accountant = accounts.NewAccountant()
|
||||
s.world = game.NewWorld()
|
||||
|
||||
return s
|
||||
}
|
||||
|
@ -87,10 +87,7 @@ func (s *Server) Initialise() error {
|
|||
|
||||
// Load the accounts if requested
|
||||
if s.persistence == PersistentData {
|
||||
if err := s.accountant.Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.world.Load(); err != nil {
|
||||
if err := persistence.LoadAll("accounts", &s.accountant, "world", &s.world); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -130,10 +127,7 @@ func (s *Server) Close() error {
|
|||
|
||||
// Save the accounts if requested
|
||||
if s.persistence == PersistentData {
|
||||
if err := s.accountant.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.world.Save(); err != nil {
|
||||
if err := persistence.SaveAll("accounts", s.accountant, "world", s.world); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -22,13 +21,11 @@ func TestNewServer_OptionPort(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestNewServer_OptionPersistentData(t *testing.T) {
|
||||
server := NewServer(OptionPersistentData(os.TempDir()))
|
||||
server := NewServer(OptionPersistentData())
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +44,7 @@ func TestServer_Run(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestServer_RunPersistentData(t *testing.T) {
|
||||
server := NewServer(OptionPersistentData(os.TempDir()))
|
||||
server := NewServer(OptionPersistentData())
|
||||
if server == nil {
|
||||
t.Error("Failed to create server")
|
||||
} else if err := server.Initialise(); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue