feat(log) advanced logging and env variable for port

main 1.1.0
Valentin 2024-08-05 15:20:35 +02:00
parent 3d2cb71808
commit a45aaf14c3
1 changed files with 10 additions and 3 deletions

View File

@ -3,16 +3,23 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"os"
) )
func main() { func main() {
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
targetURL := "https://" + r.Host + r.RequestURI targetURL := "https://" + r.Host + r.RequestURI
log.Printf("[REDIRECT] %s - %s (%s)", targetURL, r.RemoteAddr, r.UserAgent())
http.Redirect(w, r, targetURL, http.StatusMovedPermanently) http.Redirect(w, r, targetURL, http.StatusMovedPermanently)
}) })
log.Println("Service running on :80") log.Printf("[INFO] Service running on :%s", port)
if err := http.ListenAndServe(":80", nil); err != nil { if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalf("%v", err) log.Fatalf("[ERROR] %v", err)
} }
} }