Move LoggingMiddleware out of examples/server.go
This commit is contained in:
parent
e7a06a12bf
commit
107b3a1785
@ -26,7 +26,7 @@ func main() {
|
||||
mux.Handle("/", gemini.FileServer(os.DirFS("/var/www")))
|
||||
|
||||
server := &gemini.Server{
|
||||
Handler: LoggingMiddleware(mux),
|
||||
Handler: gemini.LoggingMiddleware(mux),
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 1 * time.Minute,
|
||||
GetCertificate: certificates.Get,
|
||||
@ -56,47 +56,3 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func LoggingMiddleware(h gemini.Handler) gemini.Handler {
|
||||
return gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
||||
lw := &logResponseWriter{rw: w}
|
||||
h.ServeGemini(ctx, lw, r)
|
||||
host := r.TLS().ServerName
|
||||
log.Printf("gemini: %s %q %d %d", host, r.URL, lw.Status, lw.Wrote)
|
||||
})
|
||||
}
|
||||
|
||||
type logResponseWriter struct {
|
||||
Status gemini.Status
|
||||
Wrote int
|
||||
rw gemini.ResponseWriter
|
||||
mediatype string
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) SetMediaType(mediatype string) {
|
||||
w.mediatype = mediatype
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) Write(b []byte) (int, error) {
|
||||
if !w.wroteHeader {
|
||||
w.WriteHeader(gemini.StatusSuccess, w.mediatype)
|
||||
}
|
||||
n, err := w.rw.Write(b)
|
||||
w.Wrote += n
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) WriteHeader(status gemini.Status, meta string) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
w.wroteHeader = true
|
||||
w.Status = status
|
||||
w.Wrote += len(meta) + 5
|
||||
w.rw.WriteHeader(status, meta)
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
53
middleware.go
Normal file
53
middleware.go
Normal file
@ -0,0 +1,53 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
// LoggingMiddleware returns a handler that wraps h and logs Gemini requests
|
||||
// and their responses to the log package's standard logger.
|
||||
// Requests are logged with the format "gemini: {host} {URL} {status code} {bytes written}".
|
||||
func LoggingMiddleware(h Handler) Handler {
|
||||
return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
lw := &logResponseWriter{rw: w}
|
||||
h.ServeGemini(ctx, lw, r)
|
||||
host := r.ServerName()
|
||||
log.Printf("gemini: %s %q %d %d", host, r.URL, lw.Status, lw.Wrote)
|
||||
})
|
||||
}
|
||||
|
||||
type logResponseWriter struct {
|
||||
Status Status
|
||||
Wrote int
|
||||
rw ResponseWriter
|
||||
mediatype string
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) SetMediaType(mediatype string) {
|
||||
w.mediatype = mediatype
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) Write(b []byte) (int, error) {
|
||||
if !w.wroteHeader {
|
||||
w.WriteHeader(StatusSuccess, w.mediatype)
|
||||
}
|
||||
n, err := w.rw.Write(b)
|
||||
w.Wrote += n
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) WriteHeader(status Status, meta string) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
w.wroteHeader = true
|
||||
w.Status = status
|
||||
w.Wrote += len(meta) + 5
|
||||
w.rw.WriteHeader(status, meta)
|
||||
}
|
||||
|
||||
func (w *logResponseWriter) Flush() error {
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user