init
This commit is contained in:
105
serve/scripts/create_test_users.go
Normal file
105
serve/scripts/create_test_users.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/config"
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/database"
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/models"
|
||||
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 加载配置
|
||||
config.LoadConfig()
|
||||
if config.GlobalConfig == nil {
|
||||
log.Fatal("Failed to load configuration")
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
database.InitDatabase()
|
||||
defer database.CloseDatabase()
|
||||
|
||||
db := database.GetDB()
|
||||
|
||||
// 测试用户数据
|
||||
testUsers := []struct {
|
||||
Username string
|
||||
Email string
|
||||
Password string
|
||||
Nickname string
|
||||
}{
|
||||
{
|
||||
Username: "testuser",
|
||||
Email: "test@example.com",
|
||||
Password: "Test@123",
|
||||
Nickname: "测试用户",
|
||||
},
|
||||
{
|
||||
Username: "student1",
|
||||
Email: "student1@example.com",
|
||||
Password: "Student@123",
|
||||
Nickname: "学生一号",
|
||||
},
|
||||
{
|
||||
Username: "student2",
|
||||
Email: "student2@example.com",
|
||||
Password: "Student@123",
|
||||
Nickname: "学生二号",
|
||||
},
|
||||
{
|
||||
Username: "teacher",
|
||||
Email: "teacher@example.com",
|
||||
Password: "Teacher@123",
|
||||
Nickname: "教师账号",
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Println("开始创建测试用户...")
|
||||
|
||||
for _, userData := range testUsers {
|
||||
// 检查用户是否已存在
|
||||
var existingUser models.User
|
||||
result := db.Where("username = ? OR email = ?", userData.Username, userData.Email).First(&existingUser)
|
||||
|
||||
if result.Error == nil {
|
||||
fmt.Printf("用户 %s 已存在,跳过创建\n", userData.Username)
|
||||
continue
|
||||
}
|
||||
|
||||
// 加密密码
|
||||
hashedPassword, err := utils.HashPassword(userData.Password)
|
||||
if err != nil {
|
||||
log.Printf("Failed to hash password for %s: %v", userData.Username, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
user := models.User{
|
||||
Username: userData.Username,
|
||||
Email: userData.Email,
|
||||
PasswordHash: hashedPassword,
|
||||
Nickname: &userData.Nickname,
|
||||
Status: "active",
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
log.Printf("Failed to create user %s: %v", userData.Username, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("✓ 成功创建用户: %s (邮箱: %s, 密码: %s)\n",
|
||||
userData.Username, userData.Email, userData.Password)
|
||||
}
|
||||
|
||||
fmt.Println("\n测试用户创建完成!")
|
||||
fmt.Println("\n可用的测试账号:")
|
||||
fmt.Println("1. 用户名: testuser 邮箱: test@example.com 密码: Test@123")
|
||||
fmt.Println("2. 用户名: student1 邮箱: student1@example.com 密码: Student@123")
|
||||
fmt.Println("3. 用户名: student2 邮箱: student2@example.com 密码: Student@123")
|
||||
fmt.Println("4. 用户名: teacher 邮箱: teacher@example.com 密码: Teacher@123")
|
||||
}
|
||||
39
serve/scripts/test_log_format.bat
Normal file
39
serve/scripts/test_log_format.bat
Normal file
@@ -0,0 +1,39 @@
|
||||
@echo off
|
||||
echo.
|
||||
echo Testing Backend Log Format
|
||||
echo ================================
|
||||
echo.
|
||||
|
||||
set BASE_URL=http://localhost:8080
|
||||
|
||||
echo [1/5] Testing Health Check (GET 200)
|
||||
curl -s "%BASE_URL%/health" >nul 2>&1
|
||||
timeout /t 1 /nobreak >nul
|
||||
|
||||
echo [2/5] Testing Login (POST 200)
|
||||
curl -s -X POST "%BASE_URL%/api/v1/auth/login" -H "Content-Type: application/json" -d "{\"account\":\"test@example.com\",\"password\":\"Test@123\"}" >nul 2>&1
|
||||
timeout /t 1 /nobreak >nul
|
||||
|
||||
echo [3/5] Testing 404 Error (GET 404)
|
||||
curl -s "%BASE_URL%/api/v1/not-found" >nul 2>&1
|
||||
timeout /t 1 /nobreak >nul
|
||||
|
||||
echo [4/5] Testing OPTIONS Preflight (OPTIONS 204)
|
||||
curl -s -X OPTIONS "%BASE_URL%/api/v1/user/profile" -H "Origin: http://localhost:3001" >nul 2>&1
|
||||
timeout /t 1 /nobreak >nul
|
||||
|
||||
echo [5/5] Testing Unauthorized (GET 401)
|
||||
curl -s "%BASE_URL%/api/v1/user/profile" >nul 2>&1
|
||||
timeout /t 1 /nobreak >nul
|
||||
|
||||
echo.
|
||||
echo Test Complete!
|
||||
echo.
|
||||
echo View logs:
|
||||
echo type logs\app.log
|
||||
echo.
|
||||
echo View last 20 lines:
|
||||
echo powershell "Get-Content logs\app.log -Tail 20"
|
||||
echo.
|
||||
|
||||
pause
|
||||
41
serve/scripts/test_log_format.sh
Normal file
41
serve/scripts/test_log_format.sh
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 测试日志格式脚本
|
||||
# 用于演示新的emoji日志格式
|
||||
|
||||
echo "🧪 测试后端日志格式"
|
||||
echo "================================"
|
||||
echo ""
|
||||
|
||||
BASE_URL="http://localhost:8080"
|
||||
|
||||
echo "1️⃣ 测试健康检查 (GET 200)"
|
||||
curl -s "$BASE_URL/health" > /dev/null
|
||||
sleep 1
|
||||
|
||||
echo "2️⃣ 测试登录 (POST 200)"
|
||||
curl -s -X POST "$BASE_URL/api/v1/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"account":"test@example.com","password":"Test@123"}' > /dev/null
|
||||
sleep 1
|
||||
|
||||
echo "3️⃣ 测试404错误 (GET 404)"
|
||||
curl -s "$BASE_URL/api/v1/not-found" > /dev/null
|
||||
sleep 1
|
||||
|
||||
echo "4️⃣ 测试OPTIONS预检 (OPTIONS 204)"
|
||||
curl -s -X OPTIONS "$BASE_URL/api/v1/user/profile" \
|
||||
-H "Origin: http://localhost:3001" > /dev/null
|
||||
sleep 1
|
||||
|
||||
echo "5️⃣ 测试未授权 (GET 401)"
|
||||
curl -s "$BASE_URL/api/v1/user/profile" > /dev/null
|
||||
sleep 1
|
||||
|
||||
echo ""
|
||||
echo "✅ 测试完成!"
|
||||
echo "📋 查看日志:"
|
||||
echo " tail -20 logs/app.log"
|
||||
echo ""
|
||||
echo "🎨 格式化查看(需要安装jq):"
|
||||
echo " tail -20 logs/app.log | jq '.'"
|
||||
44
serve/scripts/test_new_log.ps1
Normal file
44
serve/scripts/test_new_log.ps1
Normal file
@@ -0,0 +1,44 @@
|
||||
# 测试新的日志格式
|
||||
|
||||
Write-Host "`n=== Testing New Log Format ===`n" -ForegroundColor Cyan
|
||||
|
||||
$baseUrl = "http://localhost:8080"
|
||||
|
||||
# 1. 测试健康检查
|
||||
Write-Host "[1/4] Testing Health Check..." -ForegroundColor Yellow
|
||||
Invoke-WebRequest -Uri "$baseUrl/health" -UseBasicParsing | Out-Null
|
||||
Start-Sleep -Seconds 1
|
||||
|
||||
# 2. 测试登录
|
||||
Write-Host "[2/4] Testing Login..." -ForegroundColor Yellow
|
||||
$loginBody = @{
|
||||
account = "test@example.com"
|
||||
password = "Test@123"
|
||||
} | ConvertTo-Json
|
||||
|
||||
Invoke-WebRequest -Uri "$baseUrl/api/v1/auth/login" `
|
||||
-Method POST `
|
||||
-Body $loginBody `
|
||||
-ContentType "application/json" `
|
||||
-UseBasicParsing | Out-Null
|
||||
Start-Sleep -Seconds 1
|
||||
|
||||
# 3. 测试404
|
||||
Write-Host "[3/4] Testing 404 Error..." -ForegroundColor Yellow
|
||||
try {
|
||||
Invoke-WebRequest -Uri "$baseUrl/api/v1/not-found" -UseBasicParsing | Out-Null
|
||||
} catch {
|
||||
# 忽略404错误
|
||||
}
|
||||
Start-Sleep -Seconds 1
|
||||
|
||||
# 4. 测试未授权
|
||||
Write-Host "[4/4] Testing Unauthorized..." -ForegroundColor Yellow
|
||||
try {
|
||||
Invoke-WebRequest -Uri "$baseUrl/api/v1/user/profile" -UseBasicParsing | Out-Null
|
||||
} catch {
|
||||
# 忽略401错误
|
||||
}
|
||||
|
||||
Write-Host "`n=== Test Complete! ===`n" -ForegroundColor Green
|
||||
Write-Host "Check the server console for beautiful logs!" -ForegroundColor Cyan
|
||||
Reference in New Issue
Block a user