/
contexts.go
238 lines (199 loc) · 8.08 KB
/
contexts.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package handlers
import (
"context"
"net/http"
"github.com/go-openapi/runtime/middleware"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/validate"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/auth"
"github.com/transcom/mymove/pkg/db/sequence"
"github.com/transcom/mymove/pkg/dpsauth"
"github.com/transcom/mymove/pkg/iws"
"github.com/transcom/mymove/pkg/logging"
"github.com/transcom/mymove/pkg/logging/hnyzap"
"github.com/transcom/mymove/pkg/notifications"
"github.com/transcom/mymove/pkg/route"
"github.com/transcom/mymove/pkg/services"
"github.com/transcom/mymove/pkg/storage"
)
// HandlerContext provides access to all the contextual references needed by individual handlers
type HandlerContext interface {
DB() *pop.Connection
SessionAndLoggerFromContext(ctx context.Context) (*auth.Session, Logger)
SessionAndLoggerFromRequest(r *http.Request) (*auth.Session, Logger)
SessionFromRequest(r *http.Request) *auth.Session
SessionFromContext(ctx context.Context) *auth.Session
LoggerFromContext(ctx context.Context) Logger
LoggerFromRequest(r *http.Request) Logger
HoneyZapLogger() *hnyzap.Logger
FileStorer() storage.FileStorer
SetFileStorer(storer storage.FileStorer)
NotificationSender() notifications.NotificationSender
SetNotificationSender(sender notifications.NotificationSender)
Planner() route.Planner
SetPlanner(planner route.Planner)
CookieSecret() string
SetCookieSecret(secret string)
NoSessionTimeout() bool
SetNoSessionTimeout()
IWSPersonLookup() iws.PersonLookup
SetIWSPersonLookup(rbs iws.PersonLookup)
SendProductionInvoice() bool
SetSendProductionInvoice(sendProductionInvoice bool)
UseSecureCookie() bool
SetUseSecureCookie(useSecureCookie bool)
GexSender() services.GexSender
SetGexSender(gexSender services.GexSender)
ICNSequencer() sequence.Sequencer
SetICNSequencer(sequencer sequence.Sequencer)
DPSAuthParams() dpsauth.Params
SetDPSAuthParams(params dpsauth.Params)
RespondAndTraceError(ctx context.Context, err error, msg string, fields ...zap.Field) middleware.Responder
RespondAndTraceVErrors(ctx context.Context, verrs *validate.Errors, err error, msg string, fields ...zap.Field) middleware.Responder
}
// A single handlerContext is passed to each handler
type handlerContext struct {
db *pop.Connection
logger Logger
cookieSecret string
noSessionTimeout bool
planner route.Planner
storage storage.FileStorer
notificationSender notifications.NotificationSender
iwsPersonLookup iws.PersonLookup
sendProductionInvoice bool
dpsAuthParams dpsauth.Params
senderToGex services.GexSender
icnSequencer sequence.Sequencer
useSecureCookie bool
}
// NewHandlerContext returns a new handlerContext with its required private fields set.
func NewHandlerContext(db *pop.Connection, logger Logger) HandlerContext {
return &handlerContext{
db: db,
logger: logger,
}
}
func (hctx *handlerContext) SessionAndLoggerFromRequest(r *http.Request) (*auth.Session, Logger) {
return hctx.SessionAndLoggerFromContext(r.Context())
}
func (hctx *handlerContext) SessionAndLoggerFromContext(ctx context.Context) (*auth.Session, Logger) {
return auth.SessionFromContext(ctx), hctx.LoggerFromContext(ctx)
}
func (hctx *handlerContext) SessionFromRequest(r *http.Request) *auth.Session {
return auth.SessionFromContext(r.Context())
}
func (hctx *handlerContext) SessionFromContext(ctx context.Context) *auth.Session {
return auth.SessionFromContext(ctx)
}
func (hctx *handlerContext) LoggerFromRequest(r *http.Request) Logger {
return hctx.LoggerFromContext(r.Context())
}
func (hctx *handlerContext) LoggerFromContext(ctx context.Context) Logger {
if logger, ok := logging.FromContext(ctx).(Logger); ok {
return logger
}
return hctx.logger
}
// DB returns a POP db connection for the context
func (hctx *handlerContext) DB() *pop.Connection {
return hctx.db
}
// HoneyZapLogger returns the logger capable of writing to Honeycomb to use in this context
func (hctx *handlerContext) HoneyZapLogger() *hnyzap.Logger {
if zapLogger, ok := hctx.logger.(*zap.Logger); ok {
return &hnyzap.Logger{Logger: zapLogger}
}
return nil
}
// RespondAndTraceError uses Honeycomb to trace errors and then passes response to the standard ResponseForError
func (hctx *handlerContext) RespondAndTraceError(ctx context.Context, err error, msg string, fields ...zap.Field) middleware.Responder {
hctx.HoneyZapLogger().TraceError(ctx, msg, fields...)
return ResponseForError(hctx.LoggerFromContext(ctx), err)
}
// RespondAndTraceVErrors uses Honeycomb to trace errors and then passes response to the standard ResponseForVErrors
func (hctx *handlerContext) RespondAndTraceVErrors(ctx context.Context, verrs *validate.Errors, err error, msg string, fields ...zap.Field) middleware.Responder {
hctx.HoneyZapLogger().TraceError(ctx, msg, fields...)
return ResponseForVErrors(hctx.LoggerFromContext(ctx), verrs, err)
}
// FileStorer returns the storage to use in the current context
func (hctx *handlerContext) FileStorer() storage.FileStorer {
return hctx.storage
}
// SetFileStorer is a simple setter for storage private field
func (hctx *handlerContext) SetFileStorer(storer storage.FileStorer) {
hctx.storage = storer
}
// NotificationSender returns the sender to use in the current context
func (hctx *handlerContext) NotificationSender() notifications.NotificationSender {
return hctx.notificationSender
}
// SetNotificationSender is a simple setter for AWS SES private field
func (hctx *handlerContext) SetNotificationSender(sender notifications.NotificationSender) {
hctx.notificationSender = sender
}
// Planner is a simple setter for the route.Planner private field
func (hctx *handlerContext) Planner() route.Planner {
return hctx.planner
}
// SetPlanner is a simple setter for the route.Planner private field
func (hctx *handlerContext) SetPlanner(planner route.Planner) {
hctx.planner = planner
}
// CookieSecret returns the secret key to use when signing cookies
func (hctx *handlerContext) CookieSecret() string {
return hctx.cookieSecret
}
// SetCookieSecret is a simple setter for the cookieSeecret private Field
func (hctx *handlerContext) SetCookieSecret(cookieSecret string) {
hctx.cookieSecret = cookieSecret
}
// NoSessionTimeout is a flag which, when true, indicates that sessions should not timeout. Used in dev.
func (hctx *handlerContext) NoSessionTimeout() bool {
return hctx.noSessionTimeout
}
// SetNoSessionTimeout is a simple setter for the noSessionTimeout private Field
func (hctx *handlerContext) SetNoSessionTimeout() {
hctx.noSessionTimeout = true
}
func (hctx *handlerContext) IWSPersonLookup() iws.PersonLookup {
return hctx.iwsPersonLookup
}
func (hctx *handlerContext) SetIWSPersonLookup(rbs iws.PersonLookup) {
hctx.iwsPersonLookup = rbs
}
// SendProductionInvoice is a flag to notify EDI invoice generation whether it should be sent as a test or production transaction
func (hctx *handlerContext) SendProductionInvoice() bool {
return hctx.sendProductionInvoice
}
// Set UsageIndicator flag for use in EDI invoicing (ediinvoice pkg)
func (hctx *handlerContext) SetSendProductionInvoice(sendProductionInvoice bool) {
hctx.sendProductionInvoice = sendProductionInvoice
}
func (hctx *handlerContext) GexSender() services.GexSender {
return hctx.senderToGex
}
func (hctx *handlerContext) SetGexSender(sendGexRequest services.GexSender) {
hctx.senderToGex = sendGexRequest
}
func (hctx *handlerContext) ICNSequencer() sequence.Sequencer {
return hctx.icnSequencer
}
func (hctx *handlerContext) SetICNSequencer(sequencer sequence.Sequencer) {
hctx.icnSequencer = sequencer
}
func (hctx *handlerContext) DPSAuthParams() dpsauth.Params {
return hctx.dpsAuthParams
}
func (hctx *handlerContext) SetDPSAuthParams(params dpsauth.Params) {
hctx.dpsAuthParams = params
}
// UseSecureCookie determines if the field "Secure" is set to true or false upon cookie creation
func (hctx *handlerContext) UseSecureCookie() bool {
return hctx.useSecureCookie
}
// Sets flag for using Secure cookie
func (hctx *handlerContext) SetUseSecureCookie(useSecureCookie bool) {
hctx.useSecureCookie = useSecureCookie
}