2019-09-02 19:47:24 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-29 16:59:44 -07:00
|
|
|
"flag"
|
2020-09-15 15:58:21 -06:00
|
|
|
"fmt"
|
2020-12-02 17:57:10 -07:00
|
|
|
"log"
|
2019-12-05 22:25:03 -07:00
|
|
|
"mime"
|
2020-09-15 15:58:21 -06:00
|
|
|
"os"
|
2024-04-09 11:54:39 -06:00
|
|
|
"path/filepath"
|
2019-11-24 15:58:43 -07:00
|
|
|
"time"
|
2020-08-14 20:26:04 -06:00
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
2019-09-02 19:47:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
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",
|
2020-08-17 17:43:57 -06:00
|
|
|
"Path to mothball files",
|
2019-11-24 15:58:43 -07:00
|
|
|
)
|
2020-09-08 17:49:02 -06:00
|
|
|
puzzlePath := flag.String(
|
|
|
|
"puzzles",
|
|
|
|
"",
|
2020-09-11 13:03:19 -06:00
|
|
|
"Path to puzzles tree (enables development mode)",
|
2020-09-08 17:49:02 -06:00
|
|
|
)
|
2019-11-24 15:58:43 -07:00
|
|
|
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-09-15 15:58:21 -06:00
|
|
|
seed := flag.String(
|
|
|
|
"seed",
|
|
|
|
"",
|
|
|
|
"Random seed to use, overrides $SEED",
|
|
|
|
)
|
2020-02-29 16:59:44 -07:00
|
|
|
flag.Parse()
|
2019-11-24 15:58:43 -07:00
|
|
|
|
2024-04-09 11:54:39 -06:00
|
|
|
var theme *Theme
|
2020-09-08 17:49:02 -06:00
|
|
|
osfs := afero.NewOsFs()
|
2024-04-09 11:54:39 -06:00
|
|
|
if p, err := filepath.Abs(*themePath); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
theme = NewTheme(afero.NewBasePathFs(osfs, p))
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
|
|
|
|
config := Configuration{}
|
|
|
|
|
|
|
|
var provider PuzzleProvider
|
2024-04-09 11:54:39 -06:00
|
|
|
if p, err := filepath.Abs(*mothballPath); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
provider = NewMothballs(afero.NewBasePathFs(osfs, p))
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
if *puzzlePath != "" {
|
2024-04-09 11:54:39 -06:00
|
|
|
if p, err := filepath.Abs(*puzzlePath); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
provider = NewTranspilerProvider(afero.NewBasePathFs(osfs, p))
|
|
|
|
}
|
2020-09-08 17:49:02 -06:00
|
|
|
config.Devel = true
|
2020-12-02 17:57:10 -07:00
|
|
|
log.Println("-=- You are in development mode, champ! -=-")
|
2020-09-08 17:49:02 -06:00
|
|
|
}
|
2019-11-24 15:58:43 -07:00
|
|
|
|
2020-12-02 19:31:34 -07:00
|
|
|
var state StateProvider
|
2024-04-09 11:54:39 -06:00
|
|
|
if p, err := filepath.Abs(*statePath); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
state = NewState(afero.NewBasePathFs(osfs, p))
|
|
|
|
}
|
2020-12-02 19:31:34 -07:00
|
|
|
if config.Devel {
|
|
|
|
state = NewDevelState(state)
|
|
|
|
}
|
|
|
|
|
2020-12-02 17:57:10 -07:00
|
|
|
// Set random seed
|
|
|
|
if *seed == "" {
|
|
|
|
*seed = os.Getenv("SEED")
|
|
|
|
}
|
|
|
|
if *seed == "" {
|
|
|
|
*seed = fmt.Sprintf("%d%d", os.Getpid(), time.Now().Unix())
|
|
|
|
}
|
|
|
|
os.Setenv("SEED", *seed)
|
|
|
|
log.Print("SEED=", *seed)
|
|
|
|
|
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-08-18 17:04:23 -06:00
|
|
|
go theme.Maintain(*refreshInterval)
|
|
|
|
go state.Maintain(*refreshInterval)
|
2020-09-08 17:49:02 -06:00
|
|
|
go provider.Maintain(*refreshInterval)
|
2019-12-07 21:17:13 -07:00
|
|
|
|
2020-09-08 17:49:02 -06:00
|
|
|
server := NewMothServer(config, theme, state, provider)
|
2020-03-01 16:10:55 -07:00
|
|
|
httpd := NewHTTPServer(*base, server)
|
2020-08-19 18:01:21 -06:00
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
httpd.Run(*bindStr)
|
2019-11-24 15:58:43 -07:00
|
|
|
}
|