forked from sameerdhoot/wolweb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.go
62 lines (49 loc) · 1.7 KB
/
data.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strconv"
)
func loadData() {
devicesFile, err := os.Open("/wolweb/data/devices.json")
if err != nil {
log.Fatalf("Error loading devices.json file. \"%s\"", err)
}
devicesDecoder := json.NewDecoder(devicesFile)
err = devicesDecoder.Decode(&appData)
if err != nil {
log.Fatalf("Error decoding devices.json file. \"%s\"", err)
}
log.Printf("Application data loaded from devices.json")
log.Println(" - devices defined in devices.json: ", len(appData.Devices))
}
func saveData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var result HTTPResponseObject
log.Printf("New Application data received for saving to disk")
err := json.NewDecoder(r.Body).Decode(&appData)
if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
result.Success = false
result.Message = "Colud not save the data. " + err.Error()
result.ErrorObject = err
log.Printf(" - Issues decoding/saving application data")
} else {
file, _ := os.OpenFile("/wolweb/data/devices.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
encoder.Encode(appData)
result.Success = true
result.Message = "devices data saved to devices.json file. There are now " + strconv.Itoa(len(appData.Devices)) + " device defined in the list."
log.Printf(" - New application data saved to file devices.json")
}
json.NewEncoder(w).Encode(result)
}
func getData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(appData)
log.Printf("Request for Application data served")
}