Show status from the checker thingy

This commit is contained in:
dragongoose 2023-09-30 12:19:41 -04:00
parent 6845a4e573
commit 21f7080ea2
No known key found for this signature in database
GPG key ID: 01397EEC371CDAA5
3 changed files with 12 additions and 10 deletions

View file

@ -2,15 +2,15 @@ package proto
import "net/http"
func Request(url string) bool {
func Request(url string) (bool, string) {
req, err := http.Get(url)
if err != nil {
// `req` could be `nil` so we need this check
return false
return false, ""
}
if req.StatusCode != 200 {
return false
return false, req.Status
}
return true
return true, req.Status
}

View file

@ -27,18 +27,20 @@ func TestServices() []ServiceStatus {
for _, service := range services {
var serviceResult bool
// Default N/A
var serviceStatus string = "N/A"
switch service.Type {
case "http":
{
serviceResult = Request(service.Url.String())
serviceResult, serviceStatus = Request(service.Url.String())
break
}
case "tcp":
{
// go includes port in host
serviceResult = CheckTCPOpen(service.Url.Host)
serviceResult, serviceStatus = CheckTCPOpen(service.Url.Host)
break
}
}
@ -46,7 +48,7 @@ func TestServices() []ServiceStatus {
result := ServiceStatus{
Name: service.Name,
Ok: serviceResult,
Status: "",
Status: serviceStatus,
}
results = append(results, result)

View file

@ -5,17 +5,17 @@ import (
"time"
)
func CheckTCPOpen(host string) bool {
func CheckTCPOpen(host string) (bool, string) {
// Just check if the connection is successful
timeout := time.Second
conn, err := net.DialTimeout("tcp", host, timeout)
if err != nil || conn == nil {
// If timeout or anything else
return false
return false, err.Error()
}
defer conn.Close()
return true
return true, "OK"
}