This commit is contained in:
kekskurse 2025-02-01 12:07:29 +01:00
commit 5a0f51fd49
3 changed files with 57 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
vendor/
go.sum

14
go.mod Normal file
View file

@ -0,0 +1,14 @@
module git.keks.cloud/kekskurse/utils
go 1.23.1
require (
github.com/google/uuid v1.6.0
github.com/rs/zerolog v1.33.0
)
require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
)

41
web/middelware/logging.go Normal file
View file

@ -0,0 +1,41 @@
package middelware
import (
"net/http"
"os"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type Logging struct {
log zerolog.Logger
}
func NewCLILogger() Logging {
log := log.Logger.With().Str("pkg", "middelware").Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
l := Logging{
log: log,
}
return l
}
func (l Logging) Middelware(someThing string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// Logic here
uid := uuid.NewString()
start := time.Now()
// Call the next handler
next.ServeHTTP(w, r)
elapsed := time.Since(start)
log.Info().Str("method", r.Method).Str("URI", r.URL.String()).Str("uuid", uid).Int64("request time ms", elapsed.Milliseconds()).Msg("Get Request")
}
return http.HandlerFunc(fn)
}
}