コード例 #1
0
ファイル: appengine.go プロジェクト: harboe/goa
func init() {
	// Configure logging for appengine
	goa.Log.SetHandler(log15.MultiHandler(
		log15.StreamHandler(os.Stderr, log15.LogfmtFormat()),
		AppEngineLogHandler()),
	)

	// Create goa application
	service := goa.New("cellar")

	// Setup CORS to allow for swagger UI.
	spec, err := cors.New(func() {
		cors.Origin("*", func() {
			cors.Resource("*", func() {
				cors.Methods("GET", "POST", "PUT", "PATCH", "DELETE")
				cors.Headers("*")
			})
		})
	})
	if err != nil {
		panic(err)
	}

	// Setup basic middleware
	service.Use(goa.RequestID())
	service.Use(AppEngineLogCtx())
	service.Use(cors.Middleware(spec))
	service.Use(goa.Recover())

	// Mount account controller onto application
	ac := controllers.NewAccount()
	app.MountAccountController(service, ac)

	// Mount bottle controller onto application
	bc := controllers.NewBottle()
	app.MountBottleController(service, bc)

	// Mount Swagger Spec controller onto application
	swagger.MountController(service)

	// Mount CORS preflight controllers
	cors.MountPreflightController(service, spec)

	// Setup HTTP handler
	http.HandleFunc("/", service.HTTPHandler().ServeHTTP)
}
コード例 #2
0
ファイル: middleware_test.go プロジェクト: patrickToca/goa
	Context("with a running goa app", func() {
		var dsl func()
		var method string
		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)
		})