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
|
package main
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/BurntSushi/toml"
)
const (
defaultConfigPath = "/etc/shavit/config.toml"
)
const (
configFlagUsage = "A custom path to the server configuration file"
)
// Flags contain all the flags that were passed to the program
type Flags struct {
ConfigFile string
}
func getFlags() (Flags, error) {
var f Flags
flag.StringVar(&f.ConfigFile, "config", defaultConfigPath, configFlagUsage)
flag.Parse()
return f, nil
}
// Config holds the main configuration data for the server
type Config struct {
// can be relative or absolute path
SourceDir string `toml:"source"`
// default to ["index.gmi"]
IndexFiles []string `toml:"index_files"`
// default to 5
ExecTimeout int64 `toml:"exec_timeout"`
// default to false because the content might not be trusted
ExecuteFiles bool
TLSCert string `toml:"tls_certificate"`
TLSKey string `toml:"tls_key"`
}
func getConfig(path string) (Config, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf("failed to read config file: %v", err)
}
var cfg Config
err = toml.Unmarshal(raw, &cfg)
if err != nil {
return cfg, fmt.Errorf("failed to parse config file: %v", err)
}
cfg.SourceDir, err = filepath.Abs(cfg.SourceDir)
if err != nil {
return cfg, fmt.Errorf("failed to get absolute source dir: %v", err)
}
if len(cfg.IndexFiles) == 0 {
cfg.IndexFiles = []string{"index.gmi"}
}
if cfg.ExecTimeout == 0 {
cfg.ExecTimeout = 5
}
return cfg, nil
}
|