refactor: 重构项目结构和文件组织

- 将文档文件移动到docs目录下分类存放
- 将测试文件移动到tests目录
- 将工具脚本移动到tools目录
- 更新README中的文件路径说明
- 删除重复和过时的文件
- 优化项目目录结构,提升可维护性
This commit is contained in:
ben
2025-08-22 16:52:17 +08:00
parent 51576ebb6f
commit 76306f2c8e
48 changed files with 4439 additions and 146 deletions

View File

@@ -0,0 +1,138 @@
// 查询术数书内容的脚本
// 通过 Hyperdrive API 查询 NeonDB 中的术数书数据
const API_BASE_URL = 'https://hyperdrive.seekkey.tech';
// 通用请求函数
async function apiRequest(endpoint, options = {}) {
const url = `${API_BASE_URL}${endpoint}`;
const headers = {
'Content-Type': 'application/json',
...options.headers
};
try {
const response = await fetch(url, {
...options,
headers
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return await response.json();
} else {
return await response.text();
}
} catch (error) {
console.error(`Request failed for ${endpoint}:`, error.message);
throw error;
}
}
// 查询数据库表结构
async function queryTables() {
console.log('\n📋 查询数据库表结构...');
try {
const result = await apiRequest('/query-tables');
console.log('✅ 数据库表:', result);
return result;
} catch (error) {
console.log('❌ 查询表结构失败:', error.message);
return null;
}
}
// 查询术数书内容
async function queryShushuBook(limit = 10) {
console.log('\n📚 查询术数书内容...');
try {
const result = await apiRequest(`/query-shushu?limit=${limit}`);
console.log('✅ 术数书内容:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 查询术数书失败:', error.message);
return null;
}
}
// 搜索术数书内容
async function searchShushuBook(keyword, limit = 5) {
console.log(`\n🔍 搜索术数书内容: "${keyword}"...`);
try {
const result = await apiRequest(`/search-shushu?q=${encodeURIComponent(keyword)}&limit=${limit}`);
console.log('✅ 搜索结果:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 搜索失败:', error.message);
return null;
}
}
// 获取术数书统计信息
async function getShushuStats() {
console.log('\n📊 获取术数书统计信息...');
try {
const result = await apiRequest('/shushu-stats');
console.log('✅ 统计信息:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.log('❌ 获取统计信息失败:', error.message);
return null;
}
}
// 主函数
async function main() {
console.log('🚀 术数书查询脚本');
console.log('==================');
// 首先测试连接
console.log('\n🔗 测试 Hyperdrive 连接...');
try {
const connectionTest = await apiRequest('/test-connection');
console.log('✅ 连接成功:', connectionTest.message);
} catch (error) {
console.log('❌ 连接失败:', error.message);
return;
}
// 查询表结构
await queryTables();
// 获取统计信息
await getShushuStats();
// 查询术数书内容
await queryShushuBook(5);
// 搜索示例
await searchShushuBook('易经');
await searchShushuBook('八卦');
await searchShushuBook('太公');
}
// 如果是 Node.js 环境,导入 fetch
if (typeof window === 'undefined') {
// Node.js 环境
const { default: fetch } = require('node-fetch');
global.fetch = fetch;
main().catch(console.error);
} else {
// 浏览器环境
console.log('在浏览器控制台中运行: main()');
}
// 导出函数供其他模块使用
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
queryTables,
queryShushuBook,
searchShushuBook,
getShushuStats,
main
};
}

View File

