/
context.go
28 lines (23 loc) · 963 Bytes
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main
import (
"context"
"github.com/ejacobg/greenlight/internal/data"
"net/http"
)
// It is recommended to use a custom type for our context keys.
type contextKey string
// We will use a predefined constant rather than using the literal value every time.
const userContextKey = contextKey("user")
// contextSetUser returns a copy of the given request with the user data attached to its context.
func (app *application) contextSetUser(r *http.Request, user *data.User) *http.Request {
ctx := context.WithValue(r.Context(), userContextKey, user)
return r.WithContext(ctx)
}
// contextGetUser will extract a User value from a request's context, panicking if it doesn't exist. Only call this helper when you expect a User value to be present.
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
}