Add TOML config and clean up startup.

- Removed servers.json and it's config code.
- Load the new config.toml, using TOML instead of JSON.
- Moved most of the cli flags to the config file.
- Simplified creation of a new server instance (using New()).
- Goroutine running updates was moved inside the lib.
This commit is contained in:
A. Svensson 2016-06-19 15:21:28 +02:00
parent d875142d31
commit d2af505b0c
7 changed files with 97 additions and 95 deletions

27
config.toml Normal file
View File

@ -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/"

41
main.go
View File

@ -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)
}

View File

@ -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/"
}
]
}

View File

@ -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
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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) {