package health

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"runtime"
)

type HealthInfo struct {
	Hostname string
	OS       string
	Arch     string
	CPU      string
	Memory   string
}

func Handle(w http.ResponseWriter, r *http.Request) {
	json.NewEncoder(w).Encode(healthCheck())
}

func healthCheck() HealthInfo {
	hostname, _ := os.Hostname()

	var mem runtime.MemStats
	runtime.ReadMemStats(&mem)

	return HealthInfo{Hostname: hostname,
		OS:     runtime.GOOS,
		Arch:   runtime.GOARCH,
		CPU:    string(runtime.NumCPU()),
		Memory: string(mem.Alloc/1024/1024) + " MB"}
}

func (h HealthInfo) String() string {
	return fmt.Sprintf("HealthInfo{Hostname=%s, OS=%s, Arch=%s. CPU=%s, Memory=%s}", h.Hostname, h.OS, h.Arch, h.CPU, h.Memory)
}
