add access logger option

master
bel 2023-08-17 17:08:10 -06:00
parent 1a4be5d384
commit 981c850edf
2 changed files with 25 additions and 1 deletions

7
go.mod
View File

@ -1,9 +1,14 @@
module gogs.inhome.blapointe.com/local/simpleserve module gogs.inhome.blapointe.com/local/simpleserve
go 1.16 go 1.18
require ( require (
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34 gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc
gogs.inhome.blapointe.com/local/notes-server v0.0.0-20230410171406-a4d39d38dc55 gogs.inhome.blapointe.com/local/notes-server v0.0.0-20230410171406-a4d39d38dc55
) )
require (
github.com/gomarkdown/markdown v0.0.0-20220607163217-45f7c050e2d1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

19
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -13,6 +14,7 @@ import (
"path" "path"
"regexp" "regexp"
"strings" "strings"
"time"
"gogs.inhome.blapointe.com/local/args" "gogs.inhome.blapointe.com/local/args"
"gogs.inhome.blapointe.com/local/gziphttp" "gogs.inhome.blapointe.com/local/gziphttp"
@ -110,6 +112,23 @@ func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass
func withAccessLogging(enabled bool, h http.HandlerFunc) http.HandlerFunc { func withAccessLogging(enabled bool, h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Host == "" {
r.URL.Host = r.Host
}
b, _ := json.Marshal(map[string]any{
"_ts": time.Now(),
"client": map[string]any{
"ip": r.RemoteAddr,
"forwarded-for": r.Header.Get("X-Forwarded-For"),
"user-agent": r.UserAgent(),
},
"request": map[string]any{
"method": r.Method,
"url": r.URL.String(),
"headers": r.Header,
},
})
fmt.Printf("%s\n", b)
h(w, r) h(w, r)
} }
} }