diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2024-09-12 23:22:16 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2024-09-12 23:22:16 -0400 |
| commit | 00ce2db61bd3ac89450af13a76439515e0ccc577 (patch) | |
| tree | 4dd4dd2ca0deefa888358d2d59fa0f11f9afb7fd /nonsense-time.go | |
| parent | 6fca74fa180673b003efbdedd8fd292225707def (diff) | |
Renamed variables and functions
Diffstat (limited to 'nonsense-time.go')
| -rw-r--r-- | nonsense-time.go | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/nonsense-time.go b/nonsense-time.go index e5a6803..6d4d438 100644 --- a/nonsense-time.go +++ b/nonsense-time.go @@ -13,8 +13,7 @@ import ( const VTT_URL string = "http://73.188.175.49:30000" -// If a url is publically reachable during a context -func isOnline(ctx context.Context, url string) bool { +func remoteOnline(ctx context.Context, url string) bool { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { panic(err) @@ -29,13 +28,13 @@ func isOnline(ctx context.Context, url string) bool { return true } -func remoteStatus(w http.ResponseWriter, url string, status bool) { - resp_body := struct { +func respondOnline(w http.ResponseWriter, url string, isOnline bool) { + data := struct { Online bool `json:"online"` Site string `json:"url"` - }{status, url} + }{isOnline, url} - jsonData, err := json.Marshal(resp_body) + jsonData, err := json.Marshal(data) if err != nil { http.Error(w, "Error constructing response", http.StatusInternalServerError) return @@ -45,40 +44,37 @@ func remoteStatus(w http.ResponseWriter, url string, status bool) { w.Write(jsonData) } -func logStatus(name string, status bool) { +func logOnline(name string, online bool) { statusText := "offline" - if status { + if online { statusText = "online" } log.Println(name, "is", statusText) } -// Check if the vtt is online -func vttStatus(w http.ResponseWriter, req *http.Request) { +func vttOnline(w http.ResponseWriter, req *http.Request) { ctx, cancel := context.WithTimeout(req.Context(), 3*time.Second) defer cancel() - status := isOnline(ctx, VTT_URL) - logStatus("Foundry VTT", status) - remoteStatus(w, VTT_URL, status) + isOnline := remoteOnline(ctx, VTT_URL) + logOnline("Foundry VTT", isOnline) + respondOnline(w, VTT_URL, isOnline) } -// Redirect to vtt func vttRedirect(w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, VTT_URL, http.StatusMovedPermanently) } -// Check if the campaign website is online -func siteStatus(w http.ResponseWriter, req *http.Request) { +func siteOnline(w http.ResponseWriter, req *http.Request) { ctx, cancel := context.WithTimeout(req.Context(), 3*time.Second) defer cancel() const URL string = "https://dnd.jpappel.xyz" - status := isOnline(ctx, URL) - logStatus("Campaign Website", status) - remoteStatus(w, URL, status) + isOnline := remoteOnline(ctx, URL) + logOnline("Campaign Website", isOnline) + respondOnline(w, URL, isOnline) } func main() { @@ -98,9 +94,9 @@ func main() { mux := http.NewServeMux() - mux.HandleFunc("GET /vtt/status", vttStatus) + mux.HandleFunc("GET /vtt/status", vttOnline) mux.HandleFunc("GET /vtt", vttRedirect) - mux.HandleFunc("GET /site/status", siteStatus) + mux.HandleFunc("GET /site/status", siteOnline) log.Println("Listening on ", addr) log.Fatal(http.ListenAndServe(addr, mux)) |
