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:
parent
d875142d31
commit
d2af505b0c
27
config.toml
Normal file
27
config.toml
Normal 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/"
|
||||||
39
main.go
39
main.go
@ -3,53 +3,26 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/lmas/ss13_se/src"
|
"github.com/lmas/ss13_se/src"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fAddr = flag.String("addr", ":8000", "address to listen on, for the web server")
|
fConfig = flag.String("config", "config.toml", "path to config file")
|
||||||
fDatabase = flag.String("database", "./ss13.db", "database file")
|
|
||||||
fDebug = flag.Bool("debug", false, "run in debug mode")
|
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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
db, e := ss13.OpenSqliteDB(*fDatabase)
|
ins, e := ss13.New(*fDebug, *fConfig)
|
||||||
CheckError(e)
|
CheckError(e)
|
||||||
|
|
||||||
ins := &ss13.Instance{
|
if *fDebug {
|
||||||
Debug: *fDebug,
|
Log("Updating servers every %d minutes", ins.Config.UpdateEvery)
|
||||||
DB: db,
|
Log("Listening on %s", ins.Config.ListenAddr)
|
||||||
PrivServersFile: *fPrivateServers,
|
|
||||||
}
|
}
|
||||||
ins.Init()
|
e = ins.Run()
|
||||||
|
|
||||||
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)
|
CheckError(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
25
servers.json
25
servers.json
@ -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/"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@ -1,9 +1,6 @@
|
|||||||
package ss13
|
package ss13
|
||||||
|
|
||||||
import (
|
import "github.com/BurntSushi/toml"
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Title string
|
Title string
|
||||||
@ -12,21 +9,28 @@ type ServerConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PollServers []ServerConfig
|
// Path to sqlite database file.
|
||||||
Timeout int
|
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) {
|
func LoadConfig(path string) (*Config, error) {
|
||||||
data, err := ioutil.ReadFile(path)
|
c := Config{}
|
||||||
if err != nil {
|
_, e := toml.DecodeFile(path, &c)
|
||||||
return nil, err
|
if e != nil {
|
||||||
|
return nil, e
|
||||||
}
|
}
|
||||||
|
return &c, nil
|
||||||
tmp := &Config{}
|
|
||||||
err = json.Unmarshal(data, &tmp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tmp, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,12 +11,10 @@ import (
|
|||||||
type D map[string]interface{}
|
type D map[string]interface{}
|
||||||
|
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
// Settings required by the user.
|
|
||||||
Debug bool
|
Debug bool
|
||||||
DB *DB
|
DB *DB
|
||||||
PrivServersFile string
|
Config *Config
|
||||||
|
|
||||||
// Internal stuff
|
|
||||||
addr string
|
addr string
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
tmpls *template.Template
|
tmpls *template.Template
|
||||||
|
|||||||
@ -20,18 +20,15 @@ func (i *Instance) UpdateServers() {
|
|||||||
|
|
||||||
tx := i.DB.NewTransaction()
|
tx := i.DB.NewTransaction()
|
||||||
|
|
||||||
config, err := LoadConfig(i.PrivServersFile)
|
|
||||||
if !LogError(err) {
|
|
||||||
if i.Debug {
|
if i.Debug {
|
||||||
fmt.Println("\nPolling servers...")
|
fmt.Println("\nPolling servers...")
|
||||||
}
|
}
|
||||||
polled, err := i.PollServers(config.PollServers, config.Timeout)
|
polled, err := i.PollServers(i.Config.Servers, i.Config.UpdateTimeout)
|
||||||
if !LogError(err) {
|
if !LogError(err) {
|
||||||
for _, s := range polled {
|
for _, s := range polled {
|
||||||
i.update_server(tx, s)
|
i.update_server(tx, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if i.Debug {
|
if i.Debug {
|
||||||
fmt.Println("\nScraping servers...")
|
fmt.Println("\nScraping servers...")
|
||||||
|
|||||||
38
src/web.go
38
src/web.go
@ -12,12 +12,40 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (i *Instance) Init() {
|
func New(debug bool, path string) (*Instance, error) {
|
||||||
i.DB.InitSchema()
|
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 {
|
func (i *Instance) Run() error {
|
||||||
i.addr = addr
|
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 = mux.NewRouter().StrictSlash(true)
|
||||||
i.router.NotFoundHandler = http.HandlerFunc(i.page_404)
|
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}", i.page_server)
|
||||||
i.router.HandleFunc("/server/{id}/{slug}", 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) {
|
func (i *Instance) page_404(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user