From d10026ae82b9a38d404e157bdc4abe6563e6b953 Mon Sep 17 00:00:00 2001 From: mappu Date: Fri, 19 May 2023 19:14:03 +1200 Subject: [PATCH] contented-multi: initial commit --- cmd/contented-multi/main.go | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 cmd/contented-multi/main.go diff --git a/cmd/contented-multi/main.go b/cmd/contented-multi/main.go new file mode 100644 index 0000000..e8d18fb --- /dev/null +++ b/cmd/contented-multi/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/json" + "flag" + "log" + "net/http" + "os" + "sync" + + "code.ivysaur.me/contented" +) + +type ContentedMultiCfg struct { + Servers []struct { + ListenAddr string + Options contented.ServerOptions + } +} + +func main() { + configFile := flag.String("config", "contented-multi.cfg", "Path to configuration file") + flag.Parse() + + fh, err := os.Open(*configFile) + if err != nil { + panic(err) + } + + var cfg ContentedMultiCfg + err = json.NewDecoder(fh).Decode(&cfg) + if err != nil { + panic(err) + } + + fh.Close() + + wg := sync.WaitGroup{} + wg.Add(len(cfg.Servers)) + + for i, _ := range cfg.Servers { + go (func(i int) { + defer wg.Done() + + s, err := contented.NewServer(&cfg.Servers[i].Options) + if err != nil { + log.Printf("Failed to create server %d/%d: %s", i+1, len(cfg.Servers), err.Error()) + return + } + + err = http.ListenAndServe(cfg.Servers[i].ListenAddr, s) + log.Printf("Server %d/%d shutting down: %s", i+1, len(cfg.Servers), err.Error()) + + })(i) + } + + wg.Wait() +}