func main() { // Configure logger logger := log15.New() logger.SetHandler(log15.StreamHandler(os.Stderr, log15.LogfmtFormat())) // Create service service := goa.New("goa Swagger service") service.WithLogger(goalog15.New(logger)) // Setup middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "spec" controller c := NewSpecController(service) app.MountSpecController(service, c) // Mount "ae" controller h := NewAeController(service) app.MountAeController(service, h) // Start service, listen on port 8080 if err := service.ListenAndServe(":8080"); err != nil { service.LogError(err.Error()) } logger.Info("Exiting...") }
func main() { // Create service service := goa.New("cellar") // Mount middleware service.Use(middleware.RequestID()) service.Use(middleware.LogRequest(true)) service.Use(middleware.ErrorHandler(service, true)) service.Use(middleware.Recover()) // Mount "bottle" controller c := NewBottleController(service) app.MountBottleController(service, c) // Start service if err := service.ListenAndServe(":8080"); err != nil { service.LogError("startup", "err", err) } }
var service *goa.Service var h goa.Handler var verbose bool var rw *testResponseWriter BeforeEach(func() { service = nil h = nil verbose = true rw = nil }) JustBeforeEach(func() { rw = newTestResponseWriter() eh := middleware.ErrorHandler(service, verbose)(h) req, err := http.NewRequest("GET", "/foo", nil) Ω(err).ShouldNot(HaveOccurred()) ctx := newContext(service, rw, req, nil) err = eh(ctx, rw, req) Ω(err).ShouldNot(HaveOccurred()) }) Context("with a handler returning a Go error", func() { BeforeEach(func() { service = newService(nil) h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return errors.New("boom") } })
Ω(logger.InfoEntries[3].Data[3]).Should(Equal(200)) Ω(logger.InfoEntries[3].Data[4]).Should(Equal("bytes")) Ω(logger.InfoEntries[3].Data[5]).Should(Equal(5)) Ω(logger.InfoEntries[3].Data[6]).Should(Equal("time")) Ω(logger.InfoEntries[3].Data[8]).Should(Equal("ctrl")) Ω(logger.InfoEntries[3].Data[9]).Should(Equal("test")) Ω(logger.InfoEntries[3].Data[10]).Should(Equal("action")) Ω(logger.InfoEntries[3].Data[11]).Should(Equal("goo")) }) It("logs error codes", func() { h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { return goa.MissingParamError("foo") } rw.ParentHeader = make(http.Header) lg := middleware.LogRequest(false)(middleware.ErrorHandler(service, false)(h)) Ω(lg(ctx, rw, req)).ShouldNot(HaveOccurred()) Ω(logger.InfoEntries).Should(HaveLen(2)) Ω(logger.InfoEntries[0].Data).Should(HaveLen(10)) Ω(logger.InfoEntries[0].Data[0]).Should(Equal("req_id")) Ω(logger.InfoEntries[0].Data[2]).Should(Equal("POST")) Ω(logger.InfoEntries[0].Data[3]).Should(Equal("/goo?param=value")) Ω(logger.InfoEntries[1].Data).Should(HaveLen(14)) Ω(logger.InfoEntries[1].Data[0]).Should(Equal("req_id")) Ω(logger.InfoEntries[1].Data[2]).Should(Equal("status")) Ω(logger.InfoEntries[1].Data[3]).Should(Equal(400)) Ω(logger.InfoEntries[1].Data[4]).Should(Equal("error")) Ω(logger.InfoEntries[1].Data[5]).Should(HaveLen(8)) // Error ID Ω(logger.InfoEntries[1].Data[6]).Should(Equal("bytes")) Ω(logger.InfoEntries[1].Data[7]).Should(Equal(126))