-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
103 lines (93 loc) · 2.59 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
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 (
"fmt"
"log"
"net"
"os"
"strconv"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func loadConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath(".") // optionally look for config in the working directory
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
log.Println("Config not found!")
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("Fatal error config file: %w \n", err))
}
}
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
// TODO: Reload config on change
})
viper.WatchConfig()
// ----------------------------------------------------------
// General config
// ----------------------------------------------------------
host := viper.Get("host")
protocol := viper.Get("protocol")
port := viper.Get("port")
if protocol == nil {
rootShop = "http"
} else {
rootShop = protocol.(string)
}
rootShop = rootShop + "://"
if host == nil {
// Retrieve current IP
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
var myIp = ""
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
if myIp == "" {
myIp = ipv4.String()
}
}
}
rootShop = rootShop + myIp
} else {
rootShop = rootShop + host.(string)
}
if port == nil {
rootShop = rootShop + ":3000"
} else {
rootShop = rootShop + ":" + strconv.Itoa(port.(int))
}
// ----------------------------------------------------------
// Debug
// ----------------------------------------------------------
debugNfs = viper.GetBool("debug.nfs")
// ----------------------------------------------------------
// Sources
// ----------------------------------------------------------
// Directories
cfgDirectories := viper.GetStringSlice("sources.directories")
if cfgDirectories == nil {
// Default search
directories = make([]string, 0)
directories = append(directories, "./games")
} else {
directories = cfgDirectories
}
// NFS
cfgNfs := viper.GetStringSlice("sources.nfs")
if cfgNfs != nil {
nfsShares = cfgNfs
}
// ----------------------------------------------------------
// Shop Template
// ----------------------------------------------------------
shopTitle := viper.GetString("name")
if shopTitle == "" {
shopTitle = "TinShop"
}
shopTemplateData = ShopTemplate{
ShopTitle: shopTitle,
}
}