mirror of
https://github.com/gomods/athens
synced 2026-02-03 12:10:32 +00:00
pkg/middleware: add tracing to ValidationHook (#1627)
This commit is contained in:
@@ -118,7 +118,7 @@ func App(conf *config.Config) (http.Handler, error) {
|
|||||||
|
|
||||||
// Having the hook set means we want to use it
|
// Having the hook set means we want to use it
|
||||||
if vHook := conf.ValidatorHook; vHook != "" {
|
if vHook := conf.ValidatorHook; vHook != "" {
|
||||||
r.Use(mw.NewValidationMiddleware(vHook))
|
r.Use(mw.NewValidationMiddleware(client, vHook))
|
||||||
}
|
}
|
||||||
|
|
||||||
store, err := GetStorage(conf.StorageType, conf.Storage, conf.TimeoutDuration(), client)
|
store, err := GetStorage(conf.StorageType, conf.Storage, conf.TimeoutDuration(), client)
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func Test_FilterMiddleware(t *testing.T) {
|
|||||||
func hookFilterApp(hook string) *mux.Router {
|
func hookFilterApp(hook string) *mux.Router {
|
||||||
h := func(w http.ResponseWriter, r *http.Request) {}
|
h := func(w http.ResponseWriter, r *http.Request) {}
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.Use(NewValidationMiddleware(hook))
|
r.Use(NewValidationMiddleware(http.DefaultClient, hook))
|
||||||
|
|
||||||
r.HandleFunc(pathList, h)
|
r.HandleFunc(pathList, h)
|
||||||
r.HandleFunc(pathVersionInfo, h)
|
r.HandleFunc(pathVersionInfo, h)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
// NewValidationMiddleware builds a middleware function that performs validation checks by calling
|
// NewValidationMiddleware builds a middleware function that performs validation checks by calling
|
||||||
// an external webhook
|
// an external webhook
|
||||||
func NewValidationMiddleware(validatorHook string) mux.MiddlewareFunc {
|
func NewValidationMiddleware(client *http.Client, validatorHook string) mux.MiddlewareFunc {
|
||||||
const op errors.Op = "actions.NewValidationMiddleware"
|
const op errors.Op = "actions.NewValidationMiddleware"
|
||||||
return func(h http.Handler) http.Handler {
|
return func(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -25,13 +25,14 @@ func NewValidationMiddleware(validatorHook string) mux.MiddlewareFunc {
|
|||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
ctx := r.Context()
|
||||||
// not checking the error. Not all requests include a version
|
// not checking the error. Not all requests include a version
|
||||||
// i.e. list requests path is like /{module:.+}/@v/list with no version parameter
|
// i.e. list requests path is like /{module:.+}/@v/list with no version parameter
|
||||||
version, _ := paths.GetVersion(r)
|
version, _ := paths.GetVersion(r)
|
||||||
if version != "" {
|
if version != "" {
|
||||||
response, err := validate(validatorHook, mod, version)
|
response, err := validate(ctx, client, validatorHook, mod, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
entry := log.EntryFromContext(r.Context())
|
entry := log.EntryFromContext(ctx)
|
||||||
entry.SystemErr(err)
|
entry.SystemErr(err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -66,7 +67,7 @@ type validationResponse struct {
|
|||||||
Message []byte
|
Message []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func validate(hook, mod, ver string) (validationResponse, error) {
|
func validate(ctx context.Context, client *http.Client, hook, mod, ver string) (validationResponse, error) {
|
||||||
const op errors.Op = "actions.validate"
|
const op errors.Op = "actions.validate"
|
||||||
|
|
||||||
toVal := &validationParams{mod, ver}
|
toVal := &validationParams{mod, ver}
|
||||||
@@ -75,7 +76,12 @@ func validate(hook, mod, ver string) (validationResponse, error) {
|
|||||||
return validationResponse{Valid: false}, errors.E(op, err)
|
return validationResponse{Valid: false}, errors.E(op, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Post(hook, "application/json", bytes.NewBuffer(jsonVal))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, hook, bytes.NewReader(jsonVal))
|
||||||
|
if err != nil {
|
||||||
|
return validationResponse{}, errors.E(op, err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return validationResponse{Valid: false}, errors.E(op, err)
|
return validationResponse{Valid: false}, errors.E(op, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user