ss13_se/ss13.go
2016-04-22 11:32:30 +02:00

112 lines
2.3 KiB
Go

package main
import (
"fmt"
"os"
"time"
"github.com/codegangsta/cli"
"github.com/lmas/ss13_se/src"
)
func main() {
app := cli.NewApp()
app.Version = ss13.VERSION
app.Usage = "Web server for showing stats for SS13 servers."
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "Run in debug mode",
},
cli.BoolFlag{
Name: "verbose",
Usage: "Show verbose messages",
},
cli.StringFlag{
Name: "addr",
Usage: "Set the listen address for the web server",
Value: ":8000",
},
cli.BoolFlag{
Name: "daemon",
Usage: "Continuously run when using the update command",
},
cli.IntFlag{
Name: "timeout",
Usage: "Time (in minutes) between each update in daemon mode",
Value: 15,
},
cli.StringFlag{
Name: "private-servers",
Usage: "JSON file with a list of private servers to poll",
Value: "./servers.json",
},
cli.StringFlag{
Name: "database",
Usage: "Database file",
Value: "./ss13.db",
},
}
app.Commands = []cli.Command{
{
Name: "run",
Usage: "Run the web server",
Action: run_server,
},
{
Name: "update",
Usage: "Update the server stats",
Action: update_stats,
},
}
app.Run(os.Args)
}
func run_server(c *cli.Context) {
if c.GlobalBool("verbose") {
fmt.Printf("Listening on %s.\n\n", c.GlobalString("addr"))
}
instance := &ss13.Instance{
Debug: c.GlobalBool("debug"),
DB: ss13.OpenSqliteDB(c.GlobalString("database")),
PrivServersFile: c.GlobalString("private-servers"),
}
instance.Init()
instance.Serve(c.GlobalString("addr"))
}
func update_stats(c *cli.Context) {
td := time.Duration(c.GlobalInt("timeout")) * time.Minute
if c.GlobalBool("verbose") {
if c.GlobalBool("daemon") {
fmt.Printf("Running updates every %v minutes\n", c.GlobalInt("timeout"))
} else {
fmt.Println("Updating...")
}
}
instance := &ss13.Instance{
Debug: c.GlobalBool("debug"),
DB: ss13.OpenSqliteDB(c.GlobalString("database")),
PrivServersFile: c.GlobalString("private-servers"),
}
instance.Init()
for {
start := time.Now()
instance.UpdateServers()
stop := time.Now()
if c.GlobalBool("verbose") {
fmt.Printf("Update completed in %v\n", stop.Sub(start))
}
if !c.GlobalBool("daemon") {
return
}
fmt.Println("Next at ", time.Now().Add(td))
time.Sleep(td)
}
}