Skip to content

Commit

Permalink
Add custom response status code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Jul 20, 2020
1 parent c9a59b5 commit d74c867
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -226,12 +227,17 @@ func (s *server) buildNotFoundResponse(w http.ResponseWriter, r *http.Request, t

// TODO: add cache precondition
w.Header().Set("Cache-Control", "max-age=60, public")
w.WriteHeader(http.StatusNotFound)
statusCode, _ := strconv.Atoi(r.URL.Query().Get("status"))
if statusCode < 200 || statusCode >= 500 {
statusCode = http.StatusNotFound
}

w.WriteHeader(statusCode)
_, _ = io.Copy(w, resizedFile)

_ = resizedFile.Close()

logger("== RESP %s == %d %s\n", time.Now().Format("15:04:05.000"), http.StatusNotFound, resizedFile.Name())
logger("== RESP %s == %d %s\n", time.Now().Format("15:04:05.000"), statusCode, resizedFile.Name())
}

func (s *server) buildErrorResponse(w http.ResponseWriter, err error) {
Expand Down

0 comments on commit d74c867

Please sign in to comment.