searchhut/database/middleware.go

23 lines
416 B
Go
Raw Normal View History

2022-07-09 13:52:55 +02:00
package database
import (
"database/sql"
"net/http"
)
var dbCtxKey = &contextKey{"database"}
type contextKey struct {
name string
}
func Middleware(db *sql.DB) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := Context(r.Context(), db)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
}