mirror of
https://github.com/gomods/athens
synced 2026-02-10 13:28:11 +00:00
* Remove Buffalo * gofmt * pr fixes * fix subrouter * bring back secure middleware + pr fixes * better place for subrouter * vendor
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/fatih/color"
|
|
)
|
|
|
|
type responseWriter struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
}
|
|
|
|
func (rw *responseWriter) WriteHeader(statusCode int) {
|
|
rw.statusCode = statusCode
|
|
rw.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
|
|
// RequestLogger logs request params to standard output
|
|
// it should only be used during dev.
|
|
func RequestLogger(h http.Handler) http.Handler {
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
|
rw := &responseWriter{w, 0}
|
|
h.ServeHTTP(rw, r)
|
|
fmt.Println(
|
|
fmtRequest(
|
|
r.Method,
|
|
r.URL.Path,
|
|
rw.statusCode,
|
|
),
|
|
)
|
|
}
|
|
return http.HandlerFunc(f)
|
|
}
|
|
|
|
func fmtRequest(method, path string, statusCode int) string {
|
|
if statusCode == 0 {
|
|
statusCode = 200
|
|
}
|
|
status := fmt.Sprint(statusCode)
|
|
switch {
|
|
case statusCode < http.StatusBadRequest:
|
|
status = color.GreenString("%v", status)
|
|
case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError:
|
|
status = color.HiYellowString("%v", status)
|
|
default:
|
|
status = color.HiRedString("%v", status)
|
|
}
|
|
return fmt.Sprintf(
|
|
"%v %v %v [%v]",
|
|
color.CyanString("handler:"),
|
|
method,
|
|
path,
|
|
status,
|
|
)
|
|
}
|