aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.go50
1 files changed, 41 insertions, 9 deletions
diff --git a/config.go b/config.go
index 93ee3fa..60fa1e5 100644
--- a/config.go
+++ b/config.go
@@ -2,6 +2,7 @@ package proxy
import (
"encoding/json"
+ "fmt"
"log"
"os"
"sync"
@@ -26,6 +27,7 @@ type Server struct {
Name string
Addr string
Fallbacks []string
+ dynamic bool
}
// A Config contains information from the configuration file
@@ -93,34 +95,34 @@ func Conf() Config {
// For the media to work you have to specify an alternative
// media source that is always available, even if the server
// is offline.
-// WARNING: Reloading the configuration file deletes the
-// servers added using this function.
-func AddServer(server Server) bool {
+func AddServer(s Server) bool {
configMu.Lock()
defer configMu.Unlock()
+ s.dynamic = true
for _, srv := range config.Servers {
- if srv.Name == server.Name {
+ if srv.Name == s.Name {
return false
}
}
- config.Servers = append(config.Servers, server)
+ config.Servers = append(config.Servers, s)
return true
}
// RmServer deletes a Server from the Config at runtime.
-// Any server can be deleted this way, not just the ones
-// added using AddServer.
+// Only servers added using AddServer can be deleted at runtime.
// Returns true on success or if the server doesn't exist.
-// WARNING: Reloading the configuration files re-adds the
-// servers it contains that were deleted using this function.
func RmServer(name string) bool {
configMu.Lock()
defer configMu.Unlock()
for i, srv := range config.Servers {
if srv.Name == name {
+ if srv.dynamic {
+ return false
+ }
+
// Can't remove server if players are connected to it
for cc := range Clts() {
if cc.ServerName() == name {
@@ -204,6 +206,36 @@ func LoadConfig() error {
return err
}
+ // Dynamic servers shouldn't be deleted silently.
+DynLoop:
+ for _, srv := range oldConf.Servers {
+ if srv.dynamic {
+ config.Servers = append(config.Servers, srv)
+ } else {
+ for _, s := range config.Servers {
+ if srv.Name == s.Name {
+ continue DynLoop
+ }
+ }
+
+ for cc := range Clts() {
+ if cc.ServerName() == srv.Name {
+ config = oldConf
+ return fmt.Errorf("can't delete server %s with players", srv.Name)
+ }
+ }
+ }
+ }
+
+ for _, srv := range config.Servers {
+ for _, s := range config.Servers {
+ if srv.Name == s.Name {
+ config = oldConf
+ return fmt.Errorf("duplicate server %s", s.Name)
+ }
+ }
+ }
+
log.Print("load config")
return nil
}