Files
FlowStack/backend/internal/handler/pipelines.go
T
Logan.Li 97193eb090 feat: v0.4.0 主题切换 + LLM 全面优化 + 登录会话 + 用户管理/LDAP
主题:
- 深色/浅色/跟随系统(useTheme:本地缓存先行+远端同步+matchMedia),
  侧边栏快捷切换,设置页三档即选即生效

LLM(参考 opsxdb/aiops):
- llm.Client.ChatStream 流式(SSE 增量解析,透传 reasoning_content,
  ctx 控制超时);POST /api/ai/chat/stream(平台实时数据注入降幻觉,
  未配 LLM 降级规则 JSON,指令解析 action 不变)
- test-llm 支持草稿直测(保存前验证,apiKey 空/掩码回退已存)
- 发布失败自动 AI 分析日志尾部(结论/根因/建议),发布记录与发布
  Tab 展示分析卡片
- 设置页 Provider 预设(DeepSeek/GLM/GLM编码/Qwen/Kimi/Ollama/自定义)
  自动填 endpoint+模型+获取 Key 直达链接

登录与会话:
- HMAC 签名 token(7 天,密钥派生自 encryption.key);
  authMiddleware 保护 /api/*(豁免登录/健康/SSE日志流/入站webhook)
- 登录页 /login + 未登录跳转 + 登出;401 自动清理跳登录

用户管理与 LDAP(借鉴 LokiStack):
- /users 页面:本地用户 CRUD、一次性密码、角色切换、来源徽章
  (本地·蓝 | LDAP导入·紫);LDAP 导入按钮仅在启用且配置齐备时显示
- LDAP 配置卡(未启用仅提示语)、测试连接草稿直测+可选测试账号
- go-ldap:管理 Bind/用户搜索(防注入)/用户 DN 直连验证;
  批量导入冲突策略(已LDAP跳过/本地同名转LDAP/新建);
  登录分流本地 argon2id 或 LDAP 目录验证 + 首登 JIT 建号
2026-10-27 12:58:21 +08:00

344 lines
11 KiB
Go

package handler
import (
"context"
"strings"
"net/http"
"time"
"github.com/gin-gonic/gin"
"flowstack/internal/crypto"
"flowstack/internal/database"
"flowstack/internal/llm"
"flowstack/internal/model"
)
// ListPipelines GET /api/pipelines —— 附带每条流水线最近一次运行信息
func ListPipelines(c *gin.Context) {
var list []model.Pipeline
database.DB.Order("updated_at DESC").Find(&list)
// 每流水线最近一次 run(一次查询取全表后内存分组,数据量小;jianmu 用反规范化表,此处无需)
var runs []model.PipelineRun
database.DB.Order("started_at DESC").Limit(500).Find(&runs)
lastByPipeline := make(map[uint]model.PipelineRun)
for _, r := range runs {
if _, ok := lastByPipeline[r.PipelineID]; !ok {
lastByPipeline[r.PipelineID] = r
}
}
type pipelineWithLastRun struct {
model.Pipeline
LastRun *model.PipelineRun `json:"lastRun"`
}
result := make([]pipelineWithLastRun, 0, len(list))
for _, p := range list {
item := pipelineWithLastRun{Pipeline: p}
if r, ok := lastByPipeline[p.ID]; ok {
item.LastRun = &r
}
result = append(result, item)
}
c.JSON(http.StatusOK, result)
}
// GetPipelineDetail GET /api/pipelines/:id —— 完整详情(编辑器各 Tab 数据源)
func GetPipelineDetail(c *gin.Context) {
var p model.Pipeline
if err := database.DB.First(&p, c.Param("id")).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
return
}
var nodes []model.PipelineNode
database.DB.Where("pipeline_id = ?", p.ID).Order("position_x").Find(&nodes)
var edges []model.PipelineEdge
database.DB.Where("pipeline_id = ?", p.ID).Find(&edges)
var lastRun model.PipelineRun
hasRun := database.DB.Where("pipeline_id = ?", p.ID).Order("started_at DESC").First(&lastRun).Error == nil
c.JSON(http.StatusOK, gin.H{
"pipeline": p, "nodes": nodes, "edges": edges,
"lastRun": func() *model.PipelineRun {
if hasRun {
return &lastRun
}
return nil
}(),
})
}
// GetSummary GET /api/summary —— 侧边栏徽标与首页统计
func GetSummary(c *gin.Context) {
var buildNodes, plugins, running, pipelines, hosts, clusters int64
database.DB.Model(&model.BuildNode{}).Count(&buildNodes)
database.DB.Model(&model.Plugin{}).Count(&plugins)
database.DB.Model(&model.PipelineRun{}).Where("status IN ('pending','queued','running')").Count(&running)
database.DB.Model(&model.Pipeline{}).Count(&pipelines)
database.DB.Model(&model.Host{}).Count(&hosts)
database.DB.Model(&model.K8sCluster{}).Count(&clusters)
c.JSON(http.StatusOK, gin.H{
"buildNodes": buildNodes, "plugins": plugins, "running": running,
"pipelines": pipelines, "hosts": hosts, "clusters": clusters,
})
}
// GetSettings GET /api/settings —— 单行设置
func GetSettings(c *gin.Context) {
var s model.SystemSetting
if err := database.DB.First(&s, 1).Error; err != nil {
s = model.SystemSetting{ID: 1, LlmEndpoint: "https://api.deepseek.com/v1", LlmModel: "deepseek-chat", LlmMaxTokens: 1200}
database.DB.Create(&s)
}
s.LlmApiKeySet = s.LlmApiKey != ""
s.LarkSecretSet = s.LarkSecret != ""
s.LdapBindPasswordSet = s.LdapBindPassword != ""
c.JSON(http.StatusOK, s)
}
// UpdateSettings PUT /api/settings
func UpdateSettings(c *gin.Context) {
var s model.SystemSetting
if err := database.DB.First(&s, 1).Error; err != nil {
s = model.SystemSetting{ID: 1}
database.DB.Create(&s)
}
var body map[string]any
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updates := map[string]any{"updated_at": time.Now()}
if v, ok := body["theme"].(string); ok {
updates["theme"] = v
}
if v, ok := body["notification"].(string); ok {
updates["notification"] = v
}
if v, ok := body["toolMode"].(string); ok {
updates["tool_mode"] = v
}
if v, ok := body["extra"].(string); ok {
updates["extra"] = v
}
if v, ok := body["llmEnabled"].(bool); ok {
updates["llm_enabled"] = v
}
if v, ok := body["llmEndpoint"].(string); ok {
updates["llm_endpoint"] = v
}
if v, ok := body["llmModel"].(string); ok {
updates["llm_model"] = v
}
if v, ok := body["llmMaxTokens"].(float64); ok {
updates["llm_max_tokens"] = int(v)
}
// API Key 只写不读:非空才更新
if v, ok := body["llmApiKey"].(string); ok && v != "" {
enc, err := crypto.EncryptField(v)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "API Key 加密失败"})
return
}
updates["llm_api_key"] = enc
}
if v, ok := body["larkEnabled"].(bool); ok {
updates["lark_enabled"] = v
}
if v, ok := body["larkWebhook"].(string); ok {
updates["lark_webhook"] = v
}
if v, ok := body["larkSecret"].(string); ok && v != "" {
enc, err := crypto.EncryptField(v)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "签名密钥加密失败"})
return
}
updates["lark_secret"] = enc
}
database.DB.Model(&s).Updates(updates)
database.DB.First(&s, 1)
s.LlmApiKeySet = s.LlmApiKey != ""
s.LarkSecretSet = s.LarkSecret != ""
s.LdapBindPasswordSet = s.LdapBindPassword != ""
c.JSON(http.StatusOK, s)
}
// TestLLM POST /api/settings/test-llm —— 支持草稿直测:body 带表单值时优先(apiKey 空=用已存)
func TestLLM(c *gin.Context) {
var s model.SystemSetting
if err := database.DB.First(&s, 1).Error; err != nil {
s = model.SystemSetting{}
}
var body struct {
LlmEnabled *bool `json:"llmEnabled"`
LlmEndpoint string `json:"llmEndpoint"`
LlmModel string `json:"llmModel"`
LlmApiKey string `json:"llmApiKey"`
}
_ = c.ShouldBindJSON(&body)
enabled := s.LlmEnabled
if body.LlmEnabled != nil {
enabled = *body.LlmEnabled
}
endpoint := body.LlmEndpoint
if endpoint == "" {
endpoint = s.LlmEndpoint
}
model_ := body.LlmModel
if model_ == "" {
model_ = s.LlmModel
}
apiKey := body.LlmApiKey
if apiKey == "" || strings.Contains(apiKey, "****") {
apiKey = s.LlmApiKey
if crypto.IsEncrypted(apiKey) {
if plain, err := crypto.DecryptField(apiKey); err == nil {
apiKey = plain
}
}
}
if !enabled || apiKey == "" || endpoint == "" || model_ == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "请填写启用/接口/模型/API Key(或先保存已有配置)"})
return
}
client := llm.NewClient(endpoint, apiKey, model_, 128)
ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
defer cancel()
reply, err := client.Chat(ctx, "你是连通性检查助手,只回复:连接成功。", "ping")
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "测试失败: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "message": reply})
}
// CreatePipeline POST /api/pipelines
func CreatePipeline(c *gin.Context) {
var input struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
Type string `json:"type"`
SourceRepo string `json:"sourceRepo"`
SourceBranch string `json:"sourceBranch"`
DeployType string `json:"deployType"`
Env string `json:"env"`
}
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "name 不能为空"})
return
}
setDefault(&input.Type, "native")
setDefault(&input.SourceBranch, "main")
setDefault(&input.DeployType, "host")
setDefault(&input.Env, "staging")
p := model.Pipeline{
Name: input.Name, Description: input.Description, Type: input.Type,
SourceRepo: input.SourceRepo, SourceBranch: input.SourceBranch,
DeployType: input.DeployType, Env: input.Env,
}
if err := database.DB.Create(&p).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, p)
}
// UpdatePipeline PUT /api/pipelines/:id
func UpdatePipeline(c *gin.Context) {
var p model.Pipeline
if err := database.DB.First(&p, c.Param("id")).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
return
}
var input map[string]any
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
allowed := map[string]bool{
"name": true, "description": true, "sourceRepo": true, "sourceBranch": true,
"gitopsRepo": true, "gitopsPath": true, "deployType": true, "env": true,
"triggerMode": true, "cronExpr": true, "webhookToken": true, "watchBranches": true,
"variables": true, "cachePaths": true, "notifyWebhookURL": true, "jenkinsJobName": true,
}
updates := map[string]any{}
for k, v := range input {
if allowed[k] {
updates[k] = v
}
}
if len(updates) > 0 {
updates["updated_at"] = time.Now()
database.DB.Model(&p).Updates(updates)
}
c.JSON(http.StatusOK, p)
}
// DeletePipeline DELETE /api/pipelines/:id —— 级联删除节点与连线
func DeletePipeline(c *gin.Context) {
id := c.Param("id")
database.DB.Where("pipeline_id = ?", id).Delete(&model.PipelineEdge{})
database.DB.Where("pipeline_id = ?", id).Delete(&model.PipelineNode{})
database.DB.Delete(&model.Pipeline{}, id)
c.JSON(http.StatusOK, gin.H{"success": true})
}
// SaveNodes PUT /api/pipelines/:id/nodes —— 全量覆盖保存
func SaveNodes(c *gin.Context) {
pipelineID := c.Param("id")
var p model.Pipeline
if err := database.DB.First(&p, pipelineID).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
return
}
var body struct {
Nodes []model.PipelineNode `json:"nodes"`
}
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
database.DB.Where("pipeline_id = ?", p.ID).Delete(&model.PipelineNode{})
for i := range body.Nodes {
body.Nodes[i].ID = 0
body.Nodes[i].PipelineID = p.ID
database.DB.Create(&body.Nodes[i])
}
database.DB.Model(&p).Update("updated_at", time.Now())
c.JSON(http.StatusOK, gin.H{"success": true})
}
// SaveEdges PUT /api/pipelines/:id/edges —— 全量覆盖保存
func SaveEdges(c *gin.Context) {
pipelineID := c.Param("id")
var p model.Pipeline
if err := database.DB.First(&p, pipelineID).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "pipeline not found"})
return
}
var body struct {
Edges []model.PipelineEdge `json:"edges"`
}
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
database.DB.Where("pipeline_id = ?", p.ID).Delete(&model.PipelineEdge{})
for i := range body.Edges {
body.Edges[i].ID = 0
body.Edges[i].PipelineID = p.ID
database.DB.Create(&body.Edges[i])
}
database.DB.Model(&p).Update("updated_at", time.Now())
c.JSON(http.StatusOK, gin.H{"success": true})
}
func setDefault(v *string, def string) {
if *v == "" {
*v = def
}
}