fix(server): handle router setup panic and return error

* return error from New function if route registration panics
* add googleDispatch to handle model:action routing
This commit is contained in:
2026-04-11 21:03:29 +02:00
parent c5608bf5cd
commit c12e16c9f7
4 changed files with 52 additions and 9 deletions

View File

@@ -1,8 +1,10 @@
package server
import (
"context"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/uptrace/bunrouter"
@@ -10,6 +12,31 @@ import (
"github.com/Warky-Devs/vecna.git/pkg/embedclient"
)
// googleDispatch parses the wildcard "model:action" segment and routes to the
// correct Google handler. The colon is a literal method separator in the
// Google embedding API, not a bunrouter parameter prefix.
func (h *handler) googleDispatch(w http.ResponseWriter, req bunrouter.Request) error {
modelaction := req.Param("modelaction") // e.g. "text-embedding-foo:embedContent"
idx := strings.LastIndex(modelaction, ":")
if idx < 0 {
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "invalid Google API path"})
}
model := modelaction[:idx]
action := modelaction[idx+1:]
ctx := context.WithValue(req.Context(), modelKey, model)
req = req.WithContext(ctx)
switch action {
case "embedContent":
return h.googleEmbedContent(w, req)
case "batchEmbedContents":
return h.googleBatchEmbedContents(w, req)
default:
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "unknown Google API method: " + action})
}
}
// --- single embedContent ---
type googleEmbedContentRequest struct {
@@ -34,7 +61,7 @@ type googleEmbeddingValues struct {
}
func (h *handler) googleEmbedContent(w http.ResponseWriter, req bunrouter.Request) error {
model := req.Param("model")
model, _ := req.Context().Value(modelKey).(string)
var body googleEmbedContentRequest
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
@@ -89,7 +116,7 @@ type googleBatchResponse struct {
}
func (h *handler) googleBatchEmbedContents(w http.ResponseWriter, req bunrouter.Request) error {
model := req.Param("model")
model, _ := req.Context().Value(modelKey).(string)
var body googleBatchRequest
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {