diff options
| author | Chia <Chia@93.nz> | 2026-08-04 19:58:52 +1200 |
|---|---|---|
| committer | Chia <Chia@93.nz> | 2026-08-04 20:43:23 +1200 |
| commit | 5b651488b081b65fda8a323f228e139adb79a35d (patch) | |
| tree | 08baf40efb8fe103b32721cd991ff712323e3173 /cmd/mockupstream | |
Build AI gateway control plane and admin UI
Diffstat (limited to '')
| -rw-r--r-- | cmd/mockupstream/main.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/cmd/mockupstream/main.go b/cmd/mockupstream/main.go new file mode 100644 index 0000000..cf695bb --- /dev/null +++ b/cmd/mockupstream/main.go @@ -0,0 +1,65 @@ +// Command mockupstream is a local-only upstream used for manual gateway checks. +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net/http" + "time" +) + +func main() { + address := flag.String("address", "127.0.0.1:18080", "listen address") + flag.Parse() + mux := http.NewServeMux() + mux.HandleFunc("POST /v1/chat/completions", openAI) + mux.HandleFunc("POST /v1/messages", anthropic) + server := &http.Server{Addr: *address, Handler: mux, ReadHeaderTimeout: 5 * time.Second} + log.Printf("mock upstream listening on http://%s", *address) + log.Fatal(server.ListenAndServe()) +} + +func openAI(w http.ResponseWriter, r *http.Request) { + var request struct { + Model string `json:"model"` + Stream bool `json:"stream"` + } + if json.NewDecoder(r.Body).Decode(&request) != nil { + http.Error(w, "invalid JSON", http.StatusBadRequest) + return + } + if request.Stream { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache") + fmt.Fprintf(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"model\":%q,\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"hello from mock upstream\"},\"finish_reason\":null}]}\n\n", request.Model) + w.(http.Flusher).Flush() + time.Sleep(50 * time.Millisecond) + fmt.Fprint(w, "data: {\"id\":\"chatcmpl-local\",\"object\":\"chat.completion.chunk\",\"choices\":[],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":4,\"total_tokens\":8}}\n\ndata: [DONE]\n\n") + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "chatcmpl-local", "object": "chat.completion", "model": request.Model, + "choices": []any{map[string]any{"index": 0, "message": map[string]string{"role": "assistant", "content": "hello from mock upstream"}, "finish_reason": "stop"}}, + "usage": map[string]int{"prompt_tokens": 4, "completion_tokens": 4, "total_tokens": 8}, + }) +} + +func anthropic(w http.ResponseWriter, r *http.Request) { + var request struct { + Model string `json:"model"` + } + if json.NewDecoder(r.Body).Decode(&request) != nil { + http.Error(w, "invalid JSON", http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "msg_local", "type": "message", "role": "assistant", "model": request.Model, + "content": []any{map[string]string{"type": "text", "text": "hello from mock upstream"}}, + "stop_reason": "end_turn", "stop_sequence": nil, + "usage": map[string]int{"input_tokens": 4, "output_tokens": 4}, + }) +} |
