-
Notifications
You must be signed in to change notification settings - Fork 75
/
data.go
84 lines (67 loc) · 2.55 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strconv"
)
func loadData() {
// Read data from 'devices.json' file
devicesFile, fileErr := os.Open("devices.json")
if fileErr != nil {
log.Fatalf("Error loading devices.json file. \"%s\"", fileErr)
}
devicesDecoder := json.NewDecoder(devicesFile)
decodeErr := devicesDecoder.Decode(&appData)
if decodeErr != nil {
log.Fatalf("Error decoding devices.json file. \"%s\"", decodeErr)
}
log.Printf("Application data loaded from devices.json")
log.Println(" - devices defined in devices.json: ", len(appData.Devices))
devicesFile.Close()
}
func saveData(w http.ResponseWriter, r *http.Request) {
// Wrtie data to 'devices.json' file
w.Header().Set("Content-Type", "application/json")
var result HTTPResponseObject
log.Printf("New Application data received for saving to disk")
file, fileErr := os.OpenFile("devices.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
decoderErr := json.NewDecoder(r.Body).Decode(&appData)
baseErrStr := "Unable to save data to disk. "
if fileErr != nil {
result.Success = false
result.ErrorObject = fileErr
// http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf(" - Issues saving application data")
// Attempt to match raised exception against common errors
if os.IsPermission(fileErr) {
result.Message = baseErrStr + "Ensure you have appropriate permissions to write to 'device.json'."
} else if os.IsNotExist(fileErr) {
result.Message = baseErrStr + "Ensure you have a 'devices.json' file present to store data."
} else {
// Return generic error object if no matching case was found
result.Message = baseErrStr + fileErr.Error()
}
} else if decoderErr != nil {
result.Success = false
result.ErrorObject = decoderErr
result.Message = baseErrStr + "The device data received by the API was invalid."
log.Printf(" - Issues decoding application data")
} else {
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(s) defined in the list."
log.Printf(" - New application data saved to file devices.json")
}
//
json.NewEncoder(w).Encode(result)
file.Close()
}
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")
}