2019-09-02 19:47:24 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-29 16:59:44 -07:00
|
|
|
"flag"
|
2019-12-01 18:58:09 -07:00
|
|
|
"github.com/spf13/afero"
|
2019-09-02 19:47:24 -06:00
|
|
|
"log"
|
2019-12-05 22:25:03 -07:00
|
|
|
"mime"
|
2019-11-24 15:58:43 -07:00
|
|
|
"time"
|
2019-09-02 19:47:24 -06:00
|
|
|
)
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
func custodian(updateInterval time.Duration, components []Component) {
|
|
|
|
update := func() {
|
|
|
|
for _, c := range components {
|
|
|
|
c.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(updateInterval)
|
|
|
|
update()
|
|
|
|
for _ = range ticker.C {
|
|
|
|
update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:47:24 -06:00
|
|
|
func main() {
|
|
|
|
log.Print("Started")
|
2019-11-24 15:58:43 -07:00
|
|
|
|
|
|
|
themePath := flag.String(
|
|
|
|
"theme",
|
|
|
|
"theme",
|
|
|
|
"Path to theme files",
|
|
|
|
)
|
|
|
|
statePath := flag.String(
|
|
|
|
"state",
|
|
|
|
"state",
|
|
|
|
"Path to state files",
|
|
|
|
)
|
2020-02-29 16:51:32 -07:00
|
|
|
mothballPath := flag.String(
|
2019-11-24 15:58:43 -07:00
|
|
|
"mothballs",
|
|
|
|
"mothballs",
|
|
|
|
"Path to mothballs to host",
|
|
|
|
)
|
|
|
|
refreshInterval := flag.Duration(
|
|
|
|
"refresh",
|
|
|
|
2*time.Second,
|
|
|
|
"Duration between maintenance tasks",
|
|
|
|
)
|
|
|
|
bindStr := flag.String(
|
|
|
|
"bind",
|
2020-02-29 16:59:44 -07:00
|
|
|
":8080",
|
2019-11-24 15:58:43 -07:00
|
|
|
"Bind [host]:port for HTTP service",
|
|
|
|
)
|
2020-02-29 16:51:32 -07:00
|
|
|
base := flag.String(
|
|
|
|
"base",
|
|
|
|
"/",
|
|
|
|
"Base URL of this instance",
|
|
|
|
)
|
2020-02-29 16:59:44 -07:00
|
|
|
flag.Parse()
|
2019-11-24 15:58:43 -07:00
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
theme := NewTheme(afero.NewBasePathFs(afero.NewOsFs(), *themePath))
|
|
|
|
state := NewState(afero.NewBasePathFs(afero.NewOsFs(), *statePath))
|
|
|
|
puzzles := NewMothballs(afero.NewBasePathFs(afero.NewOsFs(), *mothballPath))
|
2019-11-24 15:58:43 -07:00
|
|
|
|
2019-12-07 21:17:13 -07:00
|
|
|
// Add some MIME extensions
|
|
|
|
// Doing this avoids decompressing a mothball entry twice per request
|
|
|
|
mime.AddExtensionType(".json", "application/json")
|
|
|
|
mime.AddExtensionType(".zip", "application/zip")
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
go custodian(*refreshInterval, []Component{theme, state, puzzles})
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
httpd := NewHTTPServer(*base, theme, state, puzzles)
|
|
|
|
httpd.Run(*bindStr)
|
2019-11-24 15:58:43 -07:00
|
|
|
}
|