Refactor model type handling and add SQLite support

- Update config to include SQLite path for database migration
- Modify model type handlers to use new database schema
- Update frontend to handle new model type key and label fields
- Add database migration script to start process
- Import SQLite driver for potential database usage
This commit is contained in:
wood chen 2025-02-23 04:23:42 +08:00
parent 9cd5674876
commit 315933d57a
6 changed files with 29 additions and 32 deletions

View File

@ -18,6 +18,9 @@ type Config struct {
// 其他配置 // 其他配置
ServerPort string ServerPort string
// SQLite配置用于数据迁移
SQLitePath string
} }
func LoadConfig() (*Config, error) { func LoadConfig() (*Config, error) {
@ -47,6 +50,9 @@ func LoadConfig() (*Config, error) {
// 其他配置 // 其他配置
ServerPort: getEnv("PORT", "8080"), ServerPort: getEnv("PORT", "8080"),
// SQLite路径用于数据迁移
SQLitePath: filepath.Join(dbDir, "aimodels.db"),
} }
return config, nil return config, nil

View File

@ -6,6 +6,7 @@ import (
"log" "log"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "modernc.org/sqlite"
"aimodels-prices/config" "aimodels-prices/config"
"aimodels-prices/models" "aimodels-prices/models"

View File

@ -4,29 +4,25 @@ import (
"database/sql" "database/sql"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
)
// ModelType 模型类型结构 "aimodels-prices/models"
type ModelType struct { )
Key string `json:"key"`
Label string `json:"label"`
}
// GetModelTypes 获取所有模型类型 // GetModelTypes 获取所有模型类型
func GetModelTypes(c *gin.Context) { func GetModelTypes(c *gin.Context) {
db := c.MustGet("db").(*sql.DB) db := c.MustGet("db").(*sql.DB)
rows, err := db.Query("SELECT key, label FROM model_type") rows, err := db.Query("SELECT type_key, type_label FROM model_type")
if err != nil { if err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }
defer rows.Close() defer rows.Close()
var types []ModelType var types []models.ModelType
for rows.Next() { for rows.Next() {
var t ModelType var t models.ModelType
if err := rows.Scan(&t.Key, &t.Label); err != nil { if err := rows.Scan(&t.TypeKey, &t.TypeLabel); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }
@ -40,17 +36,17 @@ func GetModelTypes(c *gin.Context) {
func CreateModelType(c *gin.Context) { func CreateModelType(c *gin.Context) {
db := c.MustGet("db").(*sql.DB) db := c.MustGet("db").(*sql.DB)
var newType ModelType var newType models.ModelType
if err := c.ShouldBindJSON(&newType); err != nil { if err := c.ShouldBindJSON(&newType); err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
_, err := db.Exec(` _, err := db.Exec(`
INSERT INTO model_type (key, label) INSERT INTO model_type (type_key, type_label)
VALUES (?, ?) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET label = excluded.label ON DUPLICATE KEY UPDATE type_label = VALUES(type_label)
`, newType.Key, newType.Label) `, newType.TypeKey, newType.TypeLabel)
if err != nil { if err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})

View File

@ -1,10 +0,0 @@
-- 初始化模型类型数据
INSERT INTO model_type (type_key, type_label) VALUES
('text2text', '文生文'),
('text2image', '文生图'),
('text2speech', '文生音'),
('speech2text', '音生文'),
('image2text', '图生文'),
('embedding', '向量'),
('other', '其他')
ON DUPLICATE KEY UPDATE type_label = VALUES(type_label);

View File

@ -777,7 +777,7 @@ const loadModelTypes = async () => {
const types = response.data const types = response.data
const map = {} const map = {}
types.forEach(type => { types.forEach(type => {
map[type.key] = type.label map[type.type_key] = type.type_label
}) })
modelTypeMap.value = map modelTypeMap.value = map
} catch (error) { } catch (error) {
@ -795,18 +795,18 @@ const handleModelTypeCreate = async (value) => {
} }
// key使 // key使
let key = value let type_key = value
let label = value let type_label = value
if (!/^[a-zA-Z0-9_]+$/.test(value)) { if (!/^[a-zA-Z0-9_]+$/.test(value)) {
// key // key
key = `type_${Date.now()}` type_key = `type_${Date.now()}`
label = value type_label = value
} }
try { try {
await axios.post('/api/model-types', { key, label }) await axios.post('/api/model-types', { type_key, type_label })
modelTypeMap.value[key] = label modelTypeMap.value[type_key] = type_label
return key return type_key
} catch (error) { } catch (error) {
console.error('Failed to create model type:', error) console.error('Failed to create model type:', error)
ElMessage.error('创建模型类型失败') ElMessage.error('创建模型类型失败')

View File

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
# 执行数据库迁移
echo "执行数据库迁移..."
./migrate
# 启动后端服务 # 启动后端服务
echo "启动后端服务..." echo "启动后端服务..."
./main & ./main &