|
@@ -3,12 +3,12 @@ const path = require('path');
|
|
const fs = require('fs');
|
|
const fs = require('fs');
|
|
const config = require('../config.json');
|
|
const config = require('../config.json');
|
|
const Logger = require('./Logger');
|
|
const Logger = require('./Logger');
|
|
-const MySQL = require('../plugin/MySQL');
|
|
|
|
|
|
+const MySQL = require('../plugin/DataBase/MySQL');
|
|
|
|
|
|
class SERVER {
|
|
class SERVER {
|
|
constructor() {
|
|
constructor() {
|
|
this.app = express();
|
|
this.app = express();
|
|
- this.port = config.port || 3000;
|
|
|
|
|
|
+ this.port = config.port || 3000;
|
|
this.apiDirectory = path.join(__dirname, '../apis'); // API 文件存放目录
|
|
this.apiDirectory = path.join(__dirname, '../apis'); // API 文件存放目录
|
|
|
|
|
|
this.logger = new Logger(path.join(__dirname, '../logs/Server.log'), 'INFO');
|
|
this.logger = new Logger(path.join(__dirname, '../logs/Server.log'), 'INFO');
|
|
@@ -17,7 +17,7 @@ class SERVER {
|
|
this.app.use(express.json());
|
|
this.app.use(express.json());
|
|
|
|
|
|
// 初始化数据库连接
|
|
// 初始化数据库连接
|
|
- this.db = new MySQL();
|
|
|
|
|
|
+ this.db = new MySQL();
|
|
|
|
|
|
// 加载 API 路由
|
|
// 加载 API 路由
|
|
this.loadAPIs(this.apiDirectory);
|
|
this.loadAPIs(this.apiDirectory);
|
|
@@ -26,17 +26,16 @@ class SERVER {
|
|
// 测试数据库连接
|
|
// 测试数据库连接
|
|
async initDB() {
|
|
async initDB() {
|
|
try {
|
|
try {
|
|
|
|
+ this.logger.info('正在测试数据库连接');
|
|
await this.db.connect();
|
|
await this.db.connect();
|
|
- this.logger.info('数据库连接成功');
|
|
|
|
|
|
+ await this.db.close();
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- this.logger.error(`数据库连接失败: ${error.message}`);
|
|
|
|
|
|
+ this.logger.error(`数据库连接失败: ${error.stack}`);
|
|
process.exit(1);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
loadAPIs(directory) {
|
|
loadAPIs(directory) {
|
|
- this.logger.info('==============正在加载API==============');
|
|
|
|
-
|
|
|
|
const items = fs.readdirSync(directory);
|
|
const items = fs.readdirSync(directory);
|
|
|
|
|
|
items.forEach(item => {
|
|
items.forEach(item => {
|
|
@@ -51,8 +50,6 @@ class SERVER {
|
|
this.loadAPIFile(itemPath);
|
|
this.loadAPIFile(itemPath);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
-
|
|
|
|
- this.logger.info('==============API加载完成==============');
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// 加载单个 API 文件
|
|
// 加载单个 API 文件
|
|
@@ -69,7 +66,7 @@ class SERVER {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- this.logger.error(`加载API文件失败: ${filePath},错误: ${error.message}`);
|
|
|
|
|
|
+ this.logger.error(`加载API文件失败: ${filePath},错误: ${error.stack}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -79,7 +76,7 @@ class SERVER {
|
|
// 初始化数据库连接
|
|
// 初始化数据库连接
|
|
this.initDB().then(() => {
|
|
this.initDB().then(() => {
|
|
this.app.listen(this.port, () => {
|
|
this.app.listen(this.port, () => {
|
|
- this.logger.info(`==========服务器正在 ${this.port} 上运行==========`);
|
|
|
|
|
|
+ this.logger.info(`==========服务器正在 ${this.port} 端口上运行==========`);
|
|
});
|
|
});
|
|
}).catch(err => {
|
|
}).catch(err => {
|
|
this.logger.error(`启动服务器失败: ${err.message}`);
|
|
this.logger.error(`启动服务器失败: ${err.message}`);
|