diff --git a/htmgo-site/internal/urlhelper/ip.go b/htmgo-site/internal/urlhelper/ip.go new file mode 100644 index 0000000..2d33209 --- /dev/null +++ b/htmgo-site/internal/urlhelper/ip.go @@ -0,0 +1,27 @@ +package urlhelper + +import ( + "net/http" + "strings" +) + +func GetClientIp(r *http.Request) string { + // Try to get the real client IP from the 'CF-Connecting-IP' header + if ip := r.Header.Get("CF-Connecting-IP"); ip != "" { + return ip + } + + // If not available, fall back to 'X-Forwarded-For' + if ip := r.Header.Get("X-Forwarded-For"); ip != "" { + return ip + } + + // Otherwise, use the default remote address (this will be Cloudflare's IP) + remote := r.RemoteAddr + + if strings.HasPrefix(remote, "[::1]") { + return "localhost" + } + + return remote +} diff --git a/htmgo-site/main.go b/htmgo-site/main.go index 5c6a484..35914f3 100644 --- a/htmgo-site/main.go +++ b/htmgo-site/main.go @@ -1,14 +1,15 @@ package main import ( - "fmt" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" "htmgo-site/__htmgo" "htmgo-site/internal/cache" "htmgo-site/internal/markdown" "htmgo-site/internal/sitemap" + "htmgo-site/internal/urlhelper" "io/fs" + "log" "net/http" ) @@ -20,13 +21,17 @@ func main() { service.Set(locator, service.Singleton, markdown.NewRenderer) service.Set(locator, service.Singleton, cache.NewSimpleCache) - fmt.Printf("starting up server2\n") - h.Start(h.AppOpts{ ServiceLocator: locator, LiveReload: true, Register: func(app *h.App) { + app.Use(func(ctx *h.RequestContext) { + r := ctx.Request + // Log the details of the incoming request + log.Printf("Method: %s, URL: %s, RemoteAddr: %s", r.Method, r.URL.String(), urlhelper.GetClientIp(r)) + }) + app.UseWithContext(func(w http.ResponseWriter, r *http.Request, context map[string]any) { context["embeddedMarkdown"] = markdownAssets })