feat: add panic recovery middleware (#3687)

This commit is contained in:
Jon Ayers
2022-08-29 17:00:52 -05:00
committed by GitHub
parent 3cf17d34e7
commit 053fe6ff61
13 changed files with 471 additions and 40 deletions

View File

@ -1,9 +1,7 @@
package coderd
import (
"context"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/url"
@ -125,11 +123,8 @@ func New(options *Options) *API {
apiKeyMiddleware := httpmw.ExtractAPIKey(options.Database, oauthConfigs, false)
r.Use(
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(middleware.NewWrapResponseWriter(w, r.ProtoMajor), r)
})
},
httpmw.Recover(api.Logger),
httpmw.Logger(api.Logger),
httpmw.Prometheus(options.PrometheusRegistry),
)
@ -159,7 +154,6 @@ func New(options *Options) *API {
r.Use(
// Specific routes can specify smaller limits.
httpmw.RateLimitPerMinute(options.APIRateLimit),
debugLogRequest(api.Logger),
tracing.HTTPMW(api.TracerProvider, "coderd.http"),
)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
@ -438,15 +432,6 @@ func (api *API) Close() error {
return api.workspaceAgentCache.Close()
}
func debugLogRequest(log slog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
log.Debug(context.Background(), fmt.Sprintf("%s %s", r.Method, r.URL.Path))
next.ServeHTTP(rw, r)
})
}
}
func compressHandler(h http.Handler) http.Handler {
cmp := middleware.NewCompressor(5,
"text/*",