diff --git a/app.go b/app.go index 59e0924..7dacb18 100644 --- a/app.go +++ b/app.go @@ -13,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neofs-http-gw/downloader" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/nspcc-dev/neofs-http-gw/uploader" "github.com/nspcc-dev/neofs-sdk-go/pkg/logger" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool" @@ -197,6 +198,12 @@ func (a *app) Serve(ctx context.Context) { // Configure router. r := router.New() r.RedirectTrailingSlash = true + r.NotFound = func(r *fasthttp.RequestCtx) { + response.Error(r, "Not found", fasthttp.StatusNotFound) + } + r.MethodNotAllowed = func(r *fasthttp.RequestCtx) { + response.Error(r, "Method Not Allowed", fasthttp.StatusMethodNotAllowed) + } r.POST("/upload/{cid}", a.logger(uploader.Upload)) a.log.Info("added path /upload/{cid}") r.GET("/get/{cid}/{oid}", a.logger(downloader.DownloadByAddress)) diff --git a/downloader/download.go b/downloader/download.go index 1d60805..5d50fe9 100644 --- a/downloader/download.go +++ b/downloader/download.go @@ -15,6 +15,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/client" cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/valyala/fasthttp" @@ -123,7 +124,7 @@ func (r request) receiveFile(clnt client.Object, objectAddress *object.Address) ) if err = tokens.StoreBearerToken(r.RequestCtx); err != nil { r.log.Error("could not fetch and store bearer token", zap.Error(err)) - r.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) + response.Error(r.RequestCtx, "could not fetch and store bearer token", fasthttp.StatusBadRequest) return } readDetector := newDetector() @@ -177,7 +178,7 @@ func (r request) receiveFile(clnt client.Object, objectAddress *object.Address) if len(contentType) == 0 { if readDetector.err != nil { r.log.Error("could not read object", zap.Error(err)) - r.Error("could not read object", fasthttp.StatusBadRequest) + response.Error(r.RequestCtx, "could not read object", fasthttp.StatusBadRequest) return } readDetector.Wait() @@ -216,7 +217,7 @@ func (r *request) handleNeoFSErr(err error, start time.Time) { msg = errObjectNotFound.Error() } - r.Error(msg, code) + response.Error(r.RequestCtx, msg, code) } func (o objectIDs) Slice() []string { @@ -272,7 +273,7 @@ func (d *Downloader) byAddress(c *fasthttp.RequestCtx, f func(request, client.Ob ) if err := address.Parse(val); err != nil { log.Error("wrong object address", zap.Error(err)) - c.Error("wrong object address", fasthttp.StatusBadRequest) + response.Error(c, "wrong object address", fasthttp.StatusBadRequest) return } @@ -296,7 +297,7 @@ func (d *Downloader) byAttribute(c *fasthttp.RequestCtx, f func(request, client. containerID := cid.New() if err := containerID.Parse(scid); err != nil { log.Error("wrong container id", zap.Error(err)) - c.Error("wrong container id", httpStatus) + response.Error(c, "wrong container id", httpStatus) return } @@ -306,7 +307,7 @@ func (d *Downloader) byAttribute(c *fasthttp.RequestCtx, f func(request, client. if errors.Is(err, errObjectNotFound) { httpStatus = fasthttp.StatusNotFound } - c.Error("couldn't search object", httpStatus) + response.Error(c, "couldn't search object", httpStatus) return } @@ -368,13 +369,13 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) { containerID := cid.New() if err := containerID.Parse(scid); err != nil { log.Error("wrong container id", zap.Error(err)) - c.Error("wrong container id", status) + response.Error(c, "wrong container id", status) return } if err := tokens.StoreBearerToken(c); err != nil { log.Error("could not fetch and store bearer token", zap.Error(err)) - c.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) + response.Error(c, "could not fetch and store bearer token", fasthttp.StatusBadRequest) return } @@ -384,7 +385,7 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) { if errors.Is(err, errObjectNotFound) { status = fasthttp.StatusNotFound } - c.Error("couldn't find objects", status) + response.Error(c, "couldn't find objects", status) return } @@ -394,7 +395,7 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) { if err = d.streamFiles(c, containerID, ids); err != nil { log.Error("couldn't stream files", zap.Error(err)) - c.Error("couldn't stream", fasthttp.StatusInternalServerError) + response.Error(c, "couldn't stream", fasthttp.StatusInternalServerError) return } } diff --git a/downloader/head.go b/downloader/head.go index 5ecfd19..2a80129 100644 --- a/downloader/head.go +++ b/downloader/head.go @@ -7,6 +7,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/valyala/fasthttp" "go.uber.org/zap" @@ -18,7 +19,7 @@ func (r request) headObject(clnt client.Object, objectAddress *object.Address) { var start = time.Now() if err := tokens.StoreBearerToken(r.RequestCtx); err != nil { r.log.Error("could not fetch and store bearer token", zap.Error(err)) - r.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) + response.Error(r.RequestCtx, "could not fetch and store bearer token", fasthttp.StatusBadRequest) return } diff --git a/metrics.go b/metrics.go index a827738..b2f071e 100644 --- a/metrics.go +++ b/metrics.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/fasthttp/router" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/expfmt" @@ -57,7 +58,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp defer func() { <-inFlightSem }() default: - c.Error(fmt.Sprintf( + response.Error(c, fmt.Sprintf( "Limit of concurrent requests reached (%d), try again later.", opts.MaxRequestsInFlight, ), fasthttp.StatusServiceUnavailable) return @@ -76,11 +77,11 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp case promhttp.ContinueOnError: if len(mfs) == 0 { // Still report the error if no metrics have been gathered. - c.Error(err.Error(), fasthttp.StatusServiceUnavailable) + response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable) return } case promhttp.HTTPErrorOnError: - c.Error(err.Error(), fasthttp.StatusServiceUnavailable) + response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable) return } } @@ -106,7 +107,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp case promhttp.PanicOnError: panic(err) case promhttp.HTTPErrorOnError: - c.Error(err.Error(), fasthttp.StatusServiceUnavailable) + response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable) return true default: // Do nothing in all other cases, including ContinueOnError. diff --git a/pprof.go b/pprof.go index 0ef1fe5..8831314 100644 --- a/pprof.go +++ b/pprof.go @@ -5,6 +5,7 @@ import ( rtp "runtime/pprof" "github.com/fasthttp/router" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttpadaptor" ) @@ -38,6 +39,6 @@ func pprofHandler() fasthttp.RequestHandler { return } - ctx.Error("Not found", fasthttp.StatusNotFound) + response.Error(ctx, "Not found", fasthttp.StatusNotFound) } } diff --git a/response/utils.go b/response/utils.go new file mode 100644 index 0000000..8a7f383 --- /dev/null +++ b/response/utils.go @@ -0,0 +1,7 @@ +package response + +import "github.com/valyala/fasthttp" + +func Error(r *fasthttp.RequestCtx, msg string, code int) { + r.Error(msg+"\n", code) +} diff --git a/uploader/upload.go b/uploader/upload.go index f43dfb2..1833fa9 100644 --- a/uploader/upload.go +++ b/uploader/upload.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/token" + "github.com/nspcc-dev/neofs-http-gw/response" "github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/valyala/fasthttp" @@ -51,12 +52,12 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) { ) if err = tokens.StoreBearerToken(c); err != nil { log.Error("could not fetch bearer token", zap.Error(err)) - c.Error("could not fetch bearer token", fasthttp.StatusBadRequest) + response.Error(c, "could not fetch bearer token", fasthttp.StatusBadRequest) return } if err = cid.Parse(scid); err != nil { log.Error("wrong container id", zap.Error(err)) - c.Error("wrong container id", fasthttp.StatusBadRequest) + response.Error(c, "wrong container id", fasthttp.StatusBadRequest) return } defer func() { @@ -75,7 +76,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) { boundary := string(c.Request.Header.MultipartFormBoundary()) if file, err = fetchMultipartFile(u.log, bodyStream, boundary); err != nil { log.Error("could not receive multipart/form", zap.Error(err)) - c.Error("could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest) + response.Error(c, "could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest) return } filtered := filterHeaders(u.log, &c.Request.Header) @@ -112,7 +113,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) { if obj, err = u.pool.PutObject(c, ops, client.WithBearer(bt)); err != nil { log.Error("could not store file in neofs", zap.Error(err)) - c.Error("could not store file in neofs", fasthttp.StatusBadRequest) + response.Error(c, "could not store file in neofs", fasthttp.StatusBadRequest) return } @@ -122,7 +123,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) { // Try to return the response, otherwise, if something went wrong, throw an error. if err = newPutResponse(addr).encode(c); err != nil { log.Error("could not prepare response", zap.Error(err)) - c.Error("could not prepare response", fasthttp.StatusBadRequest) + response.Error(c, "could not prepare response", fasthttp.StatusBadRequest) return }