Files
reqinfo/main.go
Aykhan Shahsuvarov 78d23bd774
All checks were successful
/ build (push) Successful in 3m24s
Update main.go
2025-10-13 12:32:07 +00:00

93 lines
1.9 KiB
Go

package main
import (
"fmt"
"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)
}
if len(r.Header) > 0 {
fmt.Println("--- Headers ---")
for name, values := range r.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", name, value)
}
}
fmt.Println()
}
if len(r.URL.Query()) > 0 {
fmt.Println("--- URL Parameters ---")
for key, values := range r.URL.Query() {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
fmt.Println()
}
if len(r.Cookies()) > 0 {
fmt.Println("--- Cookies ---")
for _, cookie := range r.Cookies() {
fmt.Printf("%s: %s\n", cookie.Name, cookie.Value)
}
fmt.Println()
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println("Error reading body:", err)
} else {
if bodyStr := string(bodyBytes); bodyStr != "" {
fmt.Println("--- Body ---")
fmt.Println(bodyStr)
fmt.Println()
}
}
defer r.Body.Close()
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)
// 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))
}