diff --git a/main.go b/main.go index b3f4a97..4ed82e3 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,23 @@ import ( "io" "log" "net/http" + "os" // Import the 'os' package "strings" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Println(strings.Repeat("~", 80)) + // Determine scheme (http or https) + scheme := "http" + if r.TLS != nil { + scheme = "https" + } + + // Construct and print the full URL + fullURL := fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI) + fmt.Printf("Full URL: %s\n\n", fullURL) + if r.Host != "" { fmt.Printf("Host: %s\n\n", r.Host) } @@ -27,7 +38,6 @@ func handler(w http.ResponseWriter, r *http.Request) { if len(r.URL.Query()) > 0 { fmt.Println("--- URL Parameters ---") - r.ParseForm() for key, values := range r.URL.Query() { for _, value := range values { fmt.Printf("%s: %s\n", key, value) @@ -58,10 +68,26 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Println("--- Client IP ---") fmt.Println(r.RemoteAddr) + + // Send a simple response back to the client + fmt.Fprintln(w, "Request details logged to the server console.") } func main() { + // Get port from environment variable, with a fallback to 8080 + port := os.Getenv("PORT") + if port == "" { + port = "8080" // Default port if not specified + } + + // The address the server will listen on + listenAddr := ":" + port + http.HandleFunc("/", handler) - fmt.Println("Server started at http://localhost:8080") - log.Fatal(http.ListenAndServe(":8080", nil)) -} + + // Update the startup message to be dynamic + fmt.Printf("Server started at http://localhost:%s\n", port) + + // Use the dynamically set address + log.Fatal(http.ListenAndServe(listenAddr, nil)) +} \ No newline at end of file