32 lines
741 B
Go
32 lines
741 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"gitea.com/go-chi/session"
|
|
"gitea.zokki.net/zokki/uni/web43-diary/context"
|
|
"gitea.zokki.net/zokki/uni/web43-diary/internal/database"
|
|
)
|
|
|
|
func WrapContext(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
|
|
db, err := database.NewDB()
|
|
if err != nil {
|
|
http.Error(writer, fmt.Sprintf("can't connect to database: %s", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer db.Close()
|
|
|
|
req = req.WithContext(&context.Context{
|
|
Context: req.Context(),
|
|
DB: db,
|
|
ResponseWriter: writer,
|
|
Request: req,
|
|
Session: session.GetSession(req),
|
|
})
|
|
|
|
next.ServeHTTP(writer, req)
|
|
})
|
|
}
|