From ff05d62013906f3086b452bfeda3e0d5b9b7a541 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 9 Jan 2023 16:40:24 -0700 Subject: Initial commit. some basics: - minimal README - some TODOs - server and request handler framework - contribs: file serving, request logging - server examples - CI setup --- contrib/log/log.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 contrib/log/log.go (limited to 'contrib/log') diff --git a/contrib/log/log.go b/contrib/log/log.go new file mode 100644 index 0000000..2ccd3bc --- /dev/null +++ b/contrib/log/log.go @@ -0,0 +1,35 @@ +package log + +import ( + "context" + "io" + "time" + + kitlog "github.com/go-kit/log" + + "tildegit.org/tjp/gus/gemini" +) + +func Requests(out io.Writer, logger kitlog.Logger) gemini.Middleware { + if logger == nil { + logger = kitlog.NewLogfmtLogger(kitlog.NewSyncWriter(out)) + } + + return func(next gemini.Handler) gemini.Handler { + return func(ctx context.Context, r *gemini.Request) (resp *gemini.Response) { + start := time.Now() + defer func() { + end := time.Now() + logger.Log( + "msg", "request", + "ts", end, + "dur", end.Sub(start), + "url", r.URL, + "status", resp.Status, + ) + }() + + return next(ctx, r) + } + } +} -- cgit v1.2.3