-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (92 loc) · 2.78 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/rafaelzasas/rpiSystemMonitor/cpu"
"github.com/rafaelzasas/rpiSystemMonitor/mem"
// "github.com/rafaelzasas/rpiSystemMonitor/storage"
// "github.com/rafaelzasas/rpiSystemMonitor/network"
)
func getMemUsed(w http.ResponseWriter, r *http.Request) {
res, err := mem.GetUsed()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getMemFree(w http.ResponseWriter, r *http.Request) {
res, err := mem.GetFree()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getSwpMemFree(w http.ResponseWriter, r *http.Request) {
res, err := mem.GetSwpFree()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getSwpMemUsed(w http.ResponseWriter, r *http.Request) {
res, err := mem.GetSwpUsed()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getCpuFreq(w http.ResponseWriter, r *http.Request) {
res, err := cpu.GetFreq()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getCpuTemp(w http.ResponseWriter, r *http.Request) {
res, err := cpu.GetTemp()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
func getCpuSummary(w http.ResponseWriter, r *http.Request) {
res, err := cpu.GetTemp() // TODO: cpu.GetSummary()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(res)
}
//commonMiddleWare provides middleware like content type and headers
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.Header().Add("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
func handleRequests() {
router := mux.NewRouter().StrictSlash(true)
router.Use(commonMiddleware)
// CPU Routes
router.HandleFunc("/cpu", getCpuSummary).Methods(http.MethodGet)
router.HandleFunc("/cpu/temp", getCpuTemp).Methods(http.MethodGet)
router.HandleFunc("/cpu/freq", getCpuFreq).Methods(http.MethodGet)
// Mem Routes
router.HandleFunc("/mem/free", getMemFree).Methods(http.MethodGet)
router.HandleFunc("/mem/used", getMemUsed).Methods(http.MethodGet)
router.HandleFunc("/mem/swp/free", getSwpMemFree).Methods(http.MethodGet)
router.HandleFunc("/mem/swp/used", getSwpMemUsed).Methods(http.MethodGet)
log.Fatal(http.ListenAndServe(":3000", router))
}
func main() {
handleRequests()
}