forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
191 lines (176 loc) · 4.88 KB
/
configs.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"errors"
"fmt"
"github.com/go-yaml/yaml"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"io/ioutil"
"os"
)
// ErrorResponse is used for HTTP errors to show to User
type ErrorResponse struct {
Error string
}
// LoadConfigFile will attempt to load the 'config.yml' file in a specific directory
func LoadConfigFile(directory string) (*DbConfig, error) {
var configs *DbConfig
if os.Getenv("DB_CONN") != "" {
utils.Log(1, "DB_CONN environment variable was found, waiting for database...")
return LoadUsingEnv()
}
file, err := ioutil.ReadFile(directory + "/config.yml")
if err != nil {
return nil, errors.New("config.yml file not found at " + directory + "/config.yml - starting in setup mode")
}
err = yaml.Unmarshal(file, &configs)
if err != nil {
return nil, err
}
Configs = configs
return Configs, err
}
// LoadUsingEnv will attempt to load database configs based on environment variables. If DB_CONN is set if will force this function.
func LoadUsingEnv() (*DbConfig, error) {
Configs = new(DbConfig)
Configs.LocalIP = GetLocalIP()
if os.Getenv("DB_CONN") == "" {
return Configs, errors.New("Missing DB_CONN environment variable")
}
if os.Getenv("DB_CONN") != "sqlite" {
if os.Getenv("DB_HOST") == "" {
return Configs, errors.New("Missing DB_HOST environment variable")
}
if os.Getenv("DB_USER") == "" {
return Configs, errors.New("Missing DB_USER environment variable")
}
if os.Getenv("DB_PASS") == "" {
return Configs, errors.New("Missing DB_PASS environment variable")
}
if os.Getenv("DB_DATABASE") == "" {
return Configs, errors.New("Missing DB_DATABASE environment variable")
}
}
Configs = EnvToConfig()
CoreApp.Name = os.Getenv("NAME")
domain := os.Getenv("DOMAIN")
if domain == "" {
CoreApp.Domain = Configs.LocalIP
} else {
CoreApp.Domain = os.Getenv("DOMAIN")
}
CoreApp.DbConnection = Configs.DbConn
CoreApp.UseCdn = types.NewNullBool(os.Getenv("USE_CDN") == "true")
err := Configs.Connect(true, utils.Directory)
if err != nil {
utils.Log(4, err)
return nil, err
}
Configs.Save()
exists := DbSession.HasTable("core")
if !exists {
utils.Log(1, fmt.Sprintf("Core database does not exist, creating now!"))
Configs.DropDatabase()
Configs.CreateDatabase()
CoreApp, err = Configs.InsertCore()
if err != nil {
utils.Log(3, err)
}
admin := ReturnUser(&types.User{
Username: "admin",
Password: "admin",
Email: "info@admin.com",
Admin: types.NewNullBool(true),
})
_, err := admin.Create()
SampleData()
return Configs, err
}
return Configs, nil
}
// DefaultPort accepts a database type and returns its default port
func DefaultPort(db string) int64 {
switch db {
case "mysql":
return 3306
case "postgres":
return 5432
case "mssql":
return 1433
default:
return 0
}
}
// EnvToConfig converts environment variables to a DbConfig variable
func EnvToConfig() *DbConfig {
port := utils.ToInt(os.Getenv("DB_PORT"))
if port == 0 {
port = DefaultPort(os.Getenv("DB_PORT"))
}
name := os.Getenv("NAME")
if name == "" {
name = "Statping"
}
description := os.Getenv("DESCRIPTION")
if description == "" {
description = "Statping Monitoring Sample Data"
}
adminUser := os.Getenv("ADMIN_USER")
if adminUser == "" {
adminUser = "admin"
}
adminPass := os.Getenv("ADMIN_PASS")
if adminPass == "" {
adminPass = "admin"
}
data := &DbConfig{
DbConn: os.Getenv("DB_CONN"),
DbHost: os.Getenv("DB_HOST"),
DbUser: os.Getenv("DB_USER"),
DbPass: os.Getenv("DB_PASS"),
DbData: os.Getenv("DB_DATABASE"),
DbPort: port,
Project: name,
Description: description,
Domain: os.Getenv("DOMAIN"),
Email: "",
Username: adminUser,
Password: adminPass,
Error: nil,
Location: utils.Directory,
}
return data
}
// SampleData runs all the sample data for a new Statping installation
func SampleData() error {
if err := InsertSampleData(); err != nil {
return err
}
if err := InsertSampleHits(); err != nil {
return err
}
return nil
}
// DeleteConfig will delete the 'config.yml' file
func DeleteConfig() error {
err := os.Remove(utils.Directory + "/config.yml")
if err != nil {
utils.Log(3, err)
return err
}
return nil
}