-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
67 lines (54 loc) · 1.34 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
package main
import (
"fmt"
"os"
"path/filepath"
)
const CONFIG_DIR_ENV_VAR = "FIFTYMM_CONFIG_DIR"
const DEFAULT_CONFIG_DIR = "/etc/fiftymm/"
const PORT_ENV_VAR = "FIFTYMM_PORT"
const DEFAULT_PORT = "8080"
type App struct {
port string
configDir string
sites map[string]*Site
}
func NewApp() *App {
port := os.Getenv(PORT_ENV_VAR)
if port == "" {
port = DEFAULT_PORT
}
configDir := os.Getenv(CONFIG_DIR_ENV_VAR)
if configDir == "" {
configDir = DEFAULT_CONFIG_DIR
}
configFilesMap := make(map[string]*Site)
filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
// We only look at the top level files in the config dir
if info.Mode().IsDir() && path != configDir {
return filepath.SkipDir
}
if !(info.Mode().IsRegular() && filepath.Ext(path) == ".ini") {
return nil
}
siteConfig, loadErr := LoadSiteFromFile(path)
if loadErr != nil {
fmt.Printf("Unable to load config from file %s. Error: %s\n", path, loadErr.Error())
return nil
}
configFilesMap[siteConfig.Domain] = siteConfig
return nil
})
return &App{
port: port,
configDir: configDir,
sites: configFilesMap,
}
}
func (a *App) SiteForDomain(domain string) (*Site, error) {
if cs, ok := a.sites[domain]; !ok {
return nil, fmt.Errorf("No site configured for domain %s", domain)
} else {
return cs, nil
}
}