feat: add Location header to response

This commit is contained in:
jabuxas 2024-10-21 12:01:32 -03:00
parent fae72778d9
commit 6a2bf1ea4a
2 changed files with 12 additions and 2 deletions

View File

@ -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 {

View File

@ -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)
}