-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
74 lines (66 loc) · 1.64 KB
/
config.go
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/BurntSushi/toml"
)
type Config struct {
Port int
Hostname string
RootDir string
UserDirEnable bool
UserDir string
UserSubdomains bool
DirlistEnable bool
DirlistReverse bool
DirlistSort string
DirlistTitles bool
CGIPaths []string
UserCGIEnable bool
}
var defaultConf = &Config{
Port: 300,
Hostname: "localhost",
RootDir: "/var/spartan/",
DirlistEnable: true,
DirlistReverse: false,
DirlistSort: "name",
DirlistTitles: true,
UserDirEnable: true,
UserDir: "public_spartan",
UserSubdomains: false,
CGIPaths: []string{"cgi/"},
UserCGIEnable: false, // Turned off by default because scripts are run by server user as of now
}
func LoadConfig(path string) (*Config, error) {
var err error
var conf Config
// Defaults
conf = *defaultConf
_, err = os.Stat(path)
if os.IsNotExist(err) {
fmt.Println(path, "does not exist, using default configuration values")
return &conf, nil
}
f, err := os.Open(path)
if err == nil {
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
if _, err = toml.Decode(string(contents), &conf); err != nil {
return nil, err
}
}
// Config validation
if conf.DirlistSort != "name" && conf.DirlistSort != "time" && conf.DirlistSort != "size" {
fmt.Println("Warning: DirlistSort config option is not one of name/time/size, defaulting to name.")
conf.DirlistSort = "name"
}
// Strip trailing '/' so /~user to /~user/ redirects can work
conf.UserDir = strings.TrimRight(conf.UserDir, "/")
return &conf, nil
}