Initialize project with basic endpoints.

This commit is contained in:
Ben Binder
2025-02-23 20:00:29 -06:00
commit e68b88bcb7
5 changed files with 171 additions and 0 deletions

58
main.go Normal file
View File

@@ -0,0 +1,58 @@
package main
import (
"encoding/json"
"net/http"
"runtime"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
)
func main() {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"summary": map[string]interface{}{
// get the health of the cpu, memory and disk
},
})
})
http.HandleFunc("/health/cpu", func(w http.ResponseWriter, r *http.Request) {
percent, _ := cpu.Percent(0, false)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"go_os": runtime.GOOS,
"num_cpu": runtime.NumCPU(),
"cpu_usage": percent[0],
})
})
http.HandleFunc("/health/memory", func(w http.ResponseWriter, r *http.Request) {
v, _ := mem.VirtualMemory()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"total": v.Total,
"free": v.Available,
"used": v.Used,
"used_percent": v.UsedPercent,
})
})
http.HandleFunc("/health/disk", func(w http.ResponseWriter, r *http.Request) {
d, _ := disk.Usage("/")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"total": d.Total,
"used": d.Used,
"free": d.Free,
"percent": d.UsedPercent,
})
})
http.ListenAndServe(":8080", nil)
}