diff options
| author | wikiapiserver | 2026-06-25 22:00:25 +0200 |
|---|---|---|
| committer | wikiapiserver | 2026-06-25 22:00:25 +0200 |
| commit | 4907cfb72ef69fb94c49cfb484a5a2907db0dee8 (patch) | |
| tree | 9542d121d87cd2bb0074de0597069c9c49bdd372 | |
| parent | a67cf297df78521d1c2dee0dc2746a8046675b56 (diff) | |
| download | wikiapiserver-4907cfb72ef69fb94c49cfb484a5a2907db0dee8.tar.gz | |
fix: pass through upstream HTTP status codes from /article
- Non-2xx responses (404, 429, 5xx) are now forwarded to the
client with their original status code and response body
- Increased WriteTimeout to 45s to accommodate slow upstream API
- Added explicit resp.Body.Close() and response flush before return
| -rw-r--r-- | api/handlers.go | 10 | ||||
| -rw-r--r-- | main.go | 2 |
2 files changed, 10 insertions, 2 deletions
diff --git a/api/handlers.go b/api/handlers.go index 04fcc23..93f605e 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -258,6 +258,8 @@ func (h *Handler) GetArticle(w http.ResponseWriter, r *http.Request) { if resp.StatusCode < 200 || resp.StatusCode >= 300 { b, _ := io.ReadAll(resp.Body) + resp.Body.Close() + log.Printf("article request failed: user=%s article=%s status=%d body=%s (%dms)", username, article, resp.StatusCode, string(b), elapsed) h.db.LogApiCall(ctx, &db.ApiLogEntry{ @@ -268,7 +270,13 @@ func (h *Handler) GetArticle(w http.ResponseWriter, r *http.Request) { Error: string(b), RequestURL: queryURL, }) - http.Error(w, string(b), resp.StatusCode) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(resp.StatusCode) + w.Write(b) //nolint:errcheck + if f, ok := w.(http.Flusher); ok { + f.Flush() + } return } @@ -94,7 +94,7 @@ func main() { Addr: addr, Handler: mux, ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, + WriteTimeout: 45 * time.Second, IdleTimeout: 120 * time.Second, } |
