From 6a2bf1ea4a10fc7555dcf21d4b145b9e119a39e2 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Mon, 21 Oct 2024 12:01:32 -0300 Subject: [PATCH] feat: add Location header to response --- handlers.go | 4 ++-- helpers.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/handlers.go b/handlers.go index 5d24997..d674385 100644 --- a/handlers.go +++ b/handlers.go @@ -172,7 +172,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - fmt.Fprintf(w, "%s", fmt.Sprintf("http://%s/%s\n", app.url, filename)) + ResponseURLHandler(w, app.url, filename) } func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { @@ -209,7 +209,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - fmt.Fprintf(w, "%s", fmt.Sprintf("http://%s/%s\n", app.url, filename)) + ResponseURLHandler(w, app.url, filename) } func (app *Application) publicURL(file io.Reader, extension string) string { diff --git a/helpers.go b/helpers.go index a799e62..b4f2c40 100644 --- a/helpers.go +++ b/helpers.go @@ -119,3 +119,13 @@ func BasicAuth(next http.HandlerFunc, app *Application) http.HandlerFunc { http.Error(w, "Unauthorized", http.StatusUnauthorized) }) } + +func ResponseURLHandler(w http.ResponseWriter, url, filename string) { + pasteURL := fmt.Sprintf("http://%s/%s\n", url, filename) + + w.Header().Set("Location", pasteURL) + + w.WriteHeader(http.StatusCreated) + + fmt.Fprintf(w, "%s", pasteURL) +}