Skip to content

Commit

Permalink
chore: add further debug information to proxy status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsteinhaeuser committed Nov 26, 2024
1 parent fd36e77 commit eab59cc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion services/view/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"context"
_ "embed"
"encoding/json"
"html/template"
"io"
"log/slog"
"net/http"
"os"
"time"
)

var (
Expand Down Expand Up @@ -49,20 +51,26 @@ func main() {
}

func proxyEndpoint(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest(r.Method, envStatusServiceURL, r.Body)
ctx, cf := context.WithTimeout(r.Context(), 10*time.Second)
defer cf()

slog.Debug("preparing proxy request", "method", r.Method, "url", r.URL.Path)
req, err := http.NewRequestWithContext(ctx, r.Method, envStatusServiceURL, r.Body)
if err != nil {
slog.Error("could not create proxy request", "error", err)
http.Error(w, "could not create proxy request", http.StatusInternalServerError)
return
}

slog.Debug("copying headers from original request")
// Copy headers from the original request
for key, values := range r.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}

slog.Debug("sending proxy request")
resp, err := http.DefaultClient.Do(req)
if err != nil {
slog.Error("could not forward request", "error", err)
Expand All @@ -71,13 +79,15 @@ func proxyEndpoint(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

slog.Debug("copying response headers")
// Copy headers from the response
for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}

slog.Debug("writing response")
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err != nil {
Expand Down

0 comments on commit eab59cc

Please sign in to comment.