~nromdotcom/gemif

ref: 4537a2f30ac534f927315780f6176cd14ba1cbe4 gemif/cmd/gemif/main.go -rw-r--r-- 1.8 KiB
4537a2f3Norm MacLennan Update story 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//go:generate pkger -o cmd/gemif
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"git.sr.ht/~nromdotcom/gemif/pkg/gamemanager"
	"git.sr.ht/~nromdotcom/gemif/pkg/gemserver"
	"github.com/pelletier/go-toml"
)

//nolint:gochecknoglobals
var (
	appName    string = "GemIF"
	appVersion string = "0.0.0"
	appCommit  string = "local"
	buildTime  string = "just now!"
)

func main() {
	versionFlag := flag.Bool("version", false, "Print version information and exit")
	flag.Parse()

	if *versionFlag {
		fmt.Printf("%s %s (%s) - built %s\n\n", appName, appVersion, appCommit, buildTime)
		os.Exit(0)
	}

	file, err := ioutil.ReadFile("./config.toml")
	if err != nil {
		log.Fatalf("Couldn't load config file: %s", err)
	}

	config, err := toml.Load(string(file))
	if err != nil {
		log.Fatalf("Could not parse config file: %s", err)
	}

	book, err := gamemanager.NewStoryBook(config.Get("engine.stories_dir").(string))
	if err != nil {
		log.Fatalf("Error loading stories! %s", err)
	}

	renderDesc := config.Get("engine.render_descriptions").(bool)
	tokenFormat := config.Get("engine.statetoken_format").(string)

	engineConfig, err := gamemanager.NewEngineConfig(renderDesc, tokenFormat)
	if err != nil {
		log.Fatalf("Error loading config: %s", err)
	}

	gameMgr, err := gamemanager.NewGameManager(book, engineConfig)
	if err != nil {
		log.Fatalf("Error loading game! %s", err)
	}

	gsConfig := config.Get("gemserver").(*toml.Tree)
	serverConfig := gemserver.ServerConfig{
		Domain:   gsConfig.Get("domain").(string),
		Port:     gsConfig.Get("port").(int64),
		CertFile: gsConfig.Get("cert_file").(string),
		KeyFile:  gsConfig.Get("key_file").(string),
		Version:  appVersion,
	}

	fmt.Printf("Starting %s %s (%s) - built %s\n\n", appName, appVersion, appCommit, buildTime)

	gemserver.StartRouter(gameMgr, serverConfig)
}