/
misc.go
63 lines (56 loc) · 1.81 KB
/
misc.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
// Miscellaneous function handlers.
package httpapi
import (
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
)
// Flush and close all data files and shutdown the entire program.
func Shutdown(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
HttpDB.Close()
os.Exit(0)
}
// Copy this database into destination directory.
func Dump(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
var dest string
if !Require(w, r, "dest", &dest) {
return
}
if err := HttpDB.Dump(dest); err != nil {
http.Error(w, fmt.Sprint(err), 500)
return
}
}
// Return server memory statistics.
func MemStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
stats := new(runtime.MemStats)
runtime.ReadMemStats(stats)
resp, err := json.Marshal(stats)
if err != nil {
http.Error(w, "Cannot serialize MemStats to JSON.", 500)
return
}
w.Write(resp)
}
// Return server protocol version number.
func Version(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")
w.Write([]byte("6"))
}