Passing the context to sync callbacks

This commit is contained in:
Stefan Scheidewig 2022-05-24 22:44:45 +02:00
parent 9ef0b54c7c
commit 51b09c734b
4 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package cli
import (
"context"
"strconv"
"strings"
@ -19,11 +20,11 @@ func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool {
return false
}
func preCreateCallback(event handler.HookEvent) (handler.HTTPResponse, error) {
func preCreateCallback(_ context.Context, event handler.HookEvent) (handler.HTTPResponse, error) {
return invokeHookSync(hooks.HookPreCreate, event)
}
func preFinishCallback(event handler.HookEvent) (handler.HTTPResponse, error) {
func preFinishCallback(_ context.Context, event handler.HookEvent) (handler.HTTPResponse, error) {
return invokeHookSync(hooks.HookPreFinish, event)
}

View File

@ -1,6 +1,7 @@
package handler
import (
"context"
"errors"
"log"
"net/url"
@ -52,13 +53,13 @@ type Config struct {
// If the error is non-nil, the upload will not be created. This can be used to implement
// validation of upload metadata etc. Furthermore, HTTPResponse will be ignored and
// the error value can contain values for the HTTP response.
PreUploadCreateCallback func(hook HookEvent) (HTTPResponse, error)
PreUploadCreateCallback func(ctx context.Context, hook HookEvent) (HTTPResponse, error)
// PreFinishResponseCallback will be invoked after an upload is completed but before
// a response is returned to the client. This can be used to implement post-processing validation.
// If the callback returns no error, optional values from HTTPResponse will be contained in the HTTP response.
// If the error is non-nil, the error will be forwarded to the client. Furthermore,
// HTTPResponse will be ignored and the error value can contain values for the HTTP response.
PreFinishResponseCallback func(hook HookEvent) (HTTPResponse, error)
PreFinishResponseCallback func(ctx context.Context, hook HookEvent) (HTTPResponse, error)
}
func (config *Config) validate() error {

View File

@ -19,8 +19,7 @@ type httpContext struct {
func newContext(w http.ResponseWriter, r *http.Request) *httpContext {
return &httpContext{
// TODO: Try to reuse the request's context in the future
Context: context.Background(),
Context: r.Context(),
res: w,
req: r,
body: nil, // body can be filled later for PATCH requests

View File

@ -302,7 +302,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
}
if handler.config.PreUploadCreateCallback != nil {
resp2, err := handler.config.PreUploadCreateCallback(newHookEvent(info, r))
resp2, err := handler.config.PreUploadCreateCallback(c, newHookEvent(info, r))
if err != nil {
handler.sendError(c, err)
return
@ -677,7 +677,7 @@ func (handler *UnroutedHandler) finishUploadIfComplete(c *httpContext, resp HTTP
handler.Metrics.incUploadsFinished()
if handler.config.PreFinishResponseCallback != nil {
resp2, err := handler.config.PreFinishResponseCallback(newHookEvent(info, r))
resp2, err := handler.config.PreFinishResponseCallback(c, newHookEvent(info, r))
if err != nil {
return resp, err
}