moth/cmd/mothd/main.go

61 lines
1.2 KiB
Go
Raw Normal View History

2019-09-02 19:47:24 -06:00
package main
import (
"github.com/namsral/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"
"net/http"
"time"
2019-09-02 19:47:24 -06:00
)
func main() {
log.Print("Started")
themePath := flag.String(
"theme",
"theme",
"Path to theme files",
)
statePath := flag.String(
"state",
"state",
"Path to state files",
)
puzzlePath := flag.String(
"mothballs",
"mothballs",
"Path to mothballs to host",
)
refreshInterval := flag.Duration(
"refresh",
2*time.Second,
"Duration between maintenance tasks",
)
bindStr := flag.String(
"bind",
":8000",
"Bind [host]:port for HTTP service",
)
2019-12-01 18:58:09 -07:00
stateFs := afero.NewBasePathFs(afero.NewOsFs(), *statePath)
2019-12-05 22:25:03 -07:00
themeFs := afero.NewBasePathFs(afero.NewOsFs(), *themePath)
2019-12-01 18:58:09 -07:00
2019-12-05 22:25:03 -07:00
theme := NewTheme(themeFs)
2019-12-01 18:58:09 -07:00
state := NewState(stateFs)
puzzles := NewMothballs(*puzzlePath)
go state.Run(*refreshInterval)
go puzzles.Run(*refreshInterval)
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")
http.HandleFunc("/", theme.staticHandler)
log.Printf("Listening on %s", *bindStr)
log.Fatal(http.ListenAndServe(*bindStr, nil))
}