From 210e83d71ab8e0535232179a2da459b33cd6f1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B9=20=D0=A0=D1=8B=D0=B1=D0=B0=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Wed, 22 Jun 2022 20:45:56 +0300 Subject: [PATCH] add await header --- README.md | 4 ++++ apisprout.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 66a669d..bdca2a4 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ If your API spec is loaded from a remote URL, you can live-reload it by hitting A simple endpoint which returns status code `200` is available at `/__health`. This endpoint successfully returns `200` even if `--validate-server` is turned on, and the endpoint is being accessed from a non-validated host. +### Response delay + +To set the response delay time, add the header "await". A format value is a standard Goland duration in string representation (e.g. "300ms"). + ## Contributing Contributions are very welcome. Please open a tracking issue or pull request and we can work to get things merged in. diff --git a/apisprout.go b/apisprout.go index 44fc633..2aea86e 100644 --- a/apisprout.go +++ b/apisprout.go @@ -423,6 +423,18 @@ func mapContainsKey(dict map[string]string, key string) bool { var handler = func(rr *RefreshableRouter) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + + customAwait := req.Header.Get("await") + if customAwait != "" { + dur, err := time.ParseDuration(customAwait) + if err != nil { + log.Printf("ERROR: passed incorrect duration %s in await header", customAwait) + w.WriteHeader(http.StatusBadRequest) + return + } + time.Sleep(dur) + } + if !viper.GetBool("disable-cors") { corsOrigin := req.Header.Get("Origin") if corsOrigin == "" {