From ceac344f562dfb771796c45b31cd3b85241e3324 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sun, 15 Sep 2024 14:53:54 -0400 Subject: Add verbose output flag --- nonsense-time.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'nonsense-time.go') diff --git a/nonsense-time.go b/nonsense-time.go index 14a402a..bbf4932 100644 --- a/nonsense-time.go +++ b/nonsense-time.go @@ -13,6 +13,8 @@ import ( const VTT_URL string = "http://73.188.175.49:30000" +var logger *log.Logger + func remoteOnline(ctx context.Context, url string) bool { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { @@ -47,7 +49,7 @@ func vttOnline(w http.ResponseWriter, req *http.Request) { isOnline := remoteOnline(req.Context(), VTT_URL) if !isOnline { - log.Println("Foundry VTT is offline") + logger.Println("Foundry VTT is offline") } respondOnline(w, VTT_URL, isOnline) } @@ -60,7 +62,7 @@ func siteOnline(w http.ResponseWriter, req *http.Request) { const URL string = "https://dnd.jpappel.xyz" isOnline := remoteOnline(req.Context(), URL) if !isOnline { - log.Println("Campaign Website is offline") + logger.Println("Campaign Website is offline") } respondOnline(w, URL, isOnline) } @@ -79,6 +81,7 @@ func main() { port := flag.Int("p", 8080, "the port to listen on") bindAddr := flag.String("b", "", "the adress to bind to (leave empty for all interfaces)") waitTime := flag.Duration("w", 2*time.Second, "the maximum time for a request, unit defaults to ns") + debug := flag.Bool("v", false, "verbose output") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0]) @@ -88,10 +91,17 @@ func main() { flag.Parse() - addr := fmt.Sprintf("%s:%d", *bindAddr, *port) - mux := http.NewServeMux() + addr := fmt.Sprintf("%s:%d", *bindAddr, *port) + loggerFlags := 0 + loggerPrefix := "" + if *debug { + loggerPrefix = "Nonsense Time: " + loggerFlags = log.Ldate | log.Ltime | log.Lshortfile + } + logger = log.New(os.Stdout, loggerPrefix, loggerFlags) + vtt := timeoutMiddleware(http.HandlerFunc(vttOnline), *waitTime) site := timeoutMiddleware(http.HandlerFunc(siteOnline), *waitTime) @@ -99,6 +109,6 @@ func main() { mux.HandleFunc("GET /vtt", vttRedirect) mux.Handle("GET /site/status", site) - log.Println("Listening on ", addr) - log.Fatal(http.ListenAndServe(addr, mux)) + logger.Println("Listening on ", addr) + logger.Fatal(http.ListenAndServe(addr, mux)) } -- cgit v1.2.3