diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..2962321 --- /dev/null +++ b/config.toml @@ -0,0 +1,27 @@ +databasepath = "ss13.db" + +listenaddr = ":8000" + +updateevery = 15 # minutes + +updatetimeout = 30 # seconds + +[[servers]] +title = "Baystation12" +gameurl = "baystation12.net:8000" +siteurl = "http://baystation12.net/" + +[[servers]] +title = "Apollo Station" +gameurl = "192.95.55.67:3333" +siteurl = "https://apollo-community.org/" + +[[servers]] +title = "tgstation13 - Sybil" +gameurl = "game.tgstation13.org:1337" +siteurl = "https://tgstation13.org/" + +[[servers]] +title = "tgstation13 - Bagil" +gameurl = "game.tgstation13.org:2337" +siteurl = "https://tgstation13.org/" diff --git a/main.go b/main.go index dee7eed..84a4f0d 100644 --- a/main.go +++ b/main.go @@ -3,53 +3,26 @@ 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") + fConfig = flag.String("config", "config.toml", "path to config file") + fDebug = flag.Bool("debug", false, "run in debug mode") ) func main() { flag.Parse() - db, e := ss13.OpenSqliteDB(*fDatabase) + ins, e := ss13.New(*fDebug, *fConfig) CheckError(e) - ins := &ss13.Instance{ - Debug: *fDebug, - DB: db, - PrivServersFile: *fPrivateServers, + if *fDebug { + Log("Updating servers every %d minutes", ins.Config.UpdateEvery) + Log("Listening on %s", ins.Config.ListenAddr) } - 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) + e = ins.Run() CheckError(e) } diff --git a/servers.json b/servers.json deleted file mode 100644 index 45c6b8f..0000000 --- a/servers.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "timeout": 30, - "pollservers": [ - { - "Title": "Baystation12", - "GameUrl": "baystation12.net:8000", - "SiteUrl": "http://baystation12.net/" - }, - { - "Title": "Apollo Station", - "GameUrl": "192.95.55.67:3333", - "SiteUrl": "https://apollo-community.org/" - }, - { - "Title": "tgstation13 - Sybil", - "GameUrl": "game.tgstation13.org:1337", - "SiteUrl": "https://tgstation13.org/" - }, - { - "Title": "tgstation13 - Bagil", - "GameUrl": "game.tgstation13.org:2337", - "SiteUrl": "https://tgstation13.org/" - } - ] -} diff --git a/src/config.go b/src/config.go index bda8ef9..0762a74 100644 --- a/src/config.go +++ b/src/config.go @@ -1,9 +1,6 @@ package ss13 -import ( - "encoding/json" - "io/ioutil" -) +import "github.com/BurntSushi/toml" type ServerConfig struct { Title string @@ -12,21 +9,28 @@ type ServerConfig struct { } type Config struct { - PollServers []ServerConfig - Timeout int + // Path to sqlite database file. + DatabasePath string + + // Serve web pages on this address. + ListenAddr string + + // List of "private" servers to manually poll for updates (private as + // in they do not show up on the byond hub page). + Servers []ServerConfig + + // Update all servers every x minutes. + UpdateEvery int + + // Timeout after x seconds, when trying to update a server. + UpdateTimeout int } func LoadConfig(path string) (*Config, error) { - data, err := ioutil.ReadFile(path) - if err != nil { - return nil, err + c := Config{} + _, e := toml.DecodeFile(path, &c) + if e != nil { + return nil, e } - - tmp := &Config{} - err = json.Unmarshal(data, &tmp) - if err != nil { - return nil, err - } - - return tmp, nil + return &c, nil } diff --git a/src/models.go b/src/models.go index 516327a..410552d 100644 --- a/src/models.go +++ b/src/models.go @@ -11,12 +11,10 @@ import ( type D map[string]interface{} type Instance struct { - // Settings required by the user. - Debug bool - DB *DB - PrivServersFile string + Debug bool + DB *DB + Config *Config - // Internal stuff addr string router *mux.Router tmpls *template.Template diff --git a/src/updater.go b/src/updater.go index 852bdfc..02dd87e 100644 --- a/src/updater.go +++ b/src/updater.go @@ -20,16 +20,13 @@ func (i *Instance) UpdateServers() { tx := i.DB.NewTransaction() - config, err := LoadConfig(i.PrivServersFile) + if i.Debug { + fmt.Println("\nPolling servers...") + } + polled, err := i.PollServers(i.Config.Servers, i.Config.UpdateTimeout) if !LogError(err) { - if i.Debug { - fmt.Println("\nPolling servers...") - } - polled, err := i.PollServers(config.PollServers, config.Timeout) - if !LogError(err) { - for _, s := range polled { - i.update_server(tx, s) - } + for _, s := range polled { + i.update_server(tx, s) } } diff --git a/src/web.go b/src/web.go index 4d61ba8..1ec4665 100644 --- a/src/web.go +++ b/src/web.go @@ -12,12 +12,40 @@ import ( "github.com/gorilla/mux" ) -func (i *Instance) Init() { - i.DB.InitSchema() +func New(debug bool, path string) (*Instance, error) { + c, e := LoadConfig(path) + if e != nil { + return nil, e + } + + db, e := OpenSqliteDB(c.DatabasePath) + if e != nil { + return nil, e + } + db.InitSchema() + + i := Instance{ + Debug: debug, + DB: db, + Config: c, + } + + return &i, nil } -func (i *Instance) Serve(addr string) error { - i.addr = addr +func (i *Instance) Run() error { + go func() { + td := time.Duration(i.Config.UpdateEvery) * time.Minute + for { + start := time.Now() + i.UpdateServers() + dur := time.Since(start) + if i.Debug { + fmt.Printf("Update completed in %s\n", dur) + } + time.Sleep(td) + } + }() i.router = mux.NewRouter().StrictSlash(true) i.router.NotFoundHandler = http.HandlerFunc(i.page_404) @@ -77,7 +105,7 @@ func (i *Instance) Serve(addr string) error { i.router.HandleFunc("/server/{id}", i.page_server) i.router.HandleFunc("/server/{id}/{slug}", i.page_server) - return http.ListenAndServe(i.addr, i.router) + return http.ListenAndServe(i.Config.ListenAddr, i.router) } func (i *Instance) page_404(w http.ResponseWriter, r *http.Request) {