Add README. Process X-Forwarded-For
All checks were successful
Go / build (push) Successful in 1m22s
All checks were successful
Go / build (push) Successful in 1m22s
This commit is contained in:
16
main.go
16
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lmittmann/tint"
|
||||
@@ -41,6 +42,16 @@ func WithLogging(h http.Handler) http.Handler {
|
||||
loggingFn := func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
remoteAddr := r.RemoteAddr
|
||||
fwdAddress := r.Header.Get("X-Forwarded-For")
|
||||
if fwdAddress != "" {
|
||||
remoteAddr = fwdAddress
|
||||
ips := strings.Split(fwdAddress, ", ")
|
||||
if len(ips) > 1 {
|
||||
remoteAddr = ips[0]
|
||||
}
|
||||
}
|
||||
|
||||
responseData := &responseData{
|
||||
status: 0,
|
||||
size: 0,
|
||||
@@ -53,11 +64,14 @@ func WithLogging(h http.Handler) http.Handler {
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
slog.Info("Request:",
|
||||
// t=2023-10-27T18:08:47.231895532+13:00 remote_addr=100.114.208.117 time_ms=4 duration=4.914291ms
|
||||
slog.Info("Request Completed:",
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.RequestURI),
|
||||
slog.String("url", r.URL.Path),
|
||||
slog.String("host", r.Host),
|
||||
slog.String("referer", r.Referer()),
|
||||
slog.String("remote_addr", remoteAddr),
|
||||
slog.Int("status", responseData.status),
|
||||
slog.Int64("duration", duration.Microseconds()),
|
||||
slog.Int("size", responseData.size),
|
||||
|
||||
Reference in New Issue
Block a user