forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
148 lines (124 loc) · 3.15 KB
/
setup.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
package handlers
import (
"errors"
"github.com/statping/statping/notifiers"
"github.com/statping/statping/types/configs"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
"net/http"
"net/url"
"strconv"
)
func processSetupHandler(w http.ResponseWriter, r *http.Request) {
var err error
if core.App.Setup {
sendErrorJson(errors.New("Statping has already been setup"), w, r)
return
}
confgs, err := configs.LoadConfigForm(r)
if err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
project := r.PostForm.Get("project")
description := r.PostForm.Get("description")
domain := r.PostForm.Get("domain")
sendNews, _ := strconv.ParseBool(r.PostForm.Get("newsletter"))
sendReports, _ := strconv.ParseBool(r.PostForm.Get("send_reports"))
log.WithFields(utils.ToFields(core.App, confgs)).Debugln("new configs posted")
if err = configs.ConnectConfigs(confgs, false); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
if err := confgs.Save(utils.Directory); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
exists := confgs.Db.HasTable("core")
if !exists {
if err := confgs.DropDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
if err := confgs.CreateDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
if err := configs.CreateAdminUser(); err != nil {
sendErrorJson(err, w, r)
return
}
if err := configs.TriggerSamples(); err != nil {
sendErrorJson(err, w, r)
return
}
}
if err = confgs.MigrateDatabase(); err != nil {
sendErrorJson(err, w, r)
return
}
log.Infoln("Migrating Notifiers...")
notifiers.InitNotifiers()
c := &core.Core{
Name: project,
Description: description,
ApiSecret: utils.Params.GetString("API_SECRET"),
Domain: domain,
Version: core.App.Version,
Started: utils.Now(),
CreatedAt: utils.Now(),
UseCdn: null.NewNullBool(false),
Footer: null.NewNullString(""),
Language: confgs.Language,
AllowReports: null.NewNullBool(sendReports),
}
log.Infoln("Creating new Core")
if err := c.Create(); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
core.App = c
if sendNews {
log.Infof("Sending email address %s to newsletter server", confgs.Email)
if err := registerNews(confgs.Email, confgs.Domain); err != nil {
log.Errorln(err)
}
}
log.Infoln("Initializing new Statping instance")
if _, err := services.SelectAllServices(true); err != nil {
log.Errorln(err)
sendErrorJson(err, w, r)
return
}
services.CheckServices()
core.App.Setup = true
resetCookies()
out := struct {
Message string `json:"message"`
Config *configs.DbConfig `json:"config"`
}{
"success",
confgs,
}
returnJson(out, w, r)
}
func registerNews(email, domain string) error {
if email == "" {
return nil
}
v := url.Values{}
v.Set("email", email)
v.Set("domain", domain)
v.Set("timezone", "UTC")
resp, err := http.PostForm("https://news.statping.com/new", v)
if err != nil {
return err
}
return resp.Body.Close()
}