-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.go
224 lines (196 loc) · 7.67 KB
/
controllers.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
// Code generated by goagen v1.3.0, DO NOT EDIT.
//
// API "pos": Application Controllers
//
// Command:
// $ goagen
// --design=github.com/psavelis/goa-pos-poc/design
// --out=$(GOPATH)src\github.com\psavelis\goa-pos-poc
// --version=v1.3.0
package app
import (
"context"
"github.com/goadesign/goa"
"github.com/goadesign/goa/cors"
"net/http"
)
// initService sets up the service encoders, decoders and mux.
func initService(service *goa.Service) {
// Setup encoders and decoders
service.Encoder.Register(goa.NewJSONEncoder, "application/json")
service.Decoder.Register(goa.NewJSONDecoder, "application/json")
// Setup default encoder and decoder
service.Encoder.Register(goa.NewJSONEncoder, "*/*")
service.Decoder.Register(goa.NewJSONDecoder, "*/*")
}
// PurchaseController is the controller interface for the Purchase actions.
type PurchaseController interface {
goa.Muxer
Create(*CreatePurchaseContext) error
Show(*ShowPurchaseContext) error
}
// MountPurchaseController "mounts" a Purchase resource controller on the given service.
func MountPurchaseController(service *goa.Service, ctrl PurchaseController) {
initService(service)
var h goa.Handler
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewCreatePurchaseContext(ctx, req, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*PurchasePayload)
} else {
return goa.MissingPayloadError()
}
return ctrl.Create(rctx)
}
service.Mux.Handle("POST", "/pos/v1/purchases/", ctrl.MuxHandler("create", h, unmarshalCreatePurchasePayload))
service.LogInfo("mount", "ctrl", "Purchase", "action", "Create", "route", "POST /pos/v1/purchases/")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewShowPurchaseContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.Show(rctx)
}
service.Mux.Handle("GET", "/pos/v1/purchases/:transaction_id", ctrl.MuxHandler("show", h, nil))
service.LogInfo("mount", "ctrl", "Purchase", "action", "Show", "route", "GET /pos/v1/purchases/:transaction_id")
}
// unmarshalCreatePurchasePayload unmarshals the request body into the context request data Payload field.
func unmarshalCreatePurchasePayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &purchasePayload{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// JsController is the controller interface for the Js actions.
type JsController interface {
goa.Muxer
goa.FileServer
}
// MountJsController "mounts" a Js resource controller on the given service.
func MountJsController(service *goa.Service, ctrl JsController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/js/*filepath", ctrl.MuxHandler("preflight", handleJsOrigin(cors.HandlePreflight()), nil))
h = ctrl.FileHandler("/js/*filepath", "public/js")
h = handleJsOrigin(h)
service.Mux.Handle("GET", "/js/*filepath", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Js", "files", "public/js", "route", "GET /js/*filepath")
h = ctrl.FileHandler("/js/", "public\\js\\index.html")
h = handleJsOrigin(h)
service.Mux.Handle("GET", "/js/", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Js", "files", "public\\js\\index.html", "route", "GET /js/")
}
// handleJsOrigin applies the CORS response headers corresponding to the origin.
func handleJsOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// PublicController is the controller interface for the Public actions.
type PublicController interface {
goa.Muxer
goa.FileServer
}
// MountPublicController "mounts" a Public resource controller on the given service.
func MountPublicController(service *goa.Service, ctrl PublicController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/ui", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))
h = ctrl.FileHandler("/ui", "public/html/index.html")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/ui", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "public/html/index.html", "route", "GET /ui")
}
// handlePublicOrigin applies the CORS response headers corresponding to the origin.
func handlePublicOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// SwaggerController is the controller interface for the Swagger actions.
type SwaggerController interface {
goa.Muxer
goa.FileServer
}
// MountSwaggerController "mounts" a Swagger resource controller on the given service.
func MountSwaggerController(service *goa.Service, ctrl SwaggerController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/swagger.json", ctrl.MuxHandler("preflight", handleSwaggerOrigin(cors.HandlePreflight()), nil))
h = ctrl.FileHandler("/swagger.json", "public/swagger/swagger.json")
h = handleSwaggerOrigin(h)
service.Mux.Handle("GET", "/swagger.json", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Swagger", "files", "public/swagger/swagger.json", "route", "GET /swagger.json")
}
// handleSwaggerOrigin applies the CORS response headers corresponding to the origin.
func handleSwaggerOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}