feat(s2a): Add S2A configuration profiles management system
- Add new `/api/s2a/profiles` endpoint supporting GET, POST, DELETE operations - Create `s2a_profiles` database table with fields for API configuration, concurrency, priority, group IDs, and proxy settings - Implement database methods: `GetS2AProfiles()`, `AddS2AProfile()`, `DeleteS2AProfile()` - Add S2AProfile struct to database models with JSON serialization support - Implement profile management UI in S2AConfig page with save, load, and delete functionality - Add profile list display with ability to apply saved configurations - Add S2AProfile type definition to frontend types - Enable users to save and reuse S2A configurations as presets for faster setup
This commit is contained in:
@@ -123,6 +123,7 @@ func startServer(cfg *config.Config) {
|
||||
mux.HandleFunc("/api/s2a/proxy/", api.CORS(handleS2AProxy)) // 通配代理
|
||||
mux.HandleFunc("/api/s2a/clean-errors", api.CORS(api.HandleCleanErrorAccounts)) // 清理错误账号
|
||||
mux.HandleFunc("/api/s2a/cleaner/settings", api.CORS(handleCleanerSettings)) // 清理服务设置
|
||||
mux.HandleFunc("/api/s2a/profiles", api.CORS(handleS2AProfiles)) // S2A 配置预设管理
|
||||
|
||||
// 统计 API
|
||||
mux.HandleFunc("/api/stats/max-rpm", api.CORS(api.HandleGetMaxRPM)) // 今日最高 RPM
|
||||
@@ -1138,6 +1139,62 @@ func handleCleanerSettings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// handleS2AProfiles GET/POST/DELETE /api/s2a/profiles
|
||||
func handleS2AProfiles(w http.ResponseWriter, r *http.Request) {
|
||||
if database.Instance == nil {
|
||||
api.Error(w, http.StatusInternalServerError, "数据库未初始化")
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
profiles, err := database.Instance.GetS2AProfiles()
|
||||
if err != nil {
|
||||
api.Error(w, http.StatusInternalServerError, fmt.Sprintf("获取配置预设失败: %v", err))
|
||||
return
|
||||
}
|
||||
// 确保 group_ids 解析为 JSON
|
||||
// (数据库层返回的是 JSON string, 这里前端可以直接用,或者我们在 Go 里转一下)
|
||||
// 由于 database.S2AProfile 定义 GroupIDs 为 string,我们直接返回即可,前端解析
|
||||
api.Success(w, profiles)
|
||||
|
||||
case http.MethodPost:
|
||||
var p database.S2AProfile
|
||||
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
||||
api.Error(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
if p.Name == "" {
|
||||
api.Error(w, http.StatusBadRequest, "配置名称不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := database.Instance.AddS2AProfile(p)
|
||||
if err != nil {
|
||||
api.Error(w, http.StatusInternalServerError, fmt.Sprintf("保存配置预设失败: %v", err))
|
||||
return
|
||||
}
|
||||
p.ID = id
|
||||
api.Success(w, p)
|
||||
|
||||
case http.MethodDelete:
|
||||
idStr := r.URL.Query().Get("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
api.Error(w, http.StatusBadRequest, "无效的 ID")
|
||||
return
|
||||
}
|
||||
if err := database.Instance.DeleteS2AProfile(id); err != nil {
|
||||
api.Error(w, http.StatusInternalServerError, fmt.Sprintf("删除配置预设失败: %v", err))
|
||||
return
|
||||
}
|
||||
api.Success(w, map[string]string{"message": "已删除"})
|
||||
|
||||
default:
|
||||
api.Error(w, http.StatusMethodNotAllowed, "不支持的方法")
|
||||
}
|
||||
}
|
||||
|
||||
// handleProxyTest POST /api/proxy/test - 测试代理连接
|
||||
func handleProxyTest(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
|
||||
@@ -23,6 +23,20 @@ type TeamOwner struct {
|
||||
LastCheckedAt *time.Time `json:"last_checked_at,omitempty"`
|
||||
}
|
||||
|
||||
// S2AProfile S2A 配置预设
|
||||
type S2AProfile struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
APIBase string `json:"api_base"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
Concurrency int `json:"concurrency"`
|
||||
Priority int `json:"priority"`
|
||||
GroupIDs string `json:"group_ids"` // JSON array string
|
||||
ProxyEnabled bool `json:"proxy_enabled"`
|
||||
ProxyAddress string `json:"proxy_address"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// DB 数据库管理器
|
||||
type DB struct {
|
||||
db *sql.DB
|
||||
@@ -161,6 +175,20 @@ func (d *DB) createTables() error {
|
||||
CREATE INDEX IF NOT EXISTS idx_app_logs_timestamp ON app_logs(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_app_logs_module ON app_logs(module);
|
||||
CREATE INDEX IF NOT EXISTS idx_app_logs_level ON app_logs(level);
|
||||
|
||||
-- S2A 配置预设表
|
||||
CREATE TABLE IF NOT EXISTS s2a_profiles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
api_base TEXT NOT NULL,
|
||||
admin_key TEXT NOT NULL,
|
||||
concurrency INTEGER DEFAULT 2,
|
||||
priority INTEGER DEFAULT 0,
|
||||
group_ids TEXT,
|
||||
proxy_enabled INTEGER DEFAULT 0,
|
||||
proxy_address TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`)
|
||||
return err
|
||||
}
|
||||
@@ -1260,3 +1288,51 @@ func (d *DB) Close() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetS2AProfiles 获取所有 S2A 配置预设
|
||||
func (d *DB) GetS2AProfiles() ([]S2AProfile, error) {
|
||||
rows, err := d.db.Query(`
|
||||
SELECT id, name, api_base, admin_key, concurrency, priority, group_ids, proxy_enabled, proxy_address, created_at
|
||||
FROM s2a_profiles
|
||||
ORDER BY created_at DESC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var profiles []S2AProfile
|
||||
for rows.Next() {
|
||||
var p S2AProfile
|
||||
var proxyEnabled int
|
||||
err := rows.Scan(&p.ID, &p.Name, &p.APIBase, &p.AdminKey, &p.Concurrency, &p.Priority, &p.GroupIDs, &proxyEnabled, &p.ProxyAddress, &p.CreatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
p.ProxyEnabled = proxyEnabled == 1
|
||||
profiles = append(profiles, p)
|
||||
}
|
||||
return profiles, nil
|
||||
}
|
||||
|
||||
// AddS2AProfile 添加 S2A 配置预设
|
||||
func (d *DB) AddS2AProfile(p S2AProfile) (int64, error) {
|
||||
proxyEnabled := 0
|
||||
if p.ProxyEnabled {
|
||||
proxyEnabled = 1
|
||||
}
|
||||
result, err := d.db.Exec(`
|
||||
INSERT INTO s2a_profiles (name, api_base, admin_key, concurrency, priority, group_ids, proxy_enabled, proxy_address, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
`, p.Name, p.APIBase, p.AdminKey, p.Concurrency, p.Priority, p.GroupIDs, proxyEnabled, p.ProxyAddress)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
// DeleteS2AProfile 删除 S2A 配置预设
|
||||
func (d *DB) DeleteS2AProfile(id int64) error {
|
||||
_, err := d.db.Exec("DELETE FROM s2a_profiles WHERE id = ?", id)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user