Files
ez-api/test/mock-upstream/main.go
zenfun 5826db3954 perf(json): replace encoding/json with bytedance/sonic
Migrate all JSON marshaling and unmarshaling operations to use
github.com/bytedance/sonic for improved performance. This affects
adapters, middleware, proxy handlers, and the sync store.
2025-12-10 23:21:51 +08:00

53 lines
1.2 KiB
Go

package main
import (
"log"
"net/http"
"github.com/bytedance/sonic"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
mux.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"id": "mock-completion",
"object": "chat.completion",
"choices": []map[string]interface{}{
{
"index": 0,
"message": map[string]string{
"role": "assistant",
"content": "hello from mock",
},
"finish_reason": "stop",
},
},
}
w.Header().Set("Content-Type", "application/json")
_ = sonic.ConfigDefault.NewEncoder(w).Encode(resp)
})
mux.HandleFunc("/v1/models", func(w http.ResponseWriter, r *http.Request) {
resp := map[string]interface{}{
"object": "list",
"data": []map[string]string{
{"id": "mock-model", "object": "model", "owned_by": "mock"},
},
}
w.Header().Set("Content-Type", "application/json")
_ = sonic.ConfigDefault.NewEncoder(w).Encode(resp)
})
log.Println("mock-upstream listening on :8082")
if err := http.ListenAndServe(":8082", mux); err != nil {
log.Fatalf("mock-upstream failed: %v", err)
}
}