feat: proxy requests to service adapter upstreams instead of echoing
This commit is contained in:
@@ -2,7 +2,11 @@ package serviceadapter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
"forgejo.riotpiao.com/rock/homelab-frontend/internal/problem"
|
"forgejo.riotpiao.com/rock/homelab-frontend/internal/problem"
|
||||||
)
|
)
|
||||||
@@ -104,12 +108,36 @@ func (d *Dispatcher) Dispatch(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Call upstream with method.UpstreamPath, apply retry logic, etc.
|
// Build upstream URL
|
||||||
// For now, just echo that dispatch would happen
|
upstreamURL, err := url.Parse(adapter.Spec.Upstream.URL)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
d.writeError(w, problem.NewProblem(http.StatusInternalServerError, "about:blank#server-error",
|
||||||
fmt.Fprintf(w, `{"service":"%s","resource":"%s","method":"%s","upstream":"%s"}`,
|
"Internal Server Error", fmt.Sprintf("invalid upstream URL: %v", err)))
|
||||||
serviceName, resourceName, r.Method, adapter.Spec.Upstream.URL)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create reverse proxy with director that rewrites the path
|
||||||
|
proxy := httputil.NewSingleHostReverseProxy(upstreamURL)
|
||||||
|
proxy.Director = func(req *http.Request) {
|
||||||
|
req.URL.Scheme = upstreamURL.Scheme
|
||||||
|
req.URL.Host = upstreamURL.Host
|
||||||
|
req.URL.Path = method.UpstreamPath
|
||||||
|
req.RequestURI = ""
|
||||||
|
req.Host = upstreamURL.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set timeout
|
||||||
|
timeout := adapter.Spec.Upstream.TimeoutSeconds
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = 30
|
||||||
|
}
|
||||||
|
proxy.Transport = &http.Transport{
|
||||||
|
DialContext: (&net.Dialer{Timeout: time.Duration(timeout) * time.Second}).DialContext,
|
||||||
|
TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward the request
|
||||||
|
proxy.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasCapability checks if the request has the required capability.
|
// hasCapability checks if the request has the required capability.
|
||||||
|
|||||||
Reference in New Issue
Block a user