-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.go
129 lines (115 loc) · 4.15 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
// API "goa Swagger service": Application Controllers
//
// Code generated by goagen v1.1.0, DO NOT EDIT.
//
// Command:
// $ goagen
// --design=github.com/goadesign/swagger-service/design
// --out=$(GOPATH)/src/github.com/goadesign/swagger-service
// --version=v1.1.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.Encoder.Register(goa.NewGobEncoder, "application/gob", "application/x-gob")
service.Encoder.Register(goa.NewXMLEncoder, "application/xml")
service.Decoder.Register(goa.NewJSONDecoder, "application/json")
service.Decoder.Register(goa.NewGobDecoder, "application/gob", "application/x-gob")
service.Decoder.Register(goa.NewXMLDecoder, "application/xml")
// Setup default encoder and decoder
service.Encoder.Register(goa.NewJSONEncoder, "*/*")
service.Decoder.Register(goa.NewJSONDecoder, "*/*")
}
// AeController is the controller interface for the Ae actions.
type AeController interface {
goa.Muxer
Health(*HealthAeContext) error
Start(*StartAeContext) error
}
// MountAeController "mounts" a Ae resource controller on the given service.
func MountAeController(service *goa.Service, ctrl AeController) {
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 := NewHealthAeContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.Health(rctx)
}
service.Mux.Handle("GET", "/_ah/health", ctrl.MuxHandler("Health", h, nil))
service.LogInfo("mount", "ctrl", "Ae", "action", "Health", "route", "GET /_ah/health")
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 := NewStartAeContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.Start(rctx)
}
service.Mux.Handle("GET", "/_ah/start", ctrl.MuxHandler("Start", h, nil))
service.LogInfo("mount", "ctrl", "Ae", "action", "Start", "route", "GET /_ah/start")
}
// SpecController is the controller interface for the Spec actions.
type SpecController interface {
goa.Muxer
Show(*ShowSpecContext) error
}
// MountSpecController "mounts" a Spec resource controller on the given service.
func MountSpecController(service *goa.Service, ctrl SpecController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/swagger/spec", ctrl.MuxHandler("preflight", handleSpecOrigin(cors.HandlePreflight()), nil))
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 := NewShowSpecContext(ctx, req, service)
if err != nil {
return err
}
return ctrl.Show(rctx)
}
h = handleSpecOrigin(h)
service.Mux.Handle("GET", "/swagger/spec", ctrl.MuxHandler("Show", h, nil))
service.LogInfo("mount", "ctrl", "Spec", "action", "Show", "route", "GET /swagger/spec")
}
// handleSpecOrigin applies the CORS response headers corresponding to the origin.
func handleSpecOrigin(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")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}