예제 #1
0
파일: appengine.go 프로젝트: tylerb/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(service)
	app.MountAccountController(service, ac)

	// Mount bottle controller onto application
	bc := controllers.NewBottle(service)
	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
var _ = Describe("Middleware", func() {

	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))
			h := func(ctx *goa.Context) error { return ctx.Respond(200, nil) }
			ctrl := service.NewController("test")
			service.ServeMux().Handle(method, path, ctrl.HandleFunc("", h))
			service.ServeMux().Handle("OPTIONS", path, ctrl.HandleFunc("", 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)
예제 #3
0
package cors_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/raphael/goa"
	"github.com/raphael/goa-middleware/cors"
)

var _ = Describe("valid CORS DSL", func() {
	var dsl func()
	var spec cors.Specification
	var dslErrors error

	JustBeforeEach(func() {
		spec, dslErrors = cors.New(dsl)
		Ω(dslErrors).ShouldNot(HaveOccurred())
	})

	Context("with an empty DSL", func() {
		BeforeEach(func() {
			dsl = nil
		})

		It("returns an empty spec", func() {
			Ω(spec).ShouldNot(BeNil())
			Ω(spec).Should(HaveLen(0))
		})
	})

	Context("Origin", func() {