forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
167 lines (143 loc) · 4.49 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package goa
import (
"net/http"
"net/url"
"strconv"
"golang.org/x/net/context"
)
// Keys used to store data in context.
const (
reqKey key = iota + 1
respKey
ctrlKey
actionKey
paramsKey
logKey
logContextKey
errKey
securityScopesKey
)
type (
// RequestData provides access to the underlying HTTP request.
RequestData struct {
*http.Request
// Payload returns the decoded request body.
Payload interface{}
// Params contains the raw values for the parameters defined in the design including
// path parameters, query string parameters and header parameters.
Params url.Values
}
// ResponseData provides access to the underlying HTTP response.
ResponseData struct {
http.ResponseWriter
// The service used to encode the response.
Service *Service
// ErrorCode is the code of the error returned by the action if any.
ErrorCode string
// Status is the response HTTP status code.
Status int
// Length is the response body length.
Length int
}
// key is the type used to store internal values in the context.
// Context provides typed accessor methods to these values.
key int
)
// NewContext builds a new goa request context.
// If ctx is nil then context.Background() is used.
func NewContext(ctx context.Context, rw http.ResponseWriter, req *http.Request, params url.Values) context.Context {
if ctx == nil {
ctx = context.Background()
}
request := &RequestData{Request: req, Params: params}
response := &ResponseData{ResponseWriter: rw}
ctx = context.WithValue(ctx, respKey, response)
ctx = context.WithValue(ctx, reqKey, request)
return ctx
}
// WithAction creates a context with the given action name.
func WithAction(ctx context.Context, action string) context.Context {
return context.WithValue(ctx, actionKey, action)
}
// WithLogger sets the request context logger and returns the resulting new context.
func WithLogger(ctx context.Context, logger LogAdapter) context.Context {
return context.WithValue(ctx, logKey, logger)
}
// WithLogContext instantiates a new logger by appending the given key/value pairs to the context
// logger and setting the resulting logger in the context.
func WithLogContext(ctx context.Context, keyvals ...interface{}) context.Context {
logger := ContextLogger(ctx)
if logger == nil {
return ctx
}
nl := logger.New(keyvals...)
return WithLogger(ctx, nl)
}
// WithError creates a context with the given error.
func WithError(ctx context.Context, err error) context.Context {
return context.WithValue(ctx, errKey, err)
}
// ContextController extracts the controller name from the given context.
func ContextController(ctx context.Context) string {
if c := ctx.Value(ctrlKey); c != nil {
return c.(string)
}
return "<unknown>"
}
// ContextAction extracts the action name from the given context.
func ContextAction(ctx context.Context) string {
if a := ctx.Value(actionKey); a != nil {
return a.(string)
}
return "<unknown>"
}
// ContextRequest extracts the request data from the given context.
func ContextRequest(ctx context.Context) *RequestData {
if r := ctx.Value(reqKey); r != nil {
return r.(*RequestData)
}
return nil
}
// ContextResponse extracts the response data from the given context.
func ContextResponse(ctx context.Context) *ResponseData {
if r := ctx.Value(respKey); r != nil {
return r.(*ResponseData)
}
return nil
}
// ContextLogger extracts the logger from the given context.
func ContextLogger(ctx context.Context) LogAdapter {
if v := ctx.Value(logKey); v != nil {
return v.(LogAdapter)
}
return nil
}
// ContextError extracts the error from the given context.
func ContextError(ctx context.Context) error {
if err := ctx.Value(errKey); err != nil {
return err.(error)
}
return nil
}
// SwitchWriter overrides the underlying response writer. It returns the response
// writer that was previously set.
func (r *ResponseData) SwitchWriter(rw http.ResponseWriter) http.ResponseWriter {
rwo := r.ResponseWriter
r.ResponseWriter = rw
return rwo
}
// Written returns true if the response was written, false otherwise.
func (r *ResponseData) Written() bool {
return r.Status != 0
}
// WriteHeader records the response status code and calls the underlying writer.
func (r *ResponseData) WriteHeader(status int) {
go IncrCounter([]string{"goa", "response", strconv.Itoa(status)}, 1.0)
r.Status = status
r.ResponseWriter.WriteHeader(status)
}
// Write records the amount of data written and calls the underlying writer.
func (r *ResponseData) Write(b []byte) (int, error) {
r.Length += len(b)
return r.ResponseWriter.Write(b)
}