mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 00:54:23 +00:00
Remove remote instance proxy handling from handlers
This commit is contained in:
@@ -7,8 +7,6 @@ import (
|
||||
"llamactl/pkg/manager"
|
||||
"llamactl/pkg/validation"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -375,12 +373,6 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if this is a remote instance
|
||||
if inst.IsRemote() {
|
||||
h.RemoteInstanceProxy(w, r, validatedName, inst)
|
||||
return
|
||||
}
|
||||
|
||||
if !inst.IsRunning() {
|
||||
http.Error(w, "Instance is not running", http.StatusServiceUnavailable)
|
||||
return
|
||||
@@ -393,9 +385,12 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// Strip the "/api/v1/instances/<name>/proxy" prefix from the request URL
|
||||
prefix := fmt.Sprintf("/api/v1/instances/%s/proxy", validatedName)
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
|
||||
// Check if this is a remote instance
|
||||
if !inst.IsRemote() {
|
||||
// Strip the "/api/v1/instances/<name>/proxy" prefix from the request URL
|
||||
prefix := fmt.Sprintf("/api/v1/instances/%s/proxy", validatedName)
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
|
||||
}
|
||||
|
||||
// Update the last request time for the instance
|
||||
inst.UpdateLastRequestTime()
|
||||
@@ -408,66 +403,3 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// RemoteInstanceProxy proxies requests to a remote instance
|
||||
func (h *Handler) RemoteInstanceProxy(w http.ResponseWriter, r *http.Request, name string, inst *instance.Instance) {
|
||||
// Get the node name from instance options
|
||||
options := inst.GetOptions()
|
||||
if options == nil {
|
||||
http.Error(w, "Instance has no options configured", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the first node from the set
|
||||
var nodeName string
|
||||
for node := range options.Nodes {
|
||||
nodeName = node
|
||||
break
|
||||
}
|
||||
if nodeName == "" {
|
||||
http.Error(w, "Instance has no node configured", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if we have a cached proxy for this node
|
||||
h.remoteProxiesMu.RLock()
|
||||
proxy, exists := h.remoteProxies[nodeName]
|
||||
h.remoteProxiesMu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
// Find node configuration
|
||||
nodeConfig, exists := h.cfg.Nodes[nodeName]
|
||||
if !exists {
|
||||
http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Create reverse proxy to remote node
|
||||
targetURL, err := url.Parse(nodeConfig.Address)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse node address: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
proxy = httputil.NewSingleHostReverseProxy(targetURL)
|
||||
|
||||
// Modify request before forwarding
|
||||
originalDirector := proxy.Director
|
||||
apiKey := nodeConfig.APIKey // Capture for closure
|
||||
proxy.Director = func(req *http.Request) {
|
||||
originalDirector(req)
|
||||
// Add API key if configured
|
||||
if apiKey != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the proxy by node name
|
||||
h.remoteProxiesMu.Lock()
|
||||
h.remoteProxies[nodeName] = proxy
|
||||
h.remoteProxiesMu.Unlock()
|
||||
}
|
||||
|
||||
// Forward the request using the cached proxy
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user