This commit is contained in:
34
main.go
34
main.go
@@ -5,12 +5,23 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os" // Import the 'os' package
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println(strings.Repeat("~", 80))
|
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 != "" {
|
if r.Host != "" {
|
||||||
fmt.Printf("Host: %s\n\n", 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 {
|
if len(r.URL.Query()) > 0 {
|
||||||
fmt.Println("--- URL Parameters ---")
|
fmt.Println("--- URL Parameters ---")
|
||||||
r.ParseForm()
|
|
||||||
for key, values := range r.URL.Query() {
|
for key, values := range r.URL.Query() {
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
fmt.Printf("%s: %s\n", key, value)
|
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("--- Client IP ---")
|
||||||
fmt.Println(r.RemoteAddr)
|
fmt.Println(r.RemoteAddr)
|
||||||
|
|
||||||
|
// Send a simple response back to the client
|
||||||
|
fmt.Fprintln(w, "Request details logged to the server console.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", handler)
|
// Get port from environment variable, with a fallback to 8080
|
||||||
fmt.Println("Server started at http://localhost:8080")
|
port := os.Getenv("PORT")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
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))
|
||||||
}
|
}
|
Reference in New Issue
Block a user