2021-08-08 02:38:37 +00:00
|
|
|
package api
|
2021-08-02 21:21:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-08-31 02:08:51 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutils"
|
2021-08-02 21:21:12 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
|
|
|
)
|
|
|
|
|
2021-08-04 23:12:16 +00:00
|
|
|
func addResponseHeaders(headers map[string]string, h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
for k, v := range headers {
|
|
|
|
rw.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
h.ServeHTTP(rw, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
func annotateMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
type reqInfoKey string
|
|
|
|
|
|
|
|
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = mctx.Annotate(ctx,
|
|
|
|
reqInfoKey("remote_ip"), ip,
|
|
|
|
reqInfoKey("url"), r.URL,
|
|
|
|
reqInfoKey("method"), r.Method,
|
|
|
|
)
|
|
|
|
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
h.ServeHTTP(rw, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type logResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
2021-09-02 23:02:20 +00:00
|
|
|
http.Hijacker
|
2021-08-02 21:21:12 +00:00
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogResponseWriter(rw http.ResponseWriter) *logResponseWriter {
|
2021-09-02 23:02:20 +00:00
|
|
|
h, _ := rw.(http.Hijacker)
|
2021-08-02 21:21:12 +00:00
|
|
|
return &logResponseWriter{
|
|
|
|
ResponseWriter: rw,
|
2021-09-02 23:02:20 +00:00
|
|
|
Hijacker: h,
|
2021-08-02 21:21:12 +00:00
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *logResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
lrw.statusCode = statusCode
|
|
|
|
lrw.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logMiddleware(logger *mlog.Logger, h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-08-31 02:08:51 +00:00
|
|
|
r = apiutils.SetRequestLogger(r, logger)
|
2021-08-02 21:21:12 +00:00
|
|
|
|
|
|
|
lrw := newLogResponseWriter(rw)
|
|
|
|
|
|
|
|
started := time.Now()
|
|
|
|
h.ServeHTTP(lrw, r)
|
|
|
|
took := time.Since(started)
|
|
|
|
|
|
|
|
type logCtxKey string
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = mctx.Annotate(ctx,
|
|
|
|
logCtxKey("took"), took.String(),
|
|
|
|
logCtxKey("response_code"), lrw.statusCode,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.Info(ctx, "handled HTTP request")
|
|
|
|
})
|
|
|
|
}
|
2021-08-31 02:08:51 +00:00
|
|
|
|
2021-09-02 23:02:20 +00:00
|
|
|
func postOnlyMiddleware(h http.Handler) http.Handler {
|
2021-08-31 02:08:51 +00:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2021-09-02 23:02:20 +00:00
|
|
|
|
|
|
|
// we allow websockets to not be POSTs because, well, they can't be
|
|
|
|
if r.Method == "POST" || r.Header.Get("Upgrade") == "websocket" {
|
2021-08-31 02:08:51 +00:00
|
|
|
h.ServeHTTP(rw, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiutils.GetRequestLogger(r).WarnString(r.Context(), "method not allowed")
|
|
|
|
rw.WriteHeader(405)
|
|
|
|
})
|
|
|
|
}
|