v2 major updates
This commit is contained in:
parent
1fc1490f9b
commit
00df5f9031
|
@ -15,30 +15,32 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
UseTLS bool `json:"useTLS"`
|
Title string `json:"title"`
|
||||||
Servers []server `json:"servers"`
|
Content string `json:"content"`
|
||||||
TLSHostname string `json:"tlshostname"`
|
Servers []server `json:"servers"`
|
||||||
DebugServerPort int `json:"debugserverport"`
|
UseTLS bool `json:"useTLS"`
|
||||||
BackupDir string `json:"backupDir"`
|
TLSHostname string `json:"tlshostname"`
|
||||||
|
ServerPort int `json:"serverport"`
|
||||||
|
BackupDir string `json:"backupDir"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
Host string `json:"host"`
|
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
|
||||||
RCONPort int `json:"rconport"`
|
RCONPort int `json:"rconport"`
|
||||||
RCONPassword string `json:"rconpassword"`
|
RCONPassword string `json:"rconpassword"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"` // TODO: get this from RCON?
|
||||||
Description string `json:"description"`
|
Description string `json:"description"` // TODO: get this from RCON?
|
||||||
|
Version string // Populated by RCON
|
||||||
|
Players string // Populated by RCON
|
||||||
rconConnection *rcon.RemoteConsole
|
rconConnection *rcon.RemoteConsole
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverData struct {
|
type pageData struct {
|
||||||
IPAddr string
|
Title string
|
||||||
Port int
|
Content string
|
||||||
Title string
|
Servers []server
|
||||||
Players string
|
|
||||||
Version string
|
|
||||||
Description string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// rconCommand executes a command on the server, and returns the server's
|
// rconCommand executes a command on the server, and returns the server's
|
||||||
|
@ -77,55 +79,53 @@ func main() {
|
||||||
serveBackups = false
|
serveBackups = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data := pageData{}
|
||||||
|
data.Title = config.Title
|
||||||
|
data.Content = config.Content
|
||||||
|
data.Servers = config.Servers
|
||||||
|
|
||||||
// Set up templates
|
// Set up templates
|
||||||
fmt.Print("Parsing templates...\n")
|
fmt.Print("Parsing templates...\n")
|
||||||
t, err := template.ParseFiles("templates/index.html")
|
t, err := template.ParseFiles("templates/index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error parsing HTML template: %v\n", err)
|
log.Fatalf("Error parsing HTML template: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]
|
||||||
config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.Host, 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 s.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) {
|
||||||
|
|
||||||
data := []serverData{}
|
// Update servers with current data
|
||||||
|
for i, s := range config.Servers {
|
||||||
|
|
||||||
for _, s := range config.Servers {
|
config.Servers[i].Players, err = s.rconCommand("/players o")
|
||||||
|
|
||||||
playersOnline, 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
version, err := s.rconCommand("/version")
|
config.Servers[i].Version, err = s.rconCommand("/version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error executing version command: %v\n", err)
|
log.Printf("Error executing version command: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data = append(data, serverData{
|
|
||||||
s.Host,
|
|
||||||
s.Port,
|
|
||||||
s.Title,
|
|
||||||
playersOnline,
|
|
||||||
version,
|
|
||||||
s.Description,
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Execute(w, data)
|
err = t.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error executing template: %v\n", err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Serve backup directory
|
// Serve backup directory
|
||||||
if serveBackups {
|
if serveBackups { // TODO: also remove HTML if disabled
|
||||||
http.Handle("/backups/", http.StripPrefix("/backups/", http.FileServer(http.Dir(config.BackupDir))))
|
http.Handle("/backups/", http.StripPrefix("/backups/", http.FileServer(http.Dir(config.BackupDir))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,15 +146,18 @@ func main() {
|
||||||
|
|
||||||
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt
|
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt
|
||||||
|
|
||||||
fmt.Println("Serving...")
|
if config.ServerPort == 0 { // Value not set in JSON
|
||||||
|
config.ServerPort = 443
|
||||||
|
}
|
||||||
|
fmt.Printf("Serving HTTPS on port %v...\n", config.ServerPort)
|
||||||
srv.ListenAndServeTLS("", "") // Key/cert come from srv.TLSConfig
|
srv.ListenAndServeTLS("", "") // Key/cert come from srv.TLSConfig
|
||||||
|
|
||||||
} else { // Debug
|
} else {
|
||||||
fmt.Println("Serving...")
|
if config.ServerPort == 0 { // Value not set in JSON
|
||||||
if config.DebugServerPort == 0 { // Value not set in JSON
|
config.ServerPort = 80
|
||||||
config.DebugServerPort = 8080
|
|
||||||
}
|
}
|
||||||
http.ListenAndServe(fmt.Sprintf(":%v", config.DebugServerPort), nil) // TODO: pass as config value
|
fmt.Printf("Serving HTTP on port %v...\n", config.ServerPort)
|
||||||
|
http.ListenAndServe(fmt.Sprintf(":%v", config.ServerPort), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>blackolivepineapple.pizza Factorio Server</title>
|
<title>{{.Title}}</title>
|
||||||
<style>
|
<style>
|
||||||
html {
|
html {
|
||||||
font-family: sans-serif;
|
font-family: sans-serif;
|
||||||
|
@ -10,14 +10,14 @@
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>blackolivepineapple.pizza Factorio</h1>
|
<h1>{{.Title}}</h1>
|
||||||
<p>Hosted by the <a href="https://blackolivepineapple.pizza/">Black Olive-Pineapple Pizza Consortium</a>, in collaboration with UMD's ACM Club</p>
|
<p>{{.Content}}</p>
|
||||||
<p>Backups are taken and archived periodically, and made available in the <a href="/backups">backup archive</a>.</p>
|
<p>Backups are taken and archived periodically, and made available in the <a href="/backups">backup archive</a>.</p>
|
||||||
{{range .}}
|
{{range .Servers}}
|
||||||
<h3>{{.Title}}</h3>
|
<h3>{{.Title}}</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><b>Description:</b> {{.Description}}</li>
|
<li><b>Description:</b> {{.Description}}</li>
|
||||||
<li><b>Connection String:</b> {{.IPAddr}}{{if ne .Port 34197}}:{{.Port}}{{end}}</li>
|
<li><b>Connection String:</b> {{.Host}}{{if ne .Port 34197}}:{{.Port}}{{end}}</li>
|
||||||
<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>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
Loading…
Reference in a new issue