moth/cmd/mothd/main.go

163 lines
3.4 KiB
Go
Raw Permalink Normal View History

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"
"log"
2019-12-05 22:25:03 -07:00
"mime"
2020-09-15 15:58:21 -06:00
"os"
2021-10-28 16:36:07 -06:00
"strconv"
"time"
2020-08-14 20:26:04 -06:00
"github.com/spf13/afero"
2019-09-02 19:47:24 -06:00
)
func main() {
themePath := flag.String(
"theme",
"theme",
"Path to theme files",
)
statePath := flag.String(
"state",
"state",
"Path to state files",
)
mothballPath := flag.String(
"mothballs",
"mothballs",
2020-08-17 17:43:57 -06:00
"Path to mothball files",
)
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
)
refreshInterval := flag.Duration(
"refresh",
2*time.Second,
"Duration between maintenance tasks",
)
bindStr := flag.String(
"bind",
2020-02-29 16:59:44 -07:00
":8080",
"Bind [host]:port for HTTP service",
)
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",
)
2021-10-28 16:36:07 -06:00
stateEngine := flag.String(
"state-engine",
"legacy",
"Specifiy a state engine",
)
redis_url := flag.String(
"redis-url",
"",
"URL for Redis state instance",
)
redis_db := flag.Uint64(
"redis-db",
^uint64(0),
"Database number for Redis state instance",
)
redis_instance_id := flag.String(
"redis-instance-id",
"",
"Unique (per-cluster) instance ID",
)
2020-02-29 16:59:44 -07:00
flag.Parse()
2020-09-08 17:49:02 -06:00
osfs := afero.NewOsFs()
theme := NewTheme(afero.NewBasePathFs(osfs, *themePath))
config := Configuration{}
var provider PuzzleProvider
provider = NewMothballs(afero.NewBasePathFs(osfs, *mothballPath))
if *puzzlePath != "" {
provider = NewTranspilerProvider(afero.NewBasePathFs(osfs, *puzzlePath))
config.Devel = true
log.Println("-=- You are in development mode, champ! -=-")
2020-09-08 17:49:02 -06:00
}
var state StateProvider
2021-10-28 16:36:07 -06:00
switch engine := *stateEngine; engine {
case "redis":
2021-12-10 17:13:34 -07:00
log.Println("Moth is running with Redis")
2021-10-28 16:36:07 -06:00
redis_url_parsed := *redis_url
if redis_url_parsed == "" {
redis_url_parsed = os.Getenv("REDIS_URL")
if redis_url_parsed == "" {
log.Fatal("Redis mode was selected, but --redis-url or REDIS_URL were not set")
}
}
redis_db_parsed := *redis_db
if redis_db_parsed == ^uint64(0) {
redis_db_parsed_inner, err := strconv.ParseUint(os.Getenv("REDIS_DB"), 10, 64)
redis_db_parsed = redis_db_parsed_inner
if err != nil {
redis_db_parsed = 0
}
}
redis_instance_id_parsed := *redis_instance_id
if redis_instance_id_parsed == "" {
redis_instance_id_parsed = os.Getenv("REDIS_INSTANCE_ID")
if redis_instance_id_parsed == "" {
log.Fatal("Redis mode was selected, but --redis-instance-id or REDIS_INSTANCE_ID were not set")
2021-10-28 16:36:07 -06:00
}
}
state = NewRedisState(redis_url_parsed, int(redis_db_parsed), redis_instance_id_parsed)
2021-10-28 16:36:07 -06:00
default:
case "legacy":
2021-12-10 17:13:34 -07:00
log.Println("Moth is running with the legacy state engine")
2021-10-28 16:36:07 -06:00
state = NewState(afero.NewBasePathFs(osfs, *statePath))
}
if config.Devel {
state = NewDevelState(state)
}
// 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
httpd.Run(*bindStr)
}