Added config options for http request timeouts
This commit is contained in:
@@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"projectreshoot/contexts"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
@@ -22,7 +23,11 @@ func (w *wrappedWriter) WriteHeader(statusCode int) {
|
||||
// Middleware to add logs to console with details of the request
|
||||
func Logging(logger *zerolog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
start, err := contexts.GetStartTime(r.Context())
|
||||
if err != nil {
|
||||
// Handle failure here. internal server error maybe
|
||||
return
|
||||
}
|
||||
wrapped := &wrappedWriter{
|
||||
ResponseWriter: w,
|
||||
statusCode: http.StatusOK,
|
||||
|
||||
18
middleware/start.go
Normal file
18
middleware/start.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"projectreshoot/contexts"
|
||||
"time"
|
||||
)
|
||||
|
||||
func StartTimer(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
ctx := contexts.SetStart(r.Context(), start)
|
||||
newReq := r.WithContext(ctx)
|
||||
next.ServeHTTP(w, newReq)
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user