M0:神识 Go 网关骨架(IR 入站/SSE 出站/stub 适配器/服务令牌/健康检查)及测试与镜像

This commit is contained in:
chenxin committed 2026-08-21 22:01:41 +08:00
1 parent 7ef3a15061
commit f3058a64c3
10 files changed
+514

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
// Package relay 负责流式转发:把适配器增量以 SSE 帧写出。
// 转发红线(shenshi-gateway.md §5):X-Accel-Buffering 防代理缓冲;帧即写即刷。
package relay
import (
"encoding/json"
"fmt"
"net/http"
"hunjing/shenshi/internal/ir"
)
// SSEHeaders 设置流式响应头(在写第一帧前调用)。
func SSEHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("X-Accel-Buffering", "no")
}
// WriteDelta 写一帧增量事件(data: <Delta JSON>)。
func WriteDelta(w http.ResponseWriter, d ir.Delta) error {
body, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("序列化增量失败: %w", err)
}
_, err = fmt.Fprintf(w, "data: %s\n\n", body)
if err != nil {
return err
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
return nil
}
// WriteDone 写终止帧(data: [DONE])。
func WriteDone(w http.ResponseWriter) error {
_, err := fmt.Fprint(w, "data: [DONE]\n\n")
if err != nil {
return err
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
return nil
}