Update main.go
All checks were successful
/ build (push) Successful in 3m24s

This commit is contained in:
2025-10-13 12:32:07 +00:00
parent b9e9cfc57d
commit 78d23bd774

32
main.go
View File

@@ -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))
}