Refactor ss13.go into main.go and a much cleaner state.
This commit is contained in:
parent
ddc9377e53
commit
d710ef9bea
64
main.go
Normal file
64
main.go
Normal file
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/lmas/ss13_se/src"
|
||||
)
|
||||
|
||||
var (
|
||||
fAddr = flag.String("addr", ":8000", "address to listen on, for the web server")
|
||||
fDatabase = flag.String("database", "./ss13.db", "database file")
|
||||
fDebug = flag.Bool("debug", false, "run in debug mode")
|
||||
fPrivateServers = flag.String("servers", "./servers.json", "file with a list of private servers to poll")
|
||||
fTimeout = flag.Int("timeout", 15, "time (in minutes) between each update")
|
||||
fVerbose = flag.Bool("verbose", false, "show verbose messages")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
db, e := ss13.OpenSqliteDB(*fDatabase)
|
||||
CheckError(e)
|
||||
|
||||
ins := &ss13.Instance{
|
||||
Debug: *fDebug,
|
||||
DB: db,
|
||||
PrivServersFile: *fPrivateServers,
|
||||
}
|
||||
ins.Init()
|
||||
|
||||
td := time.Duration(*fTimeout) * time.Minute
|
||||
go func() {
|
||||
if *fVerbose {
|
||||
Log("Updating servers every %s", td)
|
||||
}
|
||||
for {
|
||||
start := time.Now()
|
||||
ins.UpdateServers()
|
||||
dur := time.Since(start)
|
||||
if *fVerbose {
|
||||
Log("Update completed in %s", dur)
|
||||
}
|
||||
time.Sleep(td)
|
||||
}
|
||||
}()
|
||||
|
||||
if *fVerbose {
|
||||
Log("Listening on %s", *fAddr)
|
||||
}
|
||||
e = ins.Serve(*fAddr)
|
||||
CheckError(e)
|
||||
}
|
||||
|
||||
func Log(f string, args ...interface{}) {
|
||||
log.Printf(f+"\n", args...)
|
||||
}
|
||||
|
||||
func CheckError(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
122
ss13.go
122
ss13.go
@ -1,122 +0,0 @@
|
||||
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"))
|
||||
}
|
||||
|
||||
db, e := ss13.OpenSqliteDB(c.GlobalString("database"))
|
||||
if ss13.LogError(e) {
|
||||
return
|
||||
}
|
||||
|
||||
instance := &ss13.Instance{
|
||||
Debug: c.GlobalBool("debug"),
|
||||
DB: db,
|
||||
PrivServersFile: c.GlobalString("private-servers"),
|
||||
}
|
||||
instance.Init()
|
||||
e = instance.Serve(c.GlobalString("addr"))
|
||||
ss13.LogError(e)
|
||||
}
|
||||
|
||||
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...")
|
||||
}
|
||||
}
|
||||
|
||||
db, e := ss13.OpenSqliteDB(c.GlobalString("database"))
|
||||
if ss13.LogError(e) {
|
||||
return
|
||||
}
|
||||
|
||||
instance := &ss13.Instance{
|
||||
Debug: c.GlobalBool("debug"),
|
||||
DB: db,
|
||||
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)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user