/
system.go
155 lines (130 loc) · 3.64 KB
/
system.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
package api
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"time"
"github.com/globalsign/mgo/bson"
routing "github.com/julienschmidt/httprouter"
)
var Version string
var Branch string
var Buildtime int64
var BuildNr string
// SysClearAll implements PUT /sys/clear_all
func SysClearAll(resp http.ResponseWriter, req *http.Request, params routing.Params) {
}
var startTime time.Time
func init() {
startTime = time.Now()
}
// SysGetUptime implements GET /sys/uptime
func SysGetUptime(resp http.ResponseWriter, req *http.Request, params routing.Params) {
uptime := time.Since(startTime)
resp.Write([]byte(uptime.String()))
}
// SysGetLogs implements GET /sys/logs
func SysGetLogs(resp http.ResponseWriter, req *http.Request, params routing.Params) {
files, err := ioutil.ReadDir("./log")
if err != nil {
http.Error(resp, "Error: "+err.Error(), http.StatusInternalServerError)
}
resp.Write([]byte{'['})
first := true
var logFile struct {
Name string `json:"name"`
Time time.Time `json:"time"`
Size int64 `json:"size"`
}
encoder := json.NewEncoder(resp)
for _, file := range files {
name := file.Name()
if logNameRegexp.MatchString(name) { // bson id (24) + ".txt" (4)
if first == false {
resp.Write([]byte{','})
}
first = false
id := bson.ObjectIdHex(name[0:24])
logFile.Name = name
logFile.Size = file.Size()
logFile.Time = id.Time()
encoder.Encode(&logFile)
}
}
resp.Write([]byte{']'})
}
var logNameRegexp = regexp.MustCompile(`^[0-9a-f]{24}\.txt$`)
// SysGetLog implements GET /sys/log/{log_name}
func SysGetLog(resp http.ResponseWriter, req *http.Request, params routing.Params) {
name := params.ByName("log_id")
if !logNameRegexp.MatchString(name) {
http.Error(resp, "Error: Bad log name.", http.StatusBadRequest)
return
}
http.ServeFile(resp, req, "./log/"+name)
}
// SysDeleteLog implements DELETE /sys/log/{log_name}
func SysDeleteLog(resp http.ResponseWriter, req *http.Request, params routing.Params) {
name := params.ByName("log_id")
if !logNameRegexp.MatchString(name) {
http.Error(resp, "Error: Bad log name.", http.StatusBadRequest)
return
}
err := os.Remove("./log/" + name)
if err != nil {
if os.IsNotExist(err) {
http.Error(resp, "Error: Log file not found.", http.StatusNotFound)
return
}
http.Error(resp, "Error: "+err.Error(), http.StatusInternalServerError)
return
}
}
func SysGetVersion(resp http.ResponseWriter, req *http.Request, params routing.Params) {
resp.Write([]byte(Version))
}
func SysGetBuildNr(resp http.ResponseWriter, req *http.Request, params routing.Params) {
resp.Write([]byte(BuildNr))
}
type Info struct {
Version string `json:"version"`
Branch string `json:"branch"`
Buildtime int64 `json:"buildtime"`
}
func SysGetInfo(resp http.ResponseWriter, req *http.Request, params routing.Params) {
info := Info{
Version: Version,
Branch: Branch,
Buildtime: Buildtime,
}
data, _ := json.Marshal(&info)
resp.Write(data)
}
func SysSetNewTimezone(resp http.ResponseWriter, req *http.Request, params routing.Params) {
body, err := io.ReadAll(req.Body)
if err != nil {
log.Printf("[ERR ] Error could not set timezone in golang: %s", err)
serveError(resp, err)
return
}
newTimezone := string(body)
location, err := time.LoadLocation(newTimezone)
if err != nil {
log.Printf("[ERR ] Error could not set timezone in golang: %s", err)
serveError(resp, err)
return
} else {
time.Local = location
err = exec.Command("timedatectl", "set-timezone", newTimezone).Run()
if err != nil {
log.Printf("[ERR ] Error could not set hosts timezone: %s", err)
serveError(resp, err)
return
}
}
}