-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (84 loc) · 2.58 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
)
// UptimeResponse represents system uptime response structure
type UptimeResponse struct {
Uptime string `json:"uptime"`
}
// CPUResponse represents CPU usage response structure
type CPUResponse struct {
CPUUsage float64 `json:"cpu_usage"`
}
// LoadResponse represents system load response structure
type LoadResponse struct {
Load1 float64 `json:"load_1"`
Load5 float64 `json:"load_5"`
Load15 float64 `json:"load_15"`
}
// writeJSON is a helper function to write a JSON response and handle errors.
func writeJSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("Failed to write JSON response: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// uptimeHandler provides the system uptime in JSON format.
func uptimeHandler(w http.ResponseWriter, r *http.Request) {
hostInfo, err := host.Info()
if err != nil {
log.Printf("Failed to get host info: %v", err)
http.Error(w, "Failed to get uptime", http.StatusInternalServerError)
return
}
uptime := time.Duration(hostInfo.Uptime) * time.Second
response := UptimeResponse{Uptime: uptime.String()}
writeJSON(w, http.StatusOK, response)
}
// cpuHandler provides CPU usage in JSON format.
func cpuHandler(w http.ResponseWriter, r *http.Request) {
percentages, err := cpu.Percent(0, false)
if err != nil {
log.Printf("Failed to get CPU usage: %v", err)
http.Error(w, "Failed to get CPU usage", http.StatusInternalServerError)
return
}
response := CPUResponse{CPUUsage: percentages[0]}
writeJSON(w, http.StatusOK, response)
}
// loadHandler provides system load in JSON format.
func loadHandler(w http.ResponseWriter, r *http.Request) {
loadStat, err := load.Avg()
if err != nil {
log.Printf("Failed to get system load: %v", err)
http.Error(w, "Failed to get system load", http.StatusInternalServerError)
return
}
response := LoadResponse{
Load1: loadStat.Load1,
Load5: loadStat.Load5,
Load15: loadStat.Load15,
}
writeJSON(w, http.StatusOK, response)
}
func main() {
http.HandleFunc("/uptime", uptimeHandler)
http.HandleFunc("/cpu", cpuHandler)
http.HandleFunc("/load", loadHandler)
port := "8080"
if p := os.Getenv("PORT"); p != "" {
port = p
}
fmt.Printf("Starting server at http://localhost:%s\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}