forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_helpers_current.go
81 lines (71 loc) · 2.52 KB
/
middleware_helpers_current.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package service
import (
"context"
"net/http"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/pkg/errors"
)
func setAPIHostContext(r *http.Request, h *host.Host) *http.Request {
return r.WithContext(context.WithValue(r.Context(), model.ApiHostKey, h))
}
func setAPITaskContext(r *http.Request, t *task.Task) *http.Request {
return r.WithContext(context.WithValue(r.Context(), model.ApiTaskKey, t))
}
func setProjectReftContext(r *http.Request, p *model.ProjectRef) *http.Request {
return r.WithContext(context.WithValue(r.Context(), model.ApiProjectRefKey, p))
}
func setProjectContext(r *http.Request, p *model.Project) *http.Request {
return r.WithContext(context.WithValue(r.Context(), model.ApiProjectKey, p))
}
func setUIRequestContext(r *http.Request, p projectContext) *http.Request {
return r.WithContext(context.WithValue(r.Context(), RequestProjectContext, p))
}
func setRestContext(r *http.Request, c *model.Context) *http.Request {
return r.WithContext(context.WithValue(r.Context(), RestContext, c))
}
// GetTask loads the task attached to a request.
func GetTask(r *http.Request) *task.Task {
if rv := r.Context().Value(model.ApiTaskKey); rv != nil {
if t, ok := rv.(*task.Task); ok {
return t
}
}
return nil
}
// GetHost loads the host attached to a request
func GetHost(r *http.Request) *host.Host {
if rv := r.Context().Value(model.ApiHostKey); rv != nil {
return rv.(*host.Host)
}
return nil
}
// GetProject loads the project attached to a request into request
// context.
func GetProject(r *http.Request) (*model.ProjectRef, *model.Project) {
pref := r.Context().Value(model.ApiProjectRefKey)
if pref == nil {
return nil, nil
}
p := r.Context().Value(model.ApiProjectKey)
if p == nil {
return nil, nil
}
return pref.(*model.ProjectRef), p.(*model.Project)
}
// GetProjectContext fetches the projectContext associated with the request. Returns an error
// if no projectContext has been loaded and attached to the request.
func GetProjectContext(r *http.Request) (projectContext, error) {
if rv := r.Context().Value(RequestProjectContext); rv != nil {
return rv.(projectContext), nil
}
return projectContext{}, errors.New("No context loaded")
}
// GetRESTContext fetches the context associated with the request.
func GetRESTContext(r *http.Request) (*model.Context, error) {
if rv := r.Context().Value(RestContext); rv != nil {
return rv.(*model.Context), nil
}
return nil, errors.New("No context loaded")
}