mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 05:32:00 +08:00
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:
parent
9cd5674876
commit
315933d57a
@ -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
|
||||||
|
@ -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"
|
||||||
|
@ -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()})
|
||||||
|
@ -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);
|
|
@ -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('创建模型类型失败')
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 执行数据库迁移
|
||||||
|
echo "执行数据库迁移..."
|
||||||
|
./migrate
|
||||||
|
|
||||||
# 启动后端服务
|
# 启动后端服务
|
||||||
echo "启动后端服务..."
|
echo "启动后端服务..."
|
||||||
./main &
|
./main &
|
||||||
|
Loading…
x
Reference in New Issue
Block a user