Refactoring code to improve HTTP status code (#1005)

* refactoring code to improve HTTP status code

* refactoring code to improve HTTP status code

* refactoring code to improve HTTP status code

* refactoring code to improve HTTP status code
This commit is contained in:
Nafis Faysal
2018-12-21 03:15:03 +06:00
committed by marpio
parent 4c2c3a24b0
commit dd801d86b1
12 changed files with 25 additions and 18 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ func basicAuth(user, pass string) buffalo.MiddlewareFunc {
return func(c buffalo.Context) error {
if !checkAuth(c.Request(), user, pass) {
c.Response().Header().Set("WWW-Authenticate", `Basic realm="basic auth required"`)
c.Render(401, nil)
c.Render(http.StatusUnauthorized, nil)
return nil
}
+2 -1
View File
@@ -2,8 +2,9 @@ package actions
import (
"github.com/gobuffalo/buffalo"
"net/http"
)
func healthHandler(c buffalo.Context) error {
return c.Render(200, nil)
return c.Render(http.StatusOK, nil)
}
+2 -1
View File
@@ -2,8 +2,9 @@ package actions
import (
"github.com/gobuffalo/buffalo"
"net/http"
)
func proxyHomeHandler(c buffalo.Context) error {
return c.Render(200, proxy.JSON("Welcome to The Athens Proxy"))
return c.Render(http.StatusOK, proxy.JSON("Welcome to The Athens Proxy"))
}
+3 -2
View File
@@ -3,14 +3,15 @@ package actions
import (
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/storage"
"net/http"
)
func getReadinessHandler(s storage.Backend) buffalo.Handler {
return func(c buffalo.Context) error {
if _, err := s.List(c, "github.com/gomods/athens"); err != nil {
return c.Render(500, nil)
return c.Render(http.StatusInternalServerError, nil)
}
return c.Render(200, nil)
return c.Render(http.StatusOK, nil)
}
}
+2 -1
View File
@@ -3,8 +3,9 @@ package actions
import (
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/build"
"net/http"
)
func versionHandler(c buffalo.Context) error {
return c.Render(200, proxy.JSON(build.Data()))
return c.Render(http.StatusOK, proxy.JSON(build.Data()))
}
+1 -1
View File
@@ -20,7 +20,7 @@ func LatestHandler(dp Protocol, lggr log.Entry, eng *render.Engine) buffalo.Hand
mod, err := paths.GetModule(c)
if err != nil {
lggr.SystemErr(errors.E(op, err))
return c.Render(500, nil)
return c.Render(http.StatusInternalServerError, nil)
}
info, err := dp.Latest(c, mod)
+1 -1
View File
@@ -21,7 +21,7 @@ func ListHandler(dp Protocol, lggr log.Entry, eng *render.Engine) buffalo.Handle
mod, err := paths.GetModule(c)
if err != nil {
lggr.SystemErr(errors.E(op, err))
return c.Render(500, nil)
return c.Render(http.StatusInternalServerError, nil)
}
versions, err := dp.List(c, mod)
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/gobuffalo/buffalo/render"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/log"
"net/http"
)
// PathVersionModule URL.
@@ -28,6 +29,6 @@ func VersionModuleHandler(dp Protocol, lggr log.Entry, eng *render.Engine) buffa
// Calling c.Response().Write will write the header directly
// and we would get a 0 status in the buffalo logs.
return c.Render(200, eng.String(string(modBts)))
return c.Render(http.StatusOK, eng.String(string(modBts)))
}
}
+2 -1
View File
@@ -2,6 +2,7 @@ package download
import (
"io"
"net/http"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
@@ -31,7 +32,7 @@ func VersionZipHandler(dp Protocol, lggr log.Entry, eng *render.Engine) buffalo.
// Calling c.Response().Write will write the header directly
// and we would get a 0 status in the buffalo logs.
c.Render(200, nil)
c.Render(http.StatusOK, nil)
_, err = io.Copy(c.Response(), zip)
if err != nil {
lggr.SystemErr(errors.E(op, errors.M(mod), errors.V(ver), err))
+3 -2
View File
@@ -2,6 +2,7 @@ package log
import (
"fmt"
"net/http"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
@@ -19,9 +20,9 @@ func (buffaloFormatter) Format(entry *logrus.Entry) ([]byte, error) {
statusCode, _ := entry.Data["status"].(int)
status := fmt.Sprint(statusCode)
switch {
case statusCode < 400:
case statusCode < http.StatusBadRequest:
status = color.GreenString("%v", status)
case statusCode >= 400 && statusCode < 500:
case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError:
status = color.HiYellowString("%v", status)
default:
status = color.HiRedString("%v", status)
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
func TestCacheControl(t *testing.T) {
h := func(c buffalo.Context) error { return c.Render(200, nil) }
h := func(c buffalo.Context) error { return c.Render(http.StatusOK, nil) }
a := buffalo.New(buffalo.Options{})
a.GET("/test", h)
+5 -5
View File
@@ -32,7 +32,7 @@ func testConfigFile(t *testing.T) (testConfigFile string) {
func middlewareFilterApp(filterFile, registryEndpoint string) (*buffalo.App, error) {
h := func(c buffalo.Context) error {
return c.Render(200, nil)
return c.Render(http.StatusOK, nil)
}
a := buffalo.New(buffalo.Options{})
@@ -85,22 +85,22 @@ func Test_FilterMiddleware(t *testing.T) {
paths := []string{"/github.com/gomods/athens/@v/list/", "/github.com/gomods/athens/@v/list"}
for _, path := range paths {
res := w.JSON(path).Get()
r.Equal(303, res.Code)
r.Equal(http.StatusSeeOther, res.Code)
r.Equal(conf.GlobalEndpoint+"/github.com/gomods/athens/@v/list/", res.HeaderMap.Get("Location"))
}
// Excluded, expects a 403
res := w.JSON("/github.com/athens-artifacts/no-tags/@v/list").Get()
r.Equal(403, res.Code)
r.Equal(http.StatusForbidden, res.Code)
// Private, the proxy is working and returns a 200
res = w.JSON("/github.com/athens-artifacts/happy-path/@v/list").Get()
r.Equal(200, res.Code)
r.Equal(http.StatusOK, res.Code)
}
func hookFilterApp(hook string) *buffalo.App {
h := func(c buffalo.Context) error {
return c.Render(200, nil)
return c.Render(http.StatusOK, nil)
}
a := buffalo.New(buffalo.Options{})