forked from utmstack/UTMStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.go
More file actions
87 lines (67 loc) · 1.64 KB
/
backend.go
File metadata and controls
87 lines (67 loc) · 1.64 KB
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
package main
import (
"bytes"
"crypto/tls"
"database/sql"
"fmt"
"net/http"
"time"
)
func Backend() error {
baseURL := "https://127.0.0.1"
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transCfg}
for intent := 0; intent <= 10; intent++ {
time.Sleep(1 * time.Minute)
_, err := client.Get(baseURL + "/api/ping")
if err != nil {
if intent >= 10 {
return err
}
} else {
break
}
}
return nil
}
func RegenerateKey(internal string) error {
baseURL := "https://127.0.0.1"
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
req, err := http.NewRequest("GET", baseURL+"/api/federation-service/generateApiToken", bytes.NewBuffer([]byte{}))
if err != nil {
return err
}
req.Header.Add("Utm-Internal-Key", internal)
client := &http.Client{Transport: transCfg}
resp, err := client.Do(req)
if err != nil {
return err
}
err = resp.Body.Close()
return err
}
func SetBaseURL(password, hostname string) error {
// Connecting to utmstack
psqlconn := fmt.Sprintf("host=localhost port=5432 user=postgres password=%s sslmode=disable database=utmstack", password)
db, err := sql.Open("postgres", psqlconn)
if err != nil {
return err
}
// Close connection when finish
defer db.Close()
// Check connection status
err = db.Ping()
if err != nil {
return err
}
// Set Base URL
_, err = db.Exec(`UPDATE public.utm_configuration_parameter SET conf_param_value=$1 WHERE conf_param_short='utmstack.mail.baseUrl';`, fmt.Sprintf("https://%s.utmstack.com", hostname))
if err != nil {
return err
}
return err
}