Make RCON optional

This commit is contained in:
Chandler Swift 2021-02-14 00:11:52 -06:00
parent 6e518a1cc8
commit e478395d8e
2 changed files with 18 additions and 14 deletions

View file

@ -27,7 +27,7 @@ type config struct {
type server struct { type server struct {
Host string `json:"host"` // for display only Host string `json:"host"` // for display only
Port int `json:"port"` Port int `json:"port"`
RCONHost string `json:"rconhost"` // not displayed, but used to connect RCONHost string `json:"rconhost"` // not displayed, but used to connect; leave blank for no RCON connection
RCONPort int `json:"rconport"` RCONPort int `json:"rconport"`
RCONPassword string `json:"rconpassword"` RCONPassword string `json:"rconpassword"`
Title string `json:"title"` // TODO: get this from RCON? Title string `json:"title"` // TODO: get this from RCON?
@ -94,18 +94,20 @@ func main() {
// Connect to RCON servers // Connect to RCON servers
for i := range config.Servers { for i := range config.Servers {
s := config.Servers[i] s := config.Servers[i]
if s.RCONHost != "" {
config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.RCONHost, s.RCONPort), s.RCONPassword) config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.RCONHost, s.RCONPort), s.RCONPassword)
if err != nil { if err != nil {
log.Fatalf("Error making RCON connection to %v: %v", s.Title, err) log.Fatalf("Error making RCON connection to %v: %v", s.Title, err)
} }
defer config.Servers[i].rconConnection.Close() defer config.Servers[i].rconConnection.Close()
} }
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Update servers with current data // Update servers with current data
for i, s := range config.Servers { for i, s := range config.Servers {
if s.rconConnection != nil {
config.Servers[i].Players, err = s.rconCommand("/players o") config.Servers[i].Players, err = s.rconCommand("/players o")
if err != nil { if err != nil {
log.Printf("Error executing players online command: %v\n", err) log.Printf("Error executing players online command: %v\n", err)
@ -115,7 +117,7 @@ func main() {
if err != nil { if err != nil {
log.Printf("Error executing version command: %v\n", err) log.Printf("Error executing version command: %v\n", err)
} }
}
} }
err = t.Execute(w, data) err = t.Execute(w, data)

View file

@ -34,8 +34,10 @@
<ul> <ul>
<li><b>Description:</b> {{.Description}}</li> <li><b>Description:</b> {{.Description}}</li>
<li><b>Connection String:</b> {{.Host}}{{if ne .Port 34197}}:{{.Port}}{{end}}</li> <li><b>Connection String:</b> {{.Host}}{{if ne .Port 34197}}:{{.Port}}{{end}}</li>
{{if ne .RCONHost ""}}
<li><b>Version:</b> {{.Version}}</li> <li><b>Version:</b> {{.Version}}</li>
<li><b>Current players:</b><br>{{.Players}}</li> <li><b>Current players:</b><br>{{.Players}}</li>
{{end}}
</ul> </ul>
{{end}} {{end}}
<p class="light">This page maintained by chandlerswift's <a href="https://github.com/chandlerswift/factorio-site">factorio server list</a>.</p> <p class="light">This page maintained by chandlerswift's <a href="https://github.com/chandlerswift/factorio-site">factorio server list</a>.</p>