Files
homelab-frontend/internal/proxy/router.go
T
Story Crater BotandClaude Opus 5 058f11cf2b
CI / Test (push) Canceled after 0s
CI / Vet (push) Canceled after 0s
CI / Build (push) Canceled after 0s
CI / Security (govulncheck) (push) Canceled after 0s
chore: initial commit of Go API gateway
Baseline for the Kong replacement on api.riotpiao.com. Brings the working
tree under version control for the first time: gateway source, the task
board that drives the agent runs, test fixtures, and K8s manifests.

Anchor the gateway ignore rule to the repo root. Unanchored, "gateway"
also matched the cmd/gateway/ source directory, so the program entrypoint
was excluded from every commit.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-19 20:54:34 -07:00

93 lines
2.7 KiB
Go

// Package proxy provides request routing and forwarding.
package proxy
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/Riotpiaole/homelab-frontend/internal/config"
)
// RouteRequest determines which upstream should handle the request.
// For /v1/* routes, it uses body-based dispatch (reads JSON to find "model" field).
// For other routes, it returns the single configured route.
func (h *Handler) RouteRequest(r *http.Request) (*Route, error) {
// For /v1/chat/completions, use body-based dispatch
if r.URL.Path == "/v1/chat/completions" && r.Method == "POST" {
return h.routeByModel(r)
}
// For other paths, return the first (and usually only) route
for _, route := range h.routes {
return route, nil
}
return nil, fmt.Errorf("no route available")
}
// routeByModel reads the request body to find the "model" field and routes accordingly.
// The body is preserved for forwarding to the upstream.
func (h *Handler) routeByModel(r *http.Request) (*Route, error) {
// If there's no body, we can't determine the model
if r.Body == nil {
return nil, fmt.Errorf("request body required")
}
// Read the body to extract the model name
// We need to be careful to preserve the body for the upstream
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("failed to read request body: %w", err)
}
// Restore the body so it can be read again by the upstream
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
// Parse the JSON to find the model field
var payload map[string]interface{}
if err := json.Unmarshal(bodyBytes, &payload); err != nil {
return nil, fmt.Errorf("invalid JSON in request body: %w", err)
}
// Extract the model name
modelName, ok := payload["model"].(string)
if !ok {
return nil, fmt.Errorf("model field missing or not a string")
}
// Look up the model in the registry
modelUpstream := h.config.LookupModel(modelName)
if modelUpstream == nil {
return nil, fmt.Errorf("unknown model: %q", modelName)
}
// Create a route for this model with appropriate timeouts
// These are sensible defaults for LLM models
upstreamCfg := &config.Upstream{
Address: modelUpstream.Address,
PathRewrite: "/v1/chat/completions",
ConnectTimeout: h.defaultConnectTimeout,
ReadTimeout: h.defaultReadTimeout,
WriteTimeout: h.defaultWriteTimeout,
MaxBodySize: h.defaultMaxBodySize,
AuthRequired: false,
}
targetURL, _ := url.Parse("http://" + modelUpstream.Address)
route := &Route{
Name: "v1-chat-" + modelName,
Upstream: upstreamCfg,
Transport: h.getOrCreateTransport(modelUpstream.Address, upstreamCfg),
Director: func(req *http.Request) {
directorFunc(req, targetURL, upstreamCfg)
},
}
return route, nil
}