func main() { // Create service service := goa.New("API") logger := log15.New() goa.Log = goalog15.New(logger) // Setup middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.Recover()) cspec, err := cors.New(func() { cors.Origin("*", func() { cors.Resource("/*", func() { cors.Headers("Accept", "Content-Type", "Origin", "Authorization") cors.Methods("GET", "POST", "PUT", "DELETE", "OPTIONS") cors.MaxAge(600) cors.Credentials(true) cors.Vary("Http-Origin") }) }) }) if err != nil { panic(err) } // mount the cors controller service.Use(cors.Middleware(cspec)) cors.MountPreflightController(service, cspec) // Mount "authentication" controller c := NewAuthenticationController(service) app.MountAuthenticationController(service, c) // Mount "operands" controller c2 := NewOperandsController(service) app.MountOperandsController(service, c2) // Mount "ui" controller ui.MountController(service) // Mount Swagger spec provider controller swagger.MountController(service) // Start service, listen on port 8080 service.ListenAndServe(":8080") fmt.Println("a ...interface{}") }
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("", false).(*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, nil)) service.ServeMux().Handle("OPTIONS", path, ctrl.HandleFunc("", optionsHandler, nil)) 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)
package cors_test import ( "github.com/goadesign/goa" "github.com/goadesign/middleware/cors" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) 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() {