github.com/df-mc/dragonfly@v0.9.13/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/df-mc/dragonfly/server" 6 "github.com/df-mc/dragonfly/server/player/chat" 7 "github.com/pelletier/go-toml" 8 "github.com/sirupsen/logrus" 9 "os" 10 ) 11 12 func main() { 13 log := logrus.New() 14 log.Formatter = &logrus.TextFormatter{ForceColors: true} 15 log.Level = logrus.DebugLevel 16 17 chat.Global.Subscribe(chat.StdoutSubscriber{}) 18 19 conf, err := readConfig(log) 20 if err != nil { 21 log.Fatalln(err) 22 } 23 24 srv := conf.New() 25 srv.CloseOnProgramEnd() 26 27 srv.Listen() 28 for srv.Accept(nil) { 29 } 30 } 31 32 // readConfig reads the configuration from the config.toml file, or creates the 33 // file if it does not yet exist. 34 func readConfig(log server.Logger) (server.Config, error) { 35 c := server.DefaultConfig() 36 var zero server.Config 37 if _, err := os.Stat("config.toml"); os.IsNotExist(err) { 38 data, err := toml.Marshal(c) 39 if err != nil { 40 return zero, fmt.Errorf("encode default config: %v", err) 41 } 42 if err := os.WriteFile("config.toml", data, 0644); err != nil { 43 return zero, fmt.Errorf("create default config: %v", err) 44 } 45 return c.Config(log) 46 } 47 data, err := os.ReadFile("config.toml") 48 if err != nil { 49 return zero, fmt.Errorf("read config: %v", err) 50 } 51 if err := toml.Unmarshal(data, &c); err != nil { 52 return zero, fmt.Errorf("decode config: %v", err) 53 } 54 return c.Config(log) 55 }