feat: 实现后端基础架构与 VOD 核心 API(Express + SQLite + JWT)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import cors from 'cors';
|
||||
import path from 'path';
|
||||
import pino from 'pino';
|
||||
import { config, validateConfig } from './config';
|
||||
import { getDb } from './db';
|
||||
import { ensureDefaultAdmin } from './services/users';
|
||||
import authRoutes from './routes/auth';
|
||||
import configRoutes from './routes/config';
|
||||
import videoRoutes from './routes/videos';
|
||||
import categoryRoutes from './routes/categories';
|
||||
import logRoutes from './routes/logs';
|
||||
|
||||
const logger = pino({
|
||||
transport: config.nodeEnv === 'development' ? { target: 'pino-pretty' } : undefined,
|
||||
});
|
||||
|
||||
async function main() {
|
||||
validateConfig();
|
||||
await getDb();
|
||||
await ensureDefaultAdmin();
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/accounts', configRoutes);
|
||||
app.use('/api/videos', videoRoutes);
|
||||
app.use('/api/categories', categoryRoutes);
|
||||
app.use('/api/logs', logRoutes);
|
||||
|
||||
// Serve static frontend in production
|
||||
if (config.nodeEnv === 'production') {
|
||||
const staticPath = path.resolve(__dirname, '../../web/dist');
|
||||
app.use(express.static(staticPath));
|
||||
app.get('*', (_req: Request, res: Response) => {
|
||||
res.sendFile(path.join(staticPath, 'index.html'));
|
||||
});
|
||||
}
|
||||
|
||||
app.use((err: Error, _req: Request, res: Response, _next: express.NextFunction) => {
|
||||
logger.error(err);
|
||||
res.status(500).json({ code: 500, message: err.message || 'Internal server error' });
|
||||
});
|
||||
|
||||
app.listen(config.port, () => {
|
||||
logger.info(`Server listening on port ${config.port}`);
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
logger.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user