mirror of
https://github.com/Warky-Devs/vecna.git
synced 2026-05-05 01:26:58 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user