功能: - 支持配置 MySQL/MongoDB 可见数据库列表 - 连接删除时自动清理关联数据并关闭连接池 - 新增加载数据库列表 API - 数据库错误提示优化 改进: - 代码简化:消除重复的表单验证和密码处理逻辑 - ResultPanel 表格高度计算重构 - 删除调试日志和临时文件 后端: - 新增 VisibleDatabases 字段到连接模型 - DeleteConnection 使用事务确保数据一致性 - LoadAllDatabases 支持 MySQL/MongoDB 数据库列表加载
143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package api
|
||
|
||
import (
|
||
"u-desk/internal/storage"
|
||
"u-desk/internal/storage/models"
|
||
)
|
||
|
||
// ConnectionAPI 连接管理API
|
||
type ConnectionAPI struct {
|
||
connService *storage.ConnectionService
|
||
}
|
||
|
||
// NewConnectionAPI 创建连接管理API
|
||
func NewConnectionAPI() (*ConnectionAPI, error) {
|
||
connService, err := storage.NewConnectionService()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &ConnectionAPI{connService}, nil
|
||
}
|
||
|
||
// SaveConnectionRequest 保存连接请求结构体
|
||
type SaveConnectionRequest struct {
|
||
ID uint `json:"id"`
|
||
Name string `json:"name"`
|
||
Type string `json:"type"`
|
||
Host string `json:"host"`
|
||
Port int `json:"port"`
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
Database string `json:"database"`
|
||
Options string `json:"options"`
|
||
VisibleDatabases string `json:"visible_databases"`
|
||
}
|
||
|
||
// SaveDbConnection 保存数据库连接配置
|
||
func (api *ConnectionAPI) SaveDbConnection(req SaveConnectionRequest) error {
|
||
conn := &models.DbConnection{
|
||
ID: req.ID,
|
||
Name: req.Name,
|
||
Type: req.Type,
|
||
Host: req.Host,
|
||
Port: req.Port,
|
||
Username: req.Username,
|
||
Password: req.Password,
|
||
Database: req.Database,
|
||
Options: req.Options,
|
||
VisibleDatabases: req.VisibleDatabases,
|
||
}
|
||
return api.connService.SaveConnection(conn)
|
||
}
|
||
|
||
// ListDbConnections 获取连接列表
|
||
func (api *ConnectionAPI) ListDbConnections() ([]map[string]interface{}, error) {
|
||
connections, err := api.connService.ListConnections()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
result := make([]map[string]interface{}, len(connections))
|
||
timeFormat := "2006-01-02 15:04:05"
|
||
for i, conn := range connections {
|
||
result[i] = map[string]interface{}{
|
||
"id": conn.ID,
|
||
"name": conn.Name,
|
||
"type": conn.Type,
|
||
"host": conn.Host,
|
||
"port": conn.Port,
|
||
"username": conn.Username,
|
||
"database": conn.Database,
|
||
"options": conn.Options,
|
||
"visible_databases": conn.VisibleDatabases,
|
||
"created_at": conn.CreatedAt.Format(timeFormat),
|
||
"updated_at": conn.UpdatedAt.Format(timeFormat),
|
||
}
|
||
}
|
||
return result, nil
|
||
}
|
||
|
||
func (api *ConnectionAPI) DeleteDbConnection(id uint) error {
|
||
return api.connService.DeleteConnection(id)
|
||
}
|
||
|
||
func (api *ConnectionAPI) TestDbConnection(id uint) error {
|
||
conn, err := api.connService.GetConnection(id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return api.connService.TestConnection(conn)
|
||
}
|
||
|
||
// TestConnectionRequest 测试连接请求结构体(不保存数据)
|
||
type TestConnectionRequest struct {
|
||
ID uint `json:"id"` // 编辑模式下的连接ID(用于获取已保存的密码)
|
||
Type string `json:"type"`
|
||
Host string `json:"host"`
|
||
Port int `json:"port"`
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
Database string `json:"database"`
|
||
Options string `json:"options"`
|
||
}
|
||
|
||
// TestDbConnectionWithParams 测试数据库连接(直接传入参数,不保存数据)
|
||
func (api *ConnectionAPI) TestDbConnectionWithParams(req TestConnectionRequest) error {
|
||
return api.connService.TestConnectionWithParams(
|
||
req.Type,
|
||
req.Host,
|
||
req.Port,
|
||
req.Username,
|
||
req.Password,
|
||
req.Database,
|
||
req.Options,
|
||
req.ID,
|
||
)
|
||
}
|
||
|
||
// LoadAllDatabasesRequest 加载全部数据库请求结构体
|
||
type LoadAllDatabasesRequest struct {
|
||
ID uint `json:"id"`
|
||
Type string `json:"type"`
|
||
Host string `json:"host"`
|
||
Port int `json:"port"`
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
Database string `json:"database"`
|
||
Options string `json:"options"`
|
||
}
|
||
|
||
// LoadAllDatabases 加载全部数据库列表
|
||
func (api *ConnectionAPI) LoadAllDatabases(req LoadAllDatabasesRequest) ([]string, error) {
|
||
return api.connService.LoadAllDatabases(
|
||
req.Type,
|
||
req.Host,
|
||
req.Port,
|
||
req.Username,
|
||
req.Password,
|
||
req.Database,
|
||
req.Options,
|
||
req.ID,
|
||
)
|
||
}
|