From 2580bfa4d3a3467c6a088bf112d760e908dd9f52 Mon Sep 17 00:00:00 2001 From: Anubhav Saini Date: Wed, 17 Jan 2024 11:18:23 +0530 Subject: [PATCH] Fixes #72. Update URL, introduce error when ListenAndServe fail. 1. The URL where the server is running is updated so that it can be clicked from the terminal, in case the terminal supports it. 2. The error is thrown if the port cannot be bound by the tutorial server. --- main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index a8a7099..975a318 100644 --- a/main.go +++ b/main.go @@ -382,14 +382,19 @@ func handle(root string) func(w http.ResponseWriter, req *http.Request) { } var ( - host = flag.String("host", "localhost:8000", "Serving host") + protocol = "http" + address = "localhost:8000" + host = flag.String("host", address, "Serving host") ) func main() { flag.Parse() - fmt.Println("Serving Go+ tutorial at", *host) + fmt.Printf("Serving Go+ tutorial at %s://%s\n", protocol, *host) http.HandleFunc("/", handle(".")) - http.ListenAndServe(*host, nil) + err := http.ListenAndServe(*host, nil) + if err != nil { + log.Fatalf("Failed to start server at %s://%s.\nError: %s.", protocol, *host, err) + } } // -----------------------------------------------------------------------------