-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
128 lines (116 loc) · 2.8 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
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
119
120
121
122
123
124
125
126
127
128
package settings
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
)
var (
ServiceSettings *settings
)
const fileName string = "./swap.json"
// const FilesDirPath string = "./files/"
const MinFilesDirPath string = "min/"
type settings struct {
Backend struct {
Host string `json:"host"`
SSLHost string `json:"sslhost"`
SecretKeyForToken string `json:"secret_key_for_token"`
FilesPath string `json:"files_path"`
FileLogs bool `json:"fileLogs"`
} `json:"backend"`
Cert struct {
Org string `json:"org"`
Hosts []string `json:"hosts"`
RsaBits int `json:"rsa-bits"`
} `json:"cert"`
Service struct {
MaxFileSize int64 `json:"max_file_size_byte"`
MaxMinutesForFile int64 `json:"max_minutes_available_for_files_download"`
CORS bool `json:"cors"`
} `json:"service"`
DB struct {
SQLite struct {
Path string `json:"file_path"`
} `json:"sqlite"`
} `json:"db"`
}
func createConfig() (data []byte, err error) {
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return
}
randBytes := make([]byte, 16)
_, err = rand.Read(randBytes)
if err != nil {
panic("Settings -> Cannot fill random bytes -> " + err.Error())
}
defaultConfig := fmt.Sprintf(`{
"backend":{
"fileLogs": true,
"host": "80",
"sslhost": "443",
"secret_key_for_token": "%s",
"files_path": "./files/"
},
"cert":{
"org": "Example Co",
"hosts": ["192.168.1.38","localhost","127.0.0.1"],
"rsa-bits":2048
},
"service":{
"max_file_size_byte": 104857600,
"max_minutes_available_for_files_download": 5,
"cors": false
},
"db":{
"sqlite":{
"file_path": "swap.db"
}
}
}`, base64.StdEncoding.EncodeToString(randBytes))
_, err = f.Write([]byte(defaultConfig))
f.Close()
if err != nil {
log.Println("Cannot write config")
return
}
b, err := ioutil.ReadFile(fileName)
return b, err
}
func LoadSettings() error {
//Read settings file
b, err := ioutil.ReadFile(fileName) // just pass the file name
if err != nil {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
b, err = createConfig()
if err != nil {
return errors.New("Strange creating config problem -> " + err.Error())
}
} else {
return errors.New("Strange config problem -> " + err.Error())
}
}
err = json.Unmarshal(b, &ServiceSettings)
if err != nil {
fmt.Println("Config unmarshaling error")
return err
}
if _, err := os.Stat(ServiceSettings.Backend.FilesPath + MinFilesDirPath); os.IsNotExist(err) {
os.MkdirAll(ServiceSettings.Backend.FilesPath+MinFilesDirPath, os.ModePerm)
}
return nil
}
func GetSettings() (*settings, error) {
if ServiceSettings == nil {
err := LoadSettings()
if err != nil {
return nil, err
}
}
return ServiceSettings, nil
}