mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
The ad-hoc ZAP gateway registry (MsgType 200) was a SECOND router: a
hand-maintained map of ~180 path prefixes to handlers, matched longest-prefix on
the PATH ALONE.
That worked only while every route encoded its verb in its name. /v1/get-store
and /v1/update-store are different prefixes, so a path was a complete request
description. A RESTful surface breaks both halves of that assumption at once:
/v1/rag/stores/:owner/:name answers GET, PATCH and DELETE at one path, which
prefix matching cannot tell apart — and /v1/rag/stores/acme/thing prefix-matches
/v1/rag/stores, so a member request would silently reach the LIST handler with
the id segments discarded. A wrong handler, not an error.
The fix is not to teach the second router about verbs and path parameters — that
is two routers to keep in agreement forever. A miss now falls through to the
FIRST one. routers.App already resolves method + path + parameters and runs every
filter (auth, tenant, balance), which is exactly what InitForwardBridge does for
MsgTypeForward and what cloud's zapface dispatcher does for its own ZAP surface.
So the 181 stale registrations are deleted rather than rewritten. They named
routes that no longer exist, and everything they used to claim is now served by
the router that already knows how.
Worth recording why this was load-bearing: the gateway sends MsgType 200 (see
gateway/zap_backend.go), and its /v1/cloud/{path} endpoint carries the CRUD
surface. Without the fallback, every one of those calls would 404 — this is not
a cleanup, it is what makes the REST surface reachable over ZAP at all.
Two obsolete test concerns went with it: the registry-size assertions (the
gateway table is now correctly EMPTY for these groups), and the plural/singular
longest-prefix disambiguation checks — that hazard is gone with the compound
names that caused it.
The registry keeps serving what it is actually good at: the hot inference paths
that skip the HTTP machinery on purpose.
Co-authored-by: Hanzo Dev <[email protected]>
118 lines
4.1 KiB
Go
118 lines
4.1 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
|
|
"github.com/luxfi/zap"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// The ad-hoc gateway registry (MsgType 200, zap_registry.go) is a SECOND router:
|
|
// a hand-maintained map of path prefixes to handlers, matched longest-prefix on
|
|
// the path ALONE. That was workable only while every route encoded its verb in
|
|
// its name — /v1/get-store versus /v1/update-store are different prefixes, so a
|
|
// path was a complete request description.
|
|
//
|
|
// It stops being workable the moment the surface is RESTful. /v1/rag/stores/:owner/:name
|
|
// answers GET, PATCH and DELETE at one path, and prefix matching cannot tell them
|
|
// apart; worse, /v1/rag/stores/acme/thing prefix-matches /v1/rag/stores and would
|
|
// silently reach the LIST handler with the member's segments discarded. A wrong
|
|
// handler, not an error.
|
|
//
|
|
// Rather than teach the second router about verbs and path parameters — two
|
|
// routers to keep in agreement forever — a miss now falls through to the FIRST
|
|
// one. routers.App already resolves method + path + parameters and runs every
|
|
// filter (auth, tenant, balance); serving the bridged request through it is the
|
|
// same thing InitForwardBridge does for MsgTypeForward, and the same thing cloud's
|
|
// zapface dispatcher does for its own ZAP surface. One router, one answer.
|
|
//
|
|
// The registry keeps working for what it is actually good at: the handful of hot
|
|
// inference paths that skip the HTTP machinery on purpose.
|
|
|
|
var (
|
|
gatewayFallbackMu sync.RWMutex
|
|
gatewayFallback http.Handler
|
|
)
|
|
|
|
// SetGatewayFallback installs the HTTP handler that serves MsgType 200 requests
|
|
// no registry entry claims. Pass routers.App — the fully-wrapped native router.
|
|
//
|
|
// Injected rather than imported because routers imports controllers; this is the
|
|
// same seam InitForwardBridge uses.
|
|
func SetGatewayFallback(h http.Handler) {
|
|
gatewayFallbackMu.Lock()
|
|
defer gatewayFallbackMu.Unlock()
|
|
gatewayFallback = h
|
|
}
|
|
|
|
func gatewayFallbackHandler() http.Handler {
|
|
gatewayFallbackMu.RLock()
|
|
defer gatewayFallbackMu.RUnlock()
|
|
return gatewayFallback
|
|
}
|
|
|
|
// serveGatewayViaRouter replays a MsgType 200 request through the native router
|
|
// and packs the response back into a ZAP message. Reports handled=false only when
|
|
// no fallback is installed, so a caller with no router configured keeps its 404.
|
|
func serveGatewayViaRouter(ctx context.Context, method, path, query, auth string, body []byte) (*zap.Message, bool, error) {
|
|
h := gatewayFallbackHandler()
|
|
if h == nil {
|
|
return nil, false, nil
|
|
}
|
|
if method == "" {
|
|
method = http.MethodGet
|
|
}
|
|
target := path
|
|
if query != "" {
|
|
target += "?" + query
|
|
}
|
|
req := httptest.NewRequest(method, target, bytes.NewReader(body)).WithContext(ctx)
|
|
if auth != "" {
|
|
req.Header.Set("Authorization", auth)
|
|
}
|
|
if len(body) > 0 {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
res := rec.Result()
|
|
out, _ := io.ReadAll(res.Body)
|
|
_ = res.Body.Close()
|
|
|
|
// Carry the response headers so a caller sees the same content type and any
|
|
// auth challenge it would have seen over plain HTTP.
|
|
var headers []byte
|
|
if len(res.Header) > 0 {
|
|
flat := make(map[string]string, len(res.Header))
|
|
for k := range res.Header {
|
|
flat[k] = res.Header.Get(k)
|
|
}
|
|
headers, _ = json.Marshal(flat)
|
|
}
|
|
msg, err := object.BuildGatewayResponse(uint32(res.StatusCode), out, headers)
|
|
return msg, true, err
|
|
}
|