fix(report): 报告源/产物落 MinIO,去掉 gateway↔mcp-go 本地盘耦合

多机/容器化第一时间断裂的隐患(ARCHITECTURE_REVIEW §7 #1):此前报告
源(reportStore)与 .docx 产物(reportExport/reportRender)都写 mcp-go
本地 SUNDYNIX_REPORTS_DIR,gateway 再按同一路径 c.File 回流——mcp-go
与 gateway 不共享磁盘即断。

- blob 包从 gateway/internal 提到 sundynix-shared/blob,gateway 与 mcp-go
  共用同一 MinIO;新增 PutBytes/GetBytes 走二进制(.docx)。
- mcp-go 注入 blob:report 源/产物优先 Put 到对象存储(键 reports/<id>.{json,docx}),
  导出结果返回 minio://<key>;MinIO 未就绪回退本地盘(单机降级,getSource 兼容旧本地报告)。
- gateway ExportReport 识别 minio:// → GetBytes 流式下载,否则 c.File 本地降级。
- 测试:mcp 本地回落往返单测 + shared/blob 真 MinIO 二进制往返(BLOB_TEST_ENDPOINT 门控)。
  四模块 build 绿,受影响测试全过。
- 附带:go mod tidy 引入 genproto 拆包冲突,四模块统一钉单体 genproto 新版解决
  (注:勿 go work sync,会剪掉 indirect require 回落旧版重现冲突)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-18 13:29:31 +08:00
parent 1c7e8e1ea9
commit ec9431bfa4
20 changed files with 484 additions and 182 deletions
-75
View File
@@ -1,75 +0,0 @@
// Package blob 封装对象存储(MinIO):大文档正文落对象存储,PG 只留元数据 + 预览 + 对象键。
package blob
import (
"bytes"
"context"
"io"
"log"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// Store 是对象存储句柄;cli 为 nil 表示降级(未连上 MinIO,大文档回退内联存 PG)。
type Store struct {
cli *minio.Client
bucket string
}
// Open 连接 MinIO 并确保 bucket 存在。连接失败返回降级实例(cli=nil),不阻断网关启动。
func Open(endpoint, accessKey, secretKey, bucket string) *Store {
cli, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: false,
})
if err != nil {
log.Printf("[blob] MinIO 不可用,大文档回退内联: %v", err)
return &Store{}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
exists, err := cli.BucketExists(ctx, bucket)
if err != nil {
log.Printf("[blob] MinIO 连接失败,大文档回退内联: %v", err)
return &Store{}
}
if !exists {
if err := cli.MakeBucket(ctx, bucket, minio.MakeBucketOptions{}); err != nil {
log.Printf("[blob] 建 bucket 失败,大文档回退内联: %v", err)
return &Store{}
}
}
log.Printf("[blob] MinIO connected %s bucket=%s", endpoint, bucket)
return &Store{cli: cli, bucket: bucket}
}
// Ready 报告对象存储是否可用。
func (s *Store) Ready() bool { return s != nil && s.cli != nil }
// Put 写入一段文本到对象键 key。
func (s *Store) Put(ctx context.Context, key, content string) error {
r := bytes.NewReader([]byte(content))
_, err := s.cli.PutObject(ctx, s.bucket, key, r, int64(len(content)), minio.PutObjectOptions{ContentType: "text/plain; charset=utf-8"})
return err
}
// Get 读回对象键 key 的全部文本。
func (s *Store) Get(ctx context.Context, key string) (string, error) {
obj, err := s.cli.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
if err != nil {
return "", err
}
defer obj.Close()
b, err := io.ReadAll(obj)
if err != nil {
return "", err
}
return string(b), nil
}
// Delete 删除对象;返回错误供级联删按需失败闭合(其它 best-effort 调用点可忽略返回值)。
func (s *Store) Delete(ctx context.Context, key string) error {
return s.cli.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
}
+1 -1
View File
@@ -18,7 +18,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/sundynix/sundynix-gateway/internal/blob"
"github.com/sundynix/sundynix-shared/blob"
"github.com/sundynix/sundynix-gateway/internal/nats"
"github.com/sundynix/sundynix-gateway/internal/store"
"github.com/sundynix/sundynix-shared/contract"
+19 -3
View File
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"net/http"
"strings"
"github.com/gin-gonic/gin"
@@ -72,10 +73,25 @@ func (h *Handler) ExportReport(c *gin.Context) {
c.Header("Content-Disposition", `attachment; filename="`+id+`.md"`)
c.Header("Content-Type", "text/markdown; charset=utf-8")
c.String(http.StatusOK, res.Content)
default: // docxres.Content 为 mcp-go 落盘的 .docx 路径
default: // docxres.Content 为 minio://<key>(对象存储,跨机可取)或本地路径(单机降级)
const docxMime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
c.Header("Content-Disposition", `attachment; filename="`+id+`.docx"`)
c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
c.File(res.Content)
c.Header("Content-Type", docxMime)
if key, ok := strings.CutPrefix(res.Content, contract.BlobScheme); ok {
// 对象存储:从 MinIO 流式取回,不依赖 gateway 与 mcp-go 共享本地盘。
if h.blob == nil || !h.blob.Ready() {
c.JSON(http.StatusBadGateway, gin.H{"error": "对象存储不可用,无法下载报告"})
return
}
data, gerr := h.blob.GetBytes(c.Request.Context(), key)
if gerr != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "报告产物已过期或不存在"})
return
}
c.Data(http.StatusOK, docxMime, data)
return
}
c.File(res.Content) // 本地路径:单机/共享卷降级
}
}
@@ -16,7 +16,7 @@ import (
"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin"
"github.com/sundynix/sundynix-gateway/internal/blob"
"github.com/sundynix/sundynix-shared/blob"
"github.com/sundynix/sundynix-gateway/internal/dsl"
"github.com/sundynix/sundynix-gateway/internal/nats"
"github.com/sundynix/sundynix-gateway/internal/payment"
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"github.com/sundynix/sundynix-gateway/internal/blob"
"github.com/sundynix/sundynix-shared/blob"
"github.com/sundynix/sundynix-gateway/internal/handler"
"github.com/sundynix/sundynix-gateway/internal/middleware"
"github.com/sundynix/sundynix-gateway/internal/nats"