From 4bd6213fdc9f703fed4c1775342fd58e61290394 Mon Sep 17 00:00:00 2001 From: Andrew Thorp Date: Mon, 2 Aug 2021 22:17:19 -0400 Subject: [PATCH] Add RequireInputHandler This handler wrapper allows users to easily create flexible input-required endpoints using custom prompts. --- handlers.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/handlers.go b/handlers.go index 02cbf81..5f55235 100644 --- a/handlers.go +++ b/handlers.go @@ -79,6 +79,19 @@ func RequireCertificateHandler(h Handler, authoriser func(certID, certKey string }) } +// RequireInputHandler returns a handler that enforces all incoming requests have a populated +// querystring. +// `prompt` is returned as response META if input is not provided. +func RequireInputHandler(h Handler, prompt string) Handler { + return HandlerFunc(func(w ResponseWriter, r *Request) { + if r.URL.RawQuery == "" { + w.SetHeader(CodeInput, prompt) + return + } + h.ServeGemini(w, r) + }) +} + // AuthoriserAllowAll allows any authenticated user to access the handler. func AuthoriserAllowAll(id, key string) bool { return true