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

View File

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

View File

@ -4,29 +4,25 @@ import (
"database/sql"
"github.com/gin-gonic/gin"
)
// ModelType 模型类型结构
type ModelType struct {
Key string `json:"key"`
Label string `json:"label"`
}
"aimodels-prices/models"
)
// GetModelTypes 获取所有模型类型
func GetModelTypes(c *gin.Context) {
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 {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
var types []ModelType
var types []models.ModelType
for rows.Next() {
var t ModelType
if err := rows.Scan(&t.Key, &t.Label); err != nil {
var t models.ModelType
if err := rows.Scan(&t.TypeKey, &t.TypeLabel); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
@ -40,17 +36,17 @@ func GetModelTypes(c *gin.Context) {
func CreateModelType(c *gin.Context) {
db := c.MustGet("db").(*sql.DB)
var newType ModelType
var newType models.ModelType
if err := c.ShouldBindJSON(&newType); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
_, err := db.Exec(`
INSERT INTO model_type (key, label)
INSERT INTO model_type (type_key, type_label)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET label = excluded.label
`, newType.Key, newType.Label)
ON DUPLICATE KEY UPDATE type_label = VALUES(type_label)
`, newType.TypeKey, newType.TypeLabel)
if err != nil {
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 map = {}
types.forEach(type => {
map[type.key] = type.label
map[type.type_key] = type.type_label
})
modelTypeMap.value = map
} catch (error) {
@ -795,18 +795,18 @@ const handleModelTypeCreate = async (value) => {
}
// key使
let key = value
let label = value
let type_key = value
let type_label = value
if (!/^[a-zA-Z0-9_]+$/.test(value)) {
// key
key = `type_${Date.now()}`
label = value
type_key = `type_${Date.now()}`
type_label = value
}
try {
await axios.post('/api/model-types', { key, label })
modelTypeMap.value[key] = label
return key
await axios.post('/api/model-types', { type_key, type_label })
modelTypeMap.value[type_key] = type_label
return type_key
} catch (error) {
console.error('Failed to create model type:', error)
ElMessage.error('创建模型类型失败')

View File

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