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 {
Host string `json:"host"` // for display only
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"`
RCONPassword string `json:"rconpassword"`
Title string `json:"title"` // TODO: get this from RCON?
@ -94,28 +94,30 @@ func main() {
// Connect to RCON servers
for i := range config.Servers {
s := config.Servers[i]
config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.RCONHost, s.RCONPort), s.RCONPassword)
if err != nil {
log.Fatalf("Error making RCON connection to %v: %v", s.Title, err)
if s.RCONHost != "" {
config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.RCONHost, s.RCONPort), s.RCONPassword)
if err != nil {
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) {
// Update servers with current data
for i, s := range config.Servers {
if s.rconConnection != nil {
config.Servers[i].Players, err = s.rconCommand("/players o")
if err != nil {
log.Printf("Error executing players online command: %v\n", err)
}
config.Servers[i].Players, err = s.rconCommand("/players o")
if err != nil {
log.Printf("Error executing players online command: %v\n", err)
config.Servers[i].Version, err = s.rconCommand("/version")
if err != nil {
log.Printf("Error executing version command: %v\n", err)
}
}
config.Servers[i].Version, err = s.rconCommand("/version")
if err != nil {
log.Printf("Error executing version command: %v\n", err)
}
}
err = t.Execute(w, data)

View file

@ -34,8 +34,10 @@
<ul>
<li><b>Description:</b> {{.Description}}</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>Current players:</b><br>{{.Players}}</li>
{{end}}
</ul>
{{end}}
<p class="light">This page maintained by chandlerswift's <a href="https://github.com/chandlerswift/factorio-site">factorio server list</a>.</p>