This commit is contained in:
lbenedar
2026-03-20 18:07:41 +03:00
parent efb58d7c15
commit 891b63f259
5 changed files with 88 additions and 1 deletions

26
cmd/api/context.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"context"
"net/http"
"gitea.local.lab/Lbenedar/greenlight/internal/data"
)
type contextKey string
const userContextKey = contextKey("user")
func (app *application) contextSetUser(r *http.Request, user *data.User) *http.Request {
ctx := context.WithValue(r.Context(), userContextKey, user)
return r.WithContext(ctx)
}
func (app *application) contextGetUser(r *http.Request) *data.User {
user, ok := r.Context().Value(userContextKey).(*data.User)
if !ok {
panic("missing user value in request context")
}
return user
}