@@ -0,0 +1,107 @@
// Simple configuration validation script
// This validates the wrangler.toml and Worker code without requiring API access
const fs = require('fs');
const path = require('path');
console.log('🔍 Validating Hyperdrive Configuration Files');
console.log('============================================');
// Check wrangler.toml
console.log('\n📋 Checking wrangler.toml...');
try {
const wranglerContent = fs.readFileSync('wrangler.toml', 'utf8');
console.log('✅ wrangler.toml exists');
// Check for required fields
const checks = [
{ field: 'name', regex: /name\s*=\s*["']([^"']+)["']/, required: true },
{ field: 'main', regex: /main\s*=\s*["']([^"']+)["']/, required: true },
{ field: 'compatibility_date', regex: /compatibility_date\s*=\s*["']([^"']+)["']/, required: true },
{ field: 'nodejs_compat', regex: /nodejs_compat/, required: true },
{ field: 'hyperdrive binding', regex: /binding\s*=\s*["']HYPERDRIVE["']/, required: true },
{ field: 'hyperdrive id', regex: /id\s*=\s*["']ef43924d89064cddabfaccf06aadfab6["']/, required: true }
];
checks.forEach(check => {
if (check.regex.test(wranglerContent)) {
console.log(`${check.field} configured`);
} else {
console.log(`${check.field} missing or incorrect`);
}
});
} catch (error) {
console.log('❌ wrangler.toml not found or unreadable');
}
// Check Worker code
console.log('\n📝 Checking Worker code...');
try {
const workerContent = fs.readFileSync('src/index.ts', 'utf8');
console.log('✅ src/index.ts exists');
const codeChecks = [
{ name: 'Hyperdrive binding usage', regex: /env\.HYPERDRIVE/ },
{ name: 'Test connection endpoint', regex: /\/test-connection/ },
{ name: 'Test query endpoint', regex: /\/test-query/ },
{ name: 'PostgreSQL import', regex: /pg/ },
{ name: 'Error handling', regex: /try\s*{[\s\S]*catch/ }
];
codeChecks.forEach(check => {
if (check.regex.test(workerContent)) {
console.log(`${check.name} implemented`);
} else {
console.log(` ⚠️ ${check.name} not found`);
}
});
} catch (error) {
console.log('❌ src/index.ts not found or unreadable');
}
// Check package.json
console.log('\n📦 Checking package.json...');
try {
const packageContent = fs.readFileSync('package.json', 'utf8');
const packageJson = JSON.parse(packageContent);
console.log('✅ package.json exists and is valid JSON');
const deps = {
'pg': packageJson.dependencies?.pg,
'@cloudflare/workers-types': packageJson.devDependencies?.['@cloudflare/workers-types'],
'@types/pg': packageJson.devDependencies?.['@types/pg'],
'typescript': packageJson.devDependencies?.typescript,
'wrangler': packageJson.devDependencies?.wrangler
};
Object.entries(deps).forEach(([dep, version]) => {
if (version) {
console.log(`${dep}: ${version}`);
} else {
console.log(`${dep}: not found`);
}
});
} catch (error) {
console.log('❌ package.json not found or invalid JSON');
}
console.log('\n📊 Configuration Summary:');
console.log(' - Project: hyperdrive-neondb-test');
console.log(' - Hyperdrive ID: ef43924d89064cddabfaccf06aadfab6');
console.log(' - Database: NeonDB (PostgreSQL)');
console.log(' - Binding: HYPERDRIVE');
console.log(' - Compatibility: nodejs_compat enabled');
console.log('\n🚀 Next Steps:');
console.log(' 1. Ensure you have proper Cloudflare API permissions');
console.log(' 2. Verify the Hyperdrive configuration exists in your Cloudflare dashboard');
console.log(' 3. Deploy with: wrangler deploy');
console.log(' 4. Test endpoints after deployment');
console.log('\n💡 Troubleshooting:');
console.log(' - If API token has insufficient permissions, use: wrangler login');
console.log(' - Check Hyperdrive exists: https://dash.cloudflare.com/[account-id]/workers/hyperdrive');
console.log(' - Verify NeonDB connection string is correct in Hyperdrive config');

View File

@@ -0,0 +1,16 @@
name = "hyperdrive-neondb-test"
main = "src/index.ts"
compatibility_date = "2025-02-04"
# Add nodejs_compat compatibility flag to support common database drivers
compatibility_flags = ["nodejs_compat"]
[observability]
enabled = true
# Hyperdrive configuration for NeonDB
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "ef43924d89064cddabfaccf06aadfab6"
# For local development, use a local PostgreSQL connection
localConnectionString = "postgresql://postgres:password@localhost:5432/testdb"