Esempio n. 1
0
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{}")
}
Esempio n. 2
0
		It("initializes the application fields", func() {
			Ω(s.Name()).Should(Equal(appName))
			Ω(s).Should(BeAssignableToTypeOf(&goa.Application{}))
			app, _ := s.(*goa.Application)
			Ω(app.Name()).Should(Equal(appName))
			Ω(app.Logger).ShouldNot(BeNil())
			Ω(app.ServeMux).ShouldNot(BeNil())
		})
	})

	Describe("Use", func() {
		Context("with a valid middleware", func() {
			var m goa.Middleware

			BeforeEach(func() {
				m = middleware.RequestID()
			})

			JustBeforeEach(func() {
				s.Use(m)
			})

			It("adds the middleware", func() {
				ctrl := s.NewController("test")
				Ω(ctrl.MiddlewareChain()).Should(HaveLen(1))
				Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(middleware.RequestID()))
			})
		})
	})

	Describe("HandleFunc", func() {
	const reqID = "request id"
	var ctx *goa.Context

	BeforeEach(func() {
		req, err := http.NewRequest("GET", "/goo", nil)
		Ω(err).ShouldNot(HaveOccurred())
		req.Header.Set("X-Request-Id", reqID)
		ctx = goa.NewContext(nil, goa.New("test"), req, new(testResponseWriter), nil)
	})

	It("sets the request ID in the context", func() {
		h := func(ctx *goa.Context) error {
			ctx.Respond(200, "ok")
			return nil
		}
		rg := middleware.RequestID()(h)
		Ω(rg(ctx)).ShouldNot(HaveOccurred())
		Ω(ctx.Value(middleware.ReqIDKey)).Should(Equal(reqID))
	})
})

var _ = Describe("Recover", func() {
	It("recovers", func() {
		h := func(ctx *goa.Context) error {
			panic("boom")
		}
		rg := middleware.Recover()(h)
		err := rg(goa.NewContext(nil, goa.New("test"), nil, nil, nil))
		Ω(err).Should(HaveOccurred())
		Ω(err.Error()).Should(Equal("panic: boom"))
	})