summaryrefslogtreecommitdiff
path: root/config.go
blob: cbe36a11c5b92a1dbbd40d58b936228b49a4c346 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main

import (
	"github.com/fsnotify/fsnotify"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"random.chars.jp/git/image-board/store"
)

var instance *store.Store

func configSetup() {
	// Configure configuration file parameters
	viper.SetConfigName("server")
	viper.SetConfigType("toml")
	viper.SetConfigPermissions(0600)
	viper.AddConfigPath(".")
	viper.AddConfigPath("/etc/imageboard/")
	viper.AddConfigPath("$HOME/.config/imageboard/")

	// Configure default values
	viper.SetDefault("system", map[string]interface{}{
		"loglevel":    "info",
		"store":       "./db",
		"single-user": true,
	})
	viper.SetDefault("server", map[string]interface{}{
		"host":  "127.0.0.1",
		"port":  int64(7777),
		"unix":  false,
		"proxy": true,
	})

	// Load configuration
	log.Info("Loading configuration file.")
	err := viper.ReadInConfig()
	if err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			err = viper.WriteConfigAs("server.toml")
			if err != nil {
				log.Fatalf("Error generating default configuration, %s", err)
			}
			log.Warn("Generated default server.toml in current directory.")
		} else {
			log.Fatalf("Error loading configuration, %s", err)
		}
	}

	if viper.ConfigFileUsed() != "" {
		log.Infof("Successfully loaded configuration %s, enabling watch.", viper.ConfigFileUsed())
	}
	viper.WatchConfig()
	viper.OnConfigChange(func(event fsnotify.Event) {
		if d {
			return
		}
		log.Infof("Configuration file %s updated.", event.Name)
		if viper.GetStringMap("server")["unix"] != serverConfig["unix"] ||
			viper.GetStringMap("server")["host"] != serverConfig["host"] ||
			viper.GetStringMap("server")["port"] != serverConfig["port"] ||
			viper.GetStringMap("system")["single-user"] != systemConfig["single-user"] {
			log.Warn("Configuration change requires restart.")
			cleanup(true)
			return
		}

		// Update configuration
		setLevel()
		serverConfig = viper.GetStringMap("server")
		systemConfig = viper.GetStringMap("system")
	})

	// Read initial config
	serverConfig = viper.GetStringMap("server")
	systemConfig = viper.GetStringMap("system")
}

func openStore() {
	path := viper.GetStringMap("system")["store"].(string)
	single, ok := viper.GetStringMap("system")["single-user"].(bool)
	if !ok {
		single = false
	}
	instance = store.New(path, single)
	if instance == nil {
		log.Fatalf("Error initializing store.")
	}
	log.Infof("Store opened on %s revision %v compat %v.", path, instance.Revision, instance.Compat)
	info := instance.User(instance.InitialUser)
	if info.Snowflake == instance.InitialUser {
		log.Infof("Initial user ID %s secret %s.", info.Snowflake, info.Secret)
		if instance.UserPasswordValidate(info.Snowflake, "initial") {
			log.Warnf("Initial user still has the initial password.")
		}
	} else {
		if single {
			log.Fatal("Instance has no initial user, single user mode unavailable.")
		}
	}
	if single {
		log.Infof("Server running in single user mode, all operations are performed as the initial user.")
	}
}