- giteaAPIBase/giteaIssueRepo/giteaIssueToken 常量编译进 server/version.go(含外网地址 :82) - config.go 移除 Gitea 三个字段;.env/.env.example 清理 GITEA_*,部署零配置 - e2e:零环境变量启动 → POST /api/v1/feedback 自动建 issue #2(外网 issue_url)→ 外网 API 关闭
128 lines
4.1 KiB
Go
128 lines
4.1 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// versionHandler 版本元数据(公开,无需登录——用于部署核对)。
|
||
func (a *app) versionHandler(c *gin.Context) {
|
||
c.JSON(http.StatusOK, versionInfo())
|
||
}
|
||
|
||
// changelogHandler 完整更新说明(Markdown 原文,公开)。
|
||
func (a *app) changelogHandler(c *gin.Context) {
|
||
c.Data(http.StatusOK, "text/markdown; charset=utf-8", []byte(changelogMarkdown()))
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// 反馈代理:公开端点 POST /api/v1/feedback → 服务端持 Gitea token 自动建 issue。
|
||
// 调用方(AI 客户端/用户)零凭据;限流防灌水。
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const feedbackHourlyLimit = 10 // 每 IP 每小时最多条数
|
||
|
||
var (
|
||
feedbackMu sync.Mutex
|
||
feedbackHits = map[string][]time.Time{} // ip -> 命中时间
|
||
)
|
||
|
||
func feedbackAllow(ip string) bool {
|
||
feedbackMu.Lock()
|
||
defer feedbackMu.Unlock()
|
||
now := time.Now()
|
||
recent := feedbackHits[ip][:0]
|
||
for _, t := range feedbackHits[ip] {
|
||
if now.Sub(t) < time.Hour {
|
||
recent = append(recent, t)
|
||
}
|
||
}
|
||
if len(recent) >= feedbackHourlyLimit {
|
||
feedbackHits[ip] = recent
|
||
return false
|
||
}
|
||
feedbackHits[ip] = append(recent, now)
|
||
return true
|
||
}
|
||
|
||
type feedbackInput struct {
|
||
Title string `json:"title"` // 必填;前缀 [bug]/[建议] 自动补
|
||
Body string `json:"body"` // 复现步骤/期望/实际
|
||
Kind string `json:"kind"` // bug | suggestion(默认 suggestion)
|
||
}
|
||
|
||
// feedbackHandler 公开反馈端点:把 bug/建议以 issue 形式自动提交到 Gitea。
|
||
// 目标地址与 token 编译进二进制(version.go 常量),部署零配置。
|
||
func (a *app) feedbackHandler(c *gin.Context) {
|
||
if giteaIssueToken == "" {
|
||
c.JSON(503, gin.H{"error": "反馈服务未配置(二进制内未嵌入 token),请改用 Web 入口建 issue"})
|
||
return
|
||
}
|
||
ip := c.ClientIP()
|
||
if !feedbackAllow(ip) {
|
||
c.JSON(429, gin.H{"error": "提交过于频繁,每小时最多 " + fmt.Sprint(feedbackHourlyLimit) + " 条"})
|
||
return
|
||
}
|
||
var in feedbackInput
|
||
if err := c.ShouldBindJSON(&in); err != nil || strings.TrimSpace(in.Title) == "" {
|
||
c.JSON(400, gin.H{"error": "title 必填;body 建议包含复现步骤/期望/实际"})
|
||
return
|
||
}
|
||
kind := in.Kind
|
||
if kind != "bug" && kind != "suggestion" {
|
||
kind = "suggestion"
|
||
}
|
||
// 标题规范化:无前缀则自动加
|
||
prefix := "[bug]"
|
||
if kind == "suggestion" {
|
||
prefix = "[建议]"
|
||
}
|
||
title := strings.TrimSpace(in.Title)
|
||
if !strings.HasPrefix(title, "[bug]") && !strings.HasPrefix(title, "[建议]") {
|
||
title = prefix + " " + title
|
||
}
|
||
// 正文自动附加版本与时间上下文
|
||
body := strings.TrimSpace(in.Body)
|
||
body += fmt.Sprintf("\n\n---\n- 提交渠道: /api/v1/feedback (%s)\n- 服务端版本: %s (commit %s)\n- 时间: %s",
|
||
kind, Version, Commit, time.Now().Format("2006-01-02 15:04:05 MST"))
|
||
|
||
payload, _ := json.Marshal(map[string]interface{}{"title": title, "body": body})
|
||
req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost,
|
||
giteaAPIBase+"/api/v1/repos/"+giteaIssueRepo+"/issues",
|
||
bytes.NewReader(payload))
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
req.Header.Set("Content-Type", "application/json")
|
||
req.Header.Set("Authorization", "token "+giteaIssueToken)
|
||
client := &http.Client{Timeout: 15 * time.Second}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
c.JSON(502, gin.H{"error": "Gitea 不可达: " + err.Error()})
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
var gi map[string]interface{}
|
||
_ = json.NewDecoder(resp.Body).Decode(&gi)
|
||
if resp.StatusCode != http.StatusCreated {
|
||
msg, _ := gi["message"].(string)
|
||
c.JSON(502, gin.H{"error": fmt.Sprintf("Gitea 拒绝 (HTTP %d): %s", resp.StatusCode, msg)})
|
||
return
|
||
}
|
||
number, _ := gi["number"].(float64)
|
||
htmlURL, _ := gi["html_url"].(string)
|
||
c.JSON(201, gin.H{
|
||
"ok": true, "issue_number": int(number), "issue_url": htmlURL,
|
||
"title": title,
|
||
"message": "issue 已自动创建,可在仓库 issues 页查看进展",
|
||
})
|
||
}
|