u
This commit is contained in:
@@ -9,7 +9,7 @@ import cookieParser from 'cookie-parser';
|
||||
import { loggerMiddleware } from './middleware/logger.js';
|
||||
import env from '../config/env.js';
|
||||
import { authMiddleware } from './middleware/auth.js';
|
||||
import { ensureConnection } from '../db/store.js';
|
||||
import { ensureConnection, getSchemaVersion, closeStore } from '../db/store.js';
|
||||
import { closeAll as closeAllSse } from './sse.js';
|
||||
import { closeAllManagers } from '../services/conversationManager.js';
|
||||
import { shutdownImageCropWorkers } from '../services/imageCrop.js';
|
||||
@@ -29,6 +29,7 @@ const __dirname = path.dirname(__filename);
|
||||
|
||||
async function registerRoutes(app, basePath = '') {
|
||||
const routesDir = path.join(__dirname, 'routes');
|
||||
let loadedCount = 0;
|
||||
|
||||
async function registerRouteFilesRecursively(dir) {
|
||||
for (const file of fs.readdirSync(dir)) {
|
||||
@@ -41,12 +42,14 @@ async function registerRoutes(app, basePath = '') {
|
||||
const routeModule = await import(pathToFileURL(fullPath).href);
|
||||
if (routeModule.register) {
|
||||
routeModule.register(app, basePath);
|
||||
loadedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await registerRouteFilesRecursively(routesDir);
|
||||
return loadedCount;
|
||||
}
|
||||
|
||||
let server;
|
||||
@@ -160,7 +163,7 @@ export async function buildApiApp() {
|
||||
});
|
||||
}
|
||||
|
||||
await registerRoutes(app, '/api');
|
||||
app.routeCount = await registerRoutes(app, '/api');
|
||||
app.use('/v1', v1Router);
|
||||
app.use('/api/v1', v1Router);
|
||||
|
||||
@@ -304,3 +307,48 @@ export async function stopApi() {
|
||||
}
|
||||
await Promise.all(closers);
|
||||
}
|
||||
|
||||
export async function runSelfTest() {
|
||||
const start = Date.now();
|
||||
apiLog.info('Starting dashback self-test (loading modules without binding ports)...');
|
||||
|
||||
// 1. Check configuration
|
||||
apiLog.info(`Config: host=${env.host} port=${env.port} dbProvider=${env.dbProvider}`);
|
||||
|
||||
// 2. Test database & store initialization
|
||||
try {
|
||||
await ensureConnection();
|
||||
const schemaVer = await getSchemaVersion();
|
||||
apiLog.info(`Database & store: OK (provider: ${env.dbProvider}, schema v${schemaVer})`);
|
||||
} catch (err) {
|
||||
apiLog.warn(`Database connection warning: ${err.message}`);
|
||||
}
|
||||
|
||||
// 3. Build API app and register all route modules
|
||||
const app = await buildApiApp();
|
||||
apiLog.info(`Routes & middleware: OK (${app.routeCount || 'all'} route modules loaded)`);
|
||||
|
||||
// 4. Test key native/WASM dependencies
|
||||
try {
|
||||
const { optimizeImage } = await import('wasm-image-optimization');
|
||||
const tinyPng = Buffer.from(
|
||||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
|
||||
'base64'
|
||||
);
|
||||
await optimizeImage({ image: tinyPng, format: 'png' });
|
||||
apiLog.info('WASM image pipeline: OK');
|
||||
} catch (err) {
|
||||
apiLog.warn(`WASM image pipeline warning: ${err.message}`);
|
||||
}
|
||||
|
||||
// 5. Clean up store connection
|
||||
try {
|
||||
await closeStore();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
const duration = Date.now() - start;
|
||||
apiLog.info(`Self-test PASSED: All modules and routes loaded successfully in ${duration}ms (no ports bound).`);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user