diff options
Diffstat (limited to 'api')
| -rw-r--r-- | api/api.go | 37 |
1 files changed, 21 insertions, 16 deletions
@@ -6,13 +6,21 @@ import ( "log/slog" "net/http" "os" + "time" ) +type ServerStatus struct { + Online bool `json:"online"` + Site string `json:"url"` + Timestamp time.Time `json:"timestamp"` +} + var Logger *slog.Logger const VTT_URL string = "http://73.188.175.49:30000" -func remoteOnline(ctx context.Context, url string) bool { +func remoteOnline(ctx context.Context, url string) ServerStatus { + status := ServerStatus{Site: url, Timestamp: time.Now()} req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { panic(err) @@ -20,19 +28,16 @@ func remoteOnline(ctx context.Context, url string) bool { _, err = http.DefaultClient.Do(req) if err != nil { - return false + status.Online = false + } else { + status.Online = true } - return true + return status } -func respondOnline(w http.ResponseWriter, url string, isOnline bool) { - data := struct { - Online bool `json:"online"` - Site string `json:"url"` - }{isOnline, url} - - jsonData, err := json.Marshal(data) +func respondOnline(w http.ResponseWriter, status ServerStatus) { + jsonData, err := json.Marshal(status) if err != nil { http.Error(w, "Error constructing response", http.StatusInternalServerError) Logger.Error("Error marshalling site status data") @@ -44,12 +49,12 @@ func respondOnline(w http.ResponseWriter, url string, isOnline bool) { } func VttOnline(w http.ResponseWriter, req *http.Request) { - isOnline := remoteOnline(req.Context(), VTT_URL) + status := remoteOnline(req.Context(), VTT_URL) - if !isOnline { + if !status.Online { Logger.DebugContext(req.Context(), "Foundry VTT is offline") } - respondOnline(w, VTT_URL, isOnline) + respondOnline(w, status) } func VttRedirect(w http.ResponseWriter, req *http.Request) { @@ -59,12 +64,12 @@ func VttRedirect(w http.ResponseWriter, req *http.Request) { func SiteOnline(w http.ResponseWriter, req *http.Request) { const URL string = "https://dnd.jpappel.xyz" - isOnline := remoteOnline(req.Context(), URL) - if !isOnline { + status := remoteOnline(req.Context(), URL) + if !status.Online { Logger.DebugContext(req.Context(), "Campaign Website is offline") } - respondOnline(w, URL, isOnline) + respondOnline(w, status) } func init() { |
