Beispiel #1
0
func TestGinLogger(t *testing.T) {
	Convey("Given a buffered logrus", t, func() {
		log := logrus.New()
		buf := &bytes.Buffer{}
		log.Out = buf

		Convey("And a configured HTTP server", func() {
			gin.SetMode(gin.ReleaseMode)
			router := gin.New()

			router.Use(utils.GinLogger("API", log))

			router.GET("/", func(c *gin.Context) {
				c.JSON(200, "hello")
			})

			ts := httptest.NewServer(router)

			Convey("A message should be logged to the buffer upon a request", func() {
				resp, err := http.Get(ts.URL + "/")
				So(err, ShouldBeNil)

				body, err := ioutil.ReadAll(resp.Body)
				So(err, ShouldBeNil)
				So(string(body), ShouldEqual, "\"hello\"\n")

				So(buf.String(), ShouldContainSubstring, "API request finished")
				So(buf.String(), ShouldContainSubstring, "method=GET path=\"/\" status=200")
			})
		})
	})
}
Beispiel #2
0
func TestGinRecovery(t *testing.T) {
	Convey("Given a configured HTTP server", t, func() {
		gin.SetMode(gin.ReleaseMode)
		router := gin.New()

		var rc *raven.Client
		router.Use(utils.GinRecovery(rc))

		router.GET("/test1", func(c *gin.Context) {
			panic("hello1")
		})
		router.GET("/test2", func(c *gin.Context) {
			panic(errors.New("hello2"))
		})
		router.GET("/test3", func(c *gin.Context) {
			c.JSON(200, "hello3")
		})

		ts := httptest.NewServer(router)

		Convey("String-valued panic should report to Sentry", func() {
			resp, err := http.Get(ts.URL + "/test1")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 500)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "{\"code\":500,\"error\":\"Internal server error\"}\n")
		})

		Convey("Error-valued panic should report to Sentry", func() {
			resp, err := http.Get(ts.URL + "/test2")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 500)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "{\"code\":500,\"error\":\"Internal server error\"}\n")
		})

		Convey("No panic should not report anything", func() {
			resp, err := http.Get(ts.URL + "/test3")
			So(err, ShouldBeNil)

			So(resp.StatusCode, ShouldEqual, 200)

			body, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)
			So(string(body), ShouldEqual, "\"hello3\"\n")

		})
	})
}
Beispiel #3
0
func (a *API) Main() {
	// Create a new router
	router := gin.New()

	// Add two global middlewares
	router.Use(utils.GinCORS())
	router.Use(utils.GinLogger("API", a.Log))
	router.Use(utils.GinRecovery(a.Raven))

	// Hello route
	router.GET("/", a.hello)

	v1 := router.Group("/v1")
	{
		// Public routes
		v1.POST("/accounts", a.createAccount) // Registration and reservation
		v1.POST("/oauth", a.oauthToken)       // Various OAuth handlers
		v1.GET("/keys/:id", a.readKey)        // Open keyserver

		// Create a subrouter
		v1a := v1.Group("/", a.authMiddleware)
		{
			// Accounts
			//v1a.GET("/accounts", a.listAccounts)
			v1a.GET("/accounts/:id", a.readAccount)
			v1a.PUT("/accounts/:id", a.updateAccount)
			//v1a.DELETE("/accounts/:id", a.deleteAccount)
			v1a.GET("/accounts/:id/addresses", a.getAccountAddresses)
			//v1a.GET("/accounts/:id/emails")
			v1a.GET("/accounts/:id/keys", a.getAccountKeys)
			v1a.GET("/accounts/:id/labels", a.getAccountLabels)
			v1a.GET("/accounts/:id/resources", a.getAccountResources)
			//v1a.GET("/accounts/:id/threads")
			v1a.GET("/accounts/:id/tokens", a.getAccountTokens)

			// Addresses
			//v1a.POST("/addresses", a.createAddress)
			//v1a.GET("/addresses", a.listAddresses)
			//v1a.GET("/addresses/:id", a.readAddress)
			//v1a.PUT("/addresses/:id", a.updateAddress)
			//v1a.DELETE("/addresses/:id", a.deleteAddress)

			// Emails
			//v1a.POST("/emails", a.createEmail)
			//v1a.GET("/emails", a.listEmails)
			//v1a.GET("/emails/:id", a.getEmail)
			//v1a.DELETE("/emails/:id", a.deleteEmail)

			// Keys
			v1a.POST("/keys", a.createKey)
			//v1a.GET("/keys", a.listKeys)
			//v1a.PUT("/keys/:id", a.updateKeys)
			//v1a.DELETE("/keys/:id", a.deleteKey)

			// Labels
			//v1a.POST("/labels", a.createLabel)
			//v1a.GET("/labels", a.listLabels)
			//v1a.GET("/labels/:id", a.readLabel)
			//v1a.PUT("/labels/:id", a.updateLabel)
			//v1a.DELETE("/labels/:id", a.deleteLabel)
			v1a.GET("/labels/:id/threads", a.getLabelThreads)

			// Resources
			v1a.POST("/resources", a.createResource)
			//v1a.GET("/resources", a.listResources)
			v1a.GET("/resources/:id", a.readResource)
			//v1a.PUT("/resources/:id", a.updateResource)
			//v1a.DELETE("/resources/:id", a.deleteResource)

			// Threads
			//v1a.GET("/threads", a.listThreads)
			//v1a.GET("/threads/:id", a.readThread)
			//v1a.PUT("/threads/:id", a.updateThread)
			//v1a.DELETE("/threads/:id", a.deleteThread)

			// Tokens
			v1a.POST("/tokens", a.createToken)
			//v1a.GET("/tokens", a.listTokens)
			//v1a.GET("/tokens/:id", a.readToken)
			//v1a.PUT("/tokens/:id", a.updateToken)
			//v1a.DELETE("/tokens/:id", a.deleteToken)
		}
	}

	// Log that we're about to start the server
	a.Log.WithFields(logrus.Fields{
		"address": a.Options.HTTPAddress,
	}).Info("Starting the server")

	// Start the server
	if err := router.Run(a.Options.HTTPAddress); err != nil {
		a.Log.WithFields(logrus.Fields{
			"err":     err,
			"address": a.Options.HTTPAddress,
		}).Fatal("Unable to start a HTTP server")
	}
}