Files
Horchposten/handlers/helpers.go
2026-03-08 14:44:50 +00:00

43 lines
768 B
Go

package handlers
import (
"net"
"net/http"
"strings"
"time"
)
const timeFormat = "2006-01-02T15:04:05.000Z"
func now() string {
return time.Now().UTC().Format(timeFormat)
}
func parseTime(s string) time.Time {
t, _ := time.Parse(timeFormat, s)
return t
}
// getClientIP extracts the client IP, preferring X-Forwarded-For.
func getClientIP(r *http.Request) string {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
parts := strings.Split(xff, ",")
ip := strings.TrimSpace(parts[0])
if ip != "" {
return ip
}
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
var validEndReasons = map[string]bool{
"death": true,
"quit": true,
"timeout": true,
"completed": true,
}