Skip to content

Commit

Permalink
feat: add Location header to response
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuxas committed Oct 21, 2024
1 parent fae7277 commit 6a2bf1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6a2bf1e

Please sign in to comment.