aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 6bbe8401c9fb131a92034368955d3da29e030849 (plain) (blame)
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
package main

import (
	"encoding/json"
	"log"
	"os"
)

const latestSerializeVer = 0x1c
const latestProtoVer = 39
const maxPlayerNameLen = 20
const playerNameChars = "^[a-zA-Z0-9-_]+$"
const bytesPerMediaBunch = 5000

const defaultSendInterval = 0.09
const defaultUserLimit = 10
const defaultAuthBackend = "sqlite3"
const defaultBindAddr = ":40000"

var conf Config

type Config struct {
	RequirePasswd bool
	SendInterval  float32
	UserLimit     int
	AuthBackend   string
	BindAddr      string
	Servers       []struct {
		Name string
		Addr string
	}
}

func loadConfig() error {
	oldConf := conf

	conf.SendInterval = defaultSendInterval
	conf.UserLimit = defaultUserLimit
	conf.AuthBackend = defaultAuthBackend
	conf.BindAddr = defaultBindAddr

	f, err := os.OpenFile("config.json", os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		conf = oldConf
		return err
	}
	defer f.Close()

	if fi, _ := f.Stat(); fi.Size() == 0 {
		f.WriteString("{\n\n}\n")
		f.Seek(0, os.SEEK_SET)
	}

	decoder := json.NewDecoder(f)
	if err := decoder.Decode(&conf); err != nil {
		conf = oldConf
		return err
	}

	log.Print("{←|⇶} load config")
	return nil
}