forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
118 lines (110 loc) · 3.09 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package configuration
import (
"encoding/json"
"errors"
"github.com/kabukky/journey/filenames"
"io/ioutil"
"log"
"reflect"
"strings"
)
// Configuration: settings that are neccesary for server configuration
type Configuration struct {
HttpHostAndPort string
HttpsHostAndPort string
HttpsUsage string
Url string
HttpsUrl string
}
func NewConfiguration() *Configuration {
var config Configuration
err := config.load()
if err != nil {
log.Println("Warning: couldn't load " + filenames.ConfigFilename + ", creating new config file.")
err = config.create()
if err != nil {
log.Fatal("Fatal error: Couldn't create configuration.")
return nil
}
err = config.load()
if err != nil {
log.Fatal("Fatal error: Couldn't load configuration.")
return nil
}
}
return &config
}
// Global config - thread safe and accessible from all packages
var Config = NewConfiguration()
func (c *Configuration) save() error {
data, err := json.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(filenames.ConfigFilename, data, 0600)
}
func (c *Configuration) load() error {
configWasChanged := false
data, err := ioutil.ReadFile(filenames.ConfigFilename)
if err != nil {
return err
}
err = json.Unmarshal(data, c)
if err != nil {
return err
}
// Make sure the url is in the right format
// Make sure there is no trailing slash at the end of the url
if strings.HasSuffix(c.Url, "/") {
c.Url = c.Url[0 : len(c.Url)-1]
configWasChanged = true
}
if !strings.HasPrefix(c.Url, "http://") && !strings.HasPrefix(c.Url, "https://") {
c.Url = "http://" + c.Url
configWasChanged = true
}
// Make sure the https url is in the right format
// Make sure there is no trailing slash at the end of the https url
if strings.HasSuffix(c.HttpsUrl, "/") {
c.HttpsUrl = c.HttpsUrl[0 : len(c.HttpsUrl)-1]
configWasChanged = true
}
if strings.HasPrefix(c.HttpsUrl, "http://") {
c.HttpsUrl = strings.Replace(c.HttpsUrl, "http://", "https://", 1)
configWasChanged = true
} else if !strings.HasPrefix(c.HttpsUrl, "https://") {
c.HttpsUrl = "https://" + c.HttpsUrl
configWasChanged = true
}
// Make sure there is no trailing slash at the end of the url
if strings.HasSuffix(c.HttpsUrl, "/") {
c.HttpsUrl = c.HttpsUrl[0 : len(c.HttpsUrl)-1]
configWasChanged = true
}
// Check if all fields are filled out
cReflected := reflect.ValueOf(*c)
for i := 0; i < cReflected.NumField(); i++ {
if cReflected.Field(i).Interface() == "" {
log.Println("Error: " + filenames.ConfigFilename + " is corrupted. Did you fill out all of the fields?")
return errors.New("Error: Configuration corrupted.")
}
}
// Save the changed config
if configWasChanged {
err = c.save()
if err != nil {
return err
}
}
return nil
}
func (c *Configuration) create() error {
// TODO: Change default port
c = &Configuration{HttpHostAndPort: ":8084", HttpsHostAndPort: ":8085", HttpsUsage: "None", Url: "127.0.0.1:8084", HttpsUrl: "127.0.0.1:8085"}
err := c.save()
if err != nil {
log.Println("Error: couldn't create " + filenames.ConfigFilename)
return err
}
return nil
}