// 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, 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, 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") }
// MountBottleController "mounts" a Bottle resource controller on the given service. func MountBottleController(service *goa.Service, ctrl BottleController) { 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 := NewCreateBottleContext(ctx, service) if err != nil { return err } // Build the payload if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil { rctx.Payload = rawPayload.(*CreateBottlePayload) } else { return goa.MissingPayloadError() } return ctrl.Create(rctx) } service.Mux.Handle("POST", "/bottles", ctrl.MuxHandler("Create", h, unmarshalCreateBottlePayload)) service.LogInfo("mount", "ctrl", "Bottle", "action", "Create", "route", "POST /bottles") 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 := NewShowBottleContext(ctx, service) if err != nil { return err } return ctrl.Show(rctx) } service.Mux.Handle("GET", "/bottles/:id", ctrl.MuxHandler("Show", h, nil)) service.LogInfo("mount", "ctrl", "Bottle", "action", "Show", "route", "GET /bottles/:id") }
// 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, 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") }
var req *http.Request var muxHandler goa.MuxHandler BeforeEach(func() { body := bytes.NewBuffer([]byte{'"', '2', '3', '4', '"'}) req, _ = http.NewRequest("GET", "/foo", body) rw = &TestResponseWriter{ParentHeader: make(http.Header)} ctrl := s.NewController("test") ctrl.MaxRequestBodyLength = 4 unmarshaler := func(ctx context.Context, service *goa.Service, req *http.Request) error { _, err := ioutil.ReadAll(req.Body) return err } handler := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { rw.WriteHeader(400) rw.Write([]byte(goa.ContextError(ctx).Error())) return nil } muxHandler = ctrl.MuxHandler("testMax", handler, unmarshaler) }) JustBeforeEach(func() { muxHandler(rw, req, nil) }) It("prevents reading more bytes", func() { Ω(string(rw.Body)).Should(MatchRegexp(`\[.*\] 413 request_too_large: request body length exceeds 4 bytes`)) }) }) Describe("MuxHandler", func() {