feat: add supervisor prototype with embedded frontend
This commit is contained in:
27
internal/httpserver/middleware.go
Normal file
27
internal/httpserver/middleware.go
Normal file
@ -0,0 +1,27 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func LoggingMiddleware(logger *log.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
started := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
logger.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(started))
|
||||
})
|
||||
}
|
||||
|
||||
func RecoverMiddleware(logger *log.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
logger.Printf("panic: %v", rec)
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user