// MountPreflightController mounts the handlers for the CORS preflight requests onto service. func MountPreflightController(service goa.Service, spec Specification) { router := service.HTTPHandler().(*httprouter.Router) for _, res := range spec { path := res.Path if res.IsPathPrefix { if strings.HasSuffix(path, "/") { path += "*cors" } else { path += "/*cors" } } var handle httprouter.Handle handle, _, tsr := router.Lookup("OPTIONS", path) if tsr { if strings.HasSuffix(path, "/") { path = path[:len(path)-1] } else { path = path + "/" } handle, _, _ = router.Lookup("OPTIONS", path) } if handle == nil { h := func(ctx *goa.Context) error { return ctx.Respond(200, nil) } router.OPTIONS(path, goa.NewHTTPRouterHandle(service, "CORS", "preflight", h)) } } }
// MountAccountController "mounts" a Account resource controller on the given service. func MountAccountController(service goa.Service, ctrl AccountController) { router := service.HTTPHandler().(*httprouter.Router) var h goa.Handler h = func(c *goa.Context) error { ctx, err := NewCreateAccountContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Create(ctx) } router.Handle("POST", "/cellar/accounts", goa.NewHTTPRouterHandle(service, "Account", "Create", h)) service.Info("mount", "ctrl", "Account", "action", "Create", "route", "POST /cellar/accounts") h = func(c *goa.Context) error { ctx, err := NewDeleteAccountContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Delete(ctx) } router.Handle("DELETE", "/cellar/accounts/:accountID", goa.NewHTTPRouterHandle(service, "Account", "Delete", h)) service.Info("mount", "ctrl", "Account", "action", "Delete", "route", "DELETE /cellar/accounts/:accountID") h = func(c *goa.Context) error { ctx, err := NewShowAccountContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Show(ctx) } router.Handle("GET", "/cellar/accounts/:accountID", goa.NewHTTPRouterHandle(service, "Account", "Show", h)) service.Info("mount", "ctrl", "Account", "action", "Show", "route", "GET /cellar/accounts/:accountID") h = func(c *goa.Context) error { ctx, err := NewUpdateAccountContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Update(ctx) } router.Handle("PUT", "/cellar/accounts/:accountID", goa.NewHTTPRouterHandle(service, "Account", "Update", h)) service.Info("mount", "ctrl", "Account", "action", "Update", "route", "PUT /cellar/accounts/:accountID") }
// MountSeriesController "mounts" a Series resource controller on the given service. func MountSeriesController(service goa.Service, ctrl SeriesController) { router := service.HTTPHandler().(*httprouter.Router) var h goa.Handler h = func(c *goa.Context) error { ctx, err := NewCreateSeriesContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Create(ctx) } router.Handle("POST", "/congo/accounts/:accountID/series", goa.NewHTTPRouterHandle(service, "Series", "Create", h)) service.Info("mount", "ctrl", "Series", "action", "Create", "route", "POST /congo/accounts/:accountID/series") h = func(c *goa.Context) error { ctx, err := NewListSeriesContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.List(ctx) } router.Handle("GET", "/congo/accounts/:accountID/series", goa.NewHTTPRouterHandle(service, "Series", "List", h)) service.Info("mount", "ctrl", "Series", "action", "List", "route", "GET /congo/accounts/:accountID/series") h = func(c *goa.Context) error { ctx, err := NewShowSeriesContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Show(ctx) } router.Handle("GET", "/congo/accounts/:accountID/series/:seriesID", goa.NewHTTPRouterHandle(service, "Series", "Show", h)) service.Info("mount", "ctrl", "Series", "action", "Show", "route", "GET /congo/accounts/:accountID/series/:seriesID") h = func(c *goa.Context) error { ctx, err := NewUpdateSeriesContext(c) if err != nil { return goa.NewBadRequestError(err) } return ctrl.Update(ctx) } router.Handle("PATCH", "/congo/accounts/:accountID/series/:seriesID", goa.NewHTTPRouterHandle(service, "Series", "Update", h)) service.Info("mount", "ctrl", "Series", "action", "Update", "route", "PATCH /congo/accounts/:accountID/series/:seriesID") }
// MountController mounts the swagger spec controller under "/swagger.json". func MountController(service goa.Service) { service.Info("mount", "ctrl", "Swagger", "action", "Show", "route", "GET /swagger.json") h := goa.NewHTTPRouterHandle(service, "Swagger", "Show", getSwagger) service.HTTPHandler().(*httprouter.Router).Handle("GET", "/swagger.json", h) }
var path string var optionsHandler goa.Handler var service *goa.GracefulApplication var url string portIndex := 1 JustBeforeEach(func() { goa.Log.SetHandler(log15.DiscardHandler()) service = goa.NewGraceful("").(*goa.GracefulApplication) spec, err := cors.New(dsl) Ω(err).ShouldNot(HaveOccurred()) service.Use(cors.Middleware(spec)) router := service.HTTPHandler().(*httprouter.Router) h := func(ctx *goa.Context) error { return ctx.Respond(200, nil) } router.Handle(method, path, goa.NewHTTPRouterHandle(service, "", "", h)) router.Handle("OPTIONS", path, goa.NewHTTPRouterHandle(service, "", "", optionsHandler)) cors.MountPreflightController(service, spec) portIndex++ port := 54511 + portIndex url = fmt.Sprintf("http://localhost:%d", port) go service.ListenAndServe(fmt.Sprintf(":%d", port)) // ugh - does anyone have a better idea? we need to wait for the server // to start listening or risk tests failing because sendind requests too // early. time.Sleep(time.Duration(100) * time.Millisecond) }) AfterEach(func() { service.Shutdown() })
}) }) }) Describe("NewHTTPRouterHandle", func() { const resName = "res" const actName = "act" var handler goa.Handler const respStatus = 200 var respContent = []byte("response") var httpHandle httprouter.Handle var ctx *goa.Context JustBeforeEach(func() { httpHandle = goa.NewHTTPRouterHandle(s, resName, actName, handler) }) BeforeEach(func() { handler = func(c *goa.Context) error { ctx = c c.Respond(respStatus, respContent) return nil } }) It("creates a handle", func() { Ω(httpHandle).ShouldNot(BeNil()) }) Context("with a request", func() {