56 lines
1 KiB
Go
56 lines
1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
checks "http-server-status/internal/pkg/checks"
|
||
|
"net/http"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
var checkList []checks.Check
|
||
|
|
||
|
func init() {
|
||
|
checkList = append(checkList, checks.HDD{}, checks.Memory{}, checks.Load{})
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
|
||
|
http.HandleFunc("/", handler)
|
||
|
err := http.ListenAndServe(":3003", nil)
|
||
|
log.Fatal().Err(err).Msg("Shutdown")
|
||
|
}
|
||
|
|
||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||
|
res, gloableRes := checkSystem()
|
||
|
resJson, err := json.Marshal(res)
|
||
|
if err != nil {
|
||
|
log.Fatal().Err(err).Msg("Check Error")
|
||
|
}
|
||
|
if gloableRes {
|
||
|
w.Write(resJson)
|
||
|
return
|
||
|
}
|
||
|
http.Error(w, string(resJson), http.StatusInternalServerError)
|
||
|
|
||
|
}
|
||
|
|
||
|
func checkSystem() (map[string]bool, bool) {
|
||
|
globaleResult := true
|
||
|
wg := sync.WaitGroup{}
|
||
|
res := make(map[string]bool)
|
||
|
for _, c := range checkList {
|
||
|
wg.Add(1)
|
||
|
go func(check checks.Check) {
|
||
|
defer wg.Done()
|
||
|
s, _ := check.Execute()
|
||
|
if s == false {
|
||
|
globaleResult = false
|
||
|
}
|
||
|
res[check.Name()] = s
|
||
|
}(c)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
|
||
|
return res, globaleResult
|
||
|
}
|