2022-05-12 15:41:39 +01:00
|
|
|
package websvc
|
|
|
|
|
2023-04-07 12:21:37 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
2023-06-13 11:41:13 +01:00
|
|
|
"time"
|
2023-04-07 12:21:37 +01:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
2023-06-13 11:41:13 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-04-07 12:21:37 +01:00
|
|
|
)
|
2022-05-12 15:41:39 +01:00
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
|
|
|
// jsonMw sets the content type of the response to application/json.
|
|
|
|
func jsonMw(h http.Handler) (wrapped http.HandlerFunc) {
|
|
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
2023-04-07 12:21:37 +01:00
|
|
|
w.Header().Set(httphdr.ContentType, aghhttp.HdrValApplicationJSON)
|
2022-05-12 15:41:39 +01:00
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(f)
|
|
|
|
}
|
2023-06-13 11:41:13 +01:00
|
|
|
|
|
|
|
// logMw logs the queries with level debug.
|
|
|
|
func logMw(h http.Handler) (wrapped http.HandlerFunc) {
|
|
|
|
f := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
start := time.Now()
|
|
|
|
m, u := r.Method, r.RequestURI
|
|
|
|
|
|
|
|
log.Debug("websvc: %s %s started", m, u)
|
|
|
|
defer func() { log.Debug("websvc: %s %s finished in %s", m, u, time.Since(start)) }()
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(f)
|
|
|
|
}
|