-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
62 lines (53 loc) · 1.45 KB
/
settings.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
package app
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
)
// Settings represents the settings of App
type Settings struct {
GeneratedPath string `json:"generated_path,omitempty"`
ServerPort int `json:"server_port,omitempty"`
FileServerPort int `json:"file_server_port,omitempty"`
GeneratorSettings *GeneratorSettings `json:"generator_settings,omitempty"`
Content interface{} `json:"content,omitempty"`
}
// GeneratorSettings represents the settings for Generating files
type GeneratorSettings struct {
Concurrency int `json:"concurrency,omitempty"`
}
// DefaultSettings returns the default settings of the App
func DefaultSettings() *Settings {
generatedPath := os.Getenv("GENERATED_PATH")
if generatedPath == "" {
generatedPath = "./generated"
}
return &Settings{
generatedPath,
8080,
3000,
&GeneratorSettings{
10,
},
nil,
}
}
// SettingsFromFile loads settings from the given file path into the given Settings
func SettingsFromFile(path string, settings interface{}, log logrus.FieldLogger) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
log.Warnf("%v not found, using defaults...", path)
return
}
file, err := ioutil.ReadFile(path)
if err != nil {
log.Warnf("error reading %v, using defaults...", path)
return
}
err = json.Unmarshal(file, settings)
if err != nil {
log.Warnf("error reading %v, using defaults...", path)
return
}
}