blob: a10218edb68990aec45c9159912dca3c2ecda20a (
plain)
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
29
30
31
32
|
package domain
import (
"context"
"net/http"
)
type ctxKey string
const (
ctxHostKey ctxKey = "host"
ctxDomainKey ctxKey = "domain"
)
// ReqWithHostAndDomain saves host name and domain in the request's context
func ReqWithHostAndDomain(r *http.Request, host string, domain *Domain) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, ctxHostKey, host)
ctx = context.WithValue(ctx, ctxDomainKey, domain)
return r.WithContext(ctx)
}
// GetHost extracts the host from request's context
func GetHost(r *http.Request) string {
return r.Context().Value(ctxHostKey).(string)
}
// FromRequest extracts the domain from request's context
func FromRequest(r *http.Request) *Domain {
return r.Context().Value(ctxDomainKey).(*Domain)
}
|