This commit is contained in:
2025-07-28 01:46:39 +08:00
parent b03129667a
commit 357639462a
219 changed files with 21634 additions and 8138 deletions

View File

@@ -0,0 +1,157 @@
package main
import (
"encoding/json"
"log"
"tyapi-server/internal/config"
"tyapi-server/internal/domains/product/entities"
"tyapi-server/internal/infrastructure/database"
"github.com/google/uuid"
)
func main() {
// 加载配置
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("加载配置失败: %v", err)
}
// 连接数据库
db, err := database.NewConnection(database.Config{
Host: cfg.Database.Host,
Port: cfg.Database.Port,
User: cfg.Database.User,
Password: cfg.Database.Password,
Name: cfg.Database.Name,
SSLMode: cfg.Database.SSLMode,
Timezone: cfg.Database.Timezone,
MaxOpenConns: cfg.Database.MaxOpenConns,
MaxIdleConns: cfg.Database.MaxIdleConns,
ConnMaxLifetime: cfg.Database.ConnMaxLifetime,
})
if err != nil {
log.Fatalf("连接数据库失败: %v", err)
}
// 获取所有产品
var products []entities.Product
if err := db.Find(&products).Error; err != nil {
log.Fatalf("获取产品失败: %v", err)
}
log.Printf("找到 %d 个产品", len(products))
for _, product := range products {
// 检查是否已有API配置
var count int64
db.Model(&entities.ProductApiConfig{}).Where("product_id = ?", product.ID).Count(&count)
if count > 0 {
log.Printf("产品已有API配置跳过: %s (%s)", product.Name, product.Code)
continue
}
// 创建默认请求参数
requestParams := []entities.RequestParam{
{
Name: "姓名",
Field: "name",
Type: "text",
Required: true,
Description: "用户真实姓名",
Example: "张三",
Validation: "^[\\u4e00-\\u9fa5]{2,4}$",
},
{
Name: "身份证号",
Field: "id_card",
Type: "idcard",
Required: true,
Description: "用户身份证号码",
Example: "110101199001011234",
Validation: "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$",
},
{
Name: "手机号",
Field: "mobile",
Type: "phone",
Required: true,
Description: "用户手机号码",
Example: "13800138000",
Validation: "^1[3-9]\\d{9}$",
},
}
// 创建默认响应字段
responseFields := []entities.ResponseField{
{
Name: "结果",
Path: "result",
Type: "string",
Required: true,
Description: "查询结果",
Example: "success",
},
{
Name: "状态码",
Path: "code",
Type: "number",
Required: true,
Description: "响应状态码",
Example: "200",
},
{
Name: "消息",
Path: "message",
Type: "string",
Required: true,
Description: "响应消息",
Example: "查询成功",
},
{
Name: "数据",
Path: "data",
Type: "object",
Required: false,
Description: "查询结果数据",
Example: "{}",
},
}
// 创建默认响应示例
responseExample := map[string]interface{}{
"result": "success",
"code": 200,
"message": "查询成功",
"data": map[string]interface{}{
"name": "张三",
"id_card": "110101199001011234",
"mobile": "13800138000",
"status": "正常",
"query_time": "2024-01-01 12:00:00",
},
}
// 序列化为JSON
requestParamsJSON, _ := json.Marshal(requestParams)
responseFieldsJSON, _ := json.Marshal(responseFields)
responseExampleJSON, _ := json.Marshal(responseExample)
// 创建API配置
apiConfig := entities.ProductApiConfig{
ID: uuid.New().String(),
ProductID: product.ID,
RequestParams: string(requestParamsJSON),
ResponseFields: string(responseFieldsJSON),
ResponseExample: string(responseExampleJSON),
}
// 保存到数据库
if err := db.Create(&apiConfig).Error; err != nil {
log.Printf("创建产品API配置失败: %s, 错误: %v", product.Name, err)
} else {
log.Printf("成功创建产品API配置: %s (%s)", product.Name, product.Code)
}
}
log.Println("产品API配置初始化完成")
}

67
scripts/test_cache.bat Normal file
View File

@@ -0,0 +1,67 @@
@echo off
REM 缓存测试脚本 (Windows版本)
REM 使用方法: scripts\test_cache.bat [base_url]
REM 默认base_url: http://localhost:8080
set BASE_URL=%1
if "%BASE_URL%"=="" set BASE_URL=http://localhost:8080
set API_BASE=%BASE_URL%/api/cache-test
echo 🧪 开始缓存系统测试...
echo 📍 测试地址: %BASE_URL%
echo.
REM 测试函数
:test_endpoint
set method=%1
set endpoint=%2
set description=%3
echo 🔍 测试 %description%...
if "%method%"=="GET" (
curl -s "%API_BASE%%endpoint%"
) else if "%method%"=="POST" (
curl -s -X POST "%API_BASE%%endpoint%"
) else if "%method%"=="DELETE" (
curl -s -X DELETE "%API_BASE%%endpoint%"
)
if %errorlevel% equ 0 (
echo ✅ 成功
) else (
echo ❌ 失败
)
echo.
goto :eof
REM 1. 测试缓存统计
call :test_endpoint GET /stats "缓存统计"
REM 2. 测试基础缓存操作
call :test_endpoint GET /test "基础缓存操作"
REM 3. 测试缓存键查询
call :test_endpoint GET /keys/gorm_cache:* "缓存键查询"
REM 4. 测试性能测试
call :test_endpoint POST /performance "缓存性能测试"
REM 5. 测试表缓存调试
call :test_endpoint GET /table/users "用户表缓存调试"
echo 🎉 缓存测试完成!
echo.
echo 📋 测试结果说明:
echo - 如果所有测试都返回200状态码说明缓存系统正常工作
echo - 如果某些测试失败,请检查应用是否正在运行
echo - 查看应用日志获取更详细的调试信息
echo.
echo 🔧 手动测试命令:
echo curl %API_BASE%/stats
echo curl %API_BASE%/test
echo curl %API_BASE%/keys/gorm_cache:*
echo curl -X POST %API_BASE%/performance
echo curl %API_BASE%/table/users
pause

79
scripts/test_cache.sh Normal file
View File

@@ -0,0 +1,79 @@
#!/bin/bash
# 缓存测试脚本
# 使用方法: ./scripts/test_cache.sh [base_url]
# 默认base_url: http://localhost:8080
BASE_URL=${1:-http://localhost:8080}
API_BASE="$BASE_URL/api/cache-test"
echo "🧪 开始缓存系统测试..."
echo "📍 测试地址: $BASE_URL"
echo ""
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 测试函数
test_endpoint() {
local method=$1
local endpoint=$2
local description=$3
echo -n "🔍 测试 $description... "
if [ "$method" = "GET" ]; then
response=$(curl -s -w "%{http_code}" "$API_BASE$endpoint")
elif [ "$method" = "POST" ]; then
response=$(curl -s -w "%{http_code}" -X POST "$API_BASE$endpoint")
elif [ "$method" = "DELETE" ]; then
response=$(curl -s -w "%{http_code}" -X DELETE "$API_BASE$endpoint")
fi
# 提取HTTP状态码
http_code="${response: -3}"
# 提取响应体
body="${response%???}"
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}✅ 成功${NC}"
echo " 响应: $body" | head -c 100
echo "..."
else
echo -e "${RED}❌ 失败 (HTTP $http_code)${NC}"
echo " 错误: $body"
fi
echo ""
}
# 1. 测试缓存统计
test_endpoint "GET" "/stats" "缓存统计"
# 2. 测试基础缓存操作
test_endpoint "GET" "/test" "基础缓存操作"
# 3. 测试缓存键查询
test_endpoint "GET" "/keys/gorm_cache:*" "缓存键查询"
# 4. 测试性能测试
test_endpoint "POST" "/performance" "缓存性能测试"
# 5. 测试表缓存调试
test_endpoint "GET" "/table/users" "用户表缓存调试"
echo "🎉 缓存测试完成!"
echo ""
echo "📋 测试结果说明:"
echo " - 如果所有测试都返回200状态码说明缓存系统正常工作"
echo " - 如果某些测试失败,请检查应用是否正在运行"
echo " - 查看应用日志获取更详细的调试信息"
echo ""
echo "🔧 手动测试命令:"
echo " curl $API_BASE/stats"
echo " curl $API_BASE/test"
echo " curl $API_BASE/keys/gorm_cache:*"
echo " curl -X POST $API_BASE/performance"
echo " curl $API_BASE/table/users"