-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
78 lines (65 loc) · 1.75 KB
/
configuration.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
package main
import (
"github.com/bitly/go-simplejson"
"github.com/froozen/go-helpers"
"os"
"reflect"
"strconv"
"strings"
)
var (
addressString, root string
delay int
args, hooks, fileTypes []string
dataDir = os.Getenv("HOME") + "/.web-stream/"
)
// Load config loads the configuration from the configuration file
func LoadConfig() {
// Open the config file
file, err := os.Open(dataDir + "config.json")
if err != nil {
helpers.Fail("Error:", dataDir+"config.json", "doesn't exist")
}
defer file.Close()
// Parse the json
json, err := simplejson.NewFromReader(file)
if err != nil {
helpers.Fail("Error: Parsing the configuration failed:", err)
}
// Load the "port" value and append it to ":" to get a valid address
// for listening and serving
addressString = ":" + strconv.Itoa(json.Get("port").MustInt(2223))
// Get the "root" value
root = json.Get("root").MustString(os.Getenv("HOME") + "/Videos/")
if !helpers.IsDir(root) {
helpers.Fail("Error:", root, "is no directory")
}
// Make sure root ends with a "/"
if !strings.HasSuffix(root, "/") {
root += "/"
}
// Load the "delay" value
delay = json.Get("delay").MustInt(3)
// Load the "args" value
args = StringSlice(json.Get("args"))
// Load the "hooks" value
hooks = StringSlice(json.Get("hooks"))
// Load the "filetypes" value
fileTypes = StringSlice(json.Get("filetypes"))
}
// StringSlice creates a string slice from a simplejson.Json value
func StringSlice(json *simplejson.Json) (slice []string) {
// Extract the array
array, err := json.Array()
if err != nil {
return
}
// Add all strings to the slice
for _, item := range array {
v := reflect.ValueOf(item)
if v.Kind() == reflect.String {
slice = append(slice, v.String())
}
}
return
}