feat: initialize project skeleton with Go backend, database, and config
- Go module with standard library + gorilla/sessions + mysql driver - Config: env-based configuration with .env file support - Database: MySQL connection with auto-migration for 6 tables - Seed data: 12 system tags with options, 8 system snippets - Auth: session cookie middleware - Handlers: auth, tags, snippets, builder, claude, dashboard, suggestions, settings - LLM: OpenAI-compatible client with 30s timeout - Docker: Dockerfile + docker-compose.yml - .env.example with all configuration options Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"prompt-generator/internal/db"
|
||||
"prompt-generator/internal/models"
|
||||
)
|
||||
|
||||
func GetSnippets(w http.ResponseWriter, r *http.Request) {
|
||||
category := r.URL.Query().Get("category")
|
||||
|
||||
query := "SELECT id, name, content, category, scope, created_at FROM snippets"
|
||||
var args []interface{}
|
||||
if category != "" {
|
||||
query += " WHERE category=?"
|
||||
args = append(args, category)
|
||||
}
|
||||
query += " ORDER BY scope, category, id"
|
||||
|
||||
rows, err := db.DB.Query(query, args...)
|
||||
if err != nil {
|
||||
fail(w, 500, "查询片段失败")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var snippets []models.Snippet
|
||||
for rows.Next() {
|
||||
var s models.Snippet
|
||||
if err := rows.Scan(&s.ID, &s.Name, &s.Content, &s.Category, &s.Scope, &s.CreatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
snippets = append(snippets, s)
|
||||
}
|
||||
if snippets == nil {
|
||||
snippets = []models.Snippet{}
|
||||
}
|
||||
success(w, snippets)
|
||||
}
|
||||
|
||||
func CreateSnippet(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Name == "" {
|
||||
fail(w, 400, "片段名称不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
res, err := db.DB.Exec("INSERT INTO snippets (name, content, category, scope) VALUES (?, ?, ?, 'personal')",
|
||||
req.Name, req.Content, req.Category)
|
||||
if err != nil {
|
||||
fail(w, 500, "创建片段失败")
|
||||
return
|
||||
}
|
||||
|
||||
id, _ := res.LastInsertId()
|
||||
success(w, map[string]int64{"id": id})
|
||||
}
|
||||
|
||||
func UpdateSnippet(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
fail(w, 400, "无效的 ID")
|
||||
return
|
||||
}
|
||||
|
||||
var scope string
|
||||
db.DB.QueryRow("SELECT scope FROM snippets WHERE id=?", id).Scan(&scope)
|
||||
if scope != "personal" {
|
||||
fail(w, 403, "不能修改系统片段")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
fail(w, 400, "请求格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec("UPDATE snippets SET name=?, content=?, category=? WHERE id=?",
|
||||
req.Name, req.Content, req.Category, id)
|
||||
if err != nil {
|
||||
fail(w, 500, "更新片段失败")
|
||||
return
|
||||
}
|
||||
success(w, nil)
|
||||
}
|
||||
|
||||
func DeleteSnippet(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
fail(w, 400, "无效的 ID")
|
||||
return
|
||||
}
|
||||
|
||||
var scope string
|
||||
db.DB.QueryRow("SELECT scope FROM snippets WHERE id=?", id).Scan(&scope)
|
||||
if scope != "personal" {
|
||||
fail(w, 403, "不能删除系统片段")
|
||||
return
|
||||
}
|
||||
|
||||
db.DB.Exec("DELETE FROM snippets WHERE id=?", id)
|
||||
success(w, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user