|
@@ -6,17 +6,29 @@ const fs = require('fs');
|
|
const md5 = require('md5.js');
|
|
const md5 = require('md5.js');
|
|
|
|
|
|
async function initDatabase() {
|
|
async function initDatabase() {
|
|
- const connection = await mysql2.createConnection({
|
|
|
|
- host: config.DATABASE_HOST,
|
|
|
|
- port: config.DATABASE_PORT,
|
|
|
|
- user: config.DATABASE_USER,
|
|
|
|
- password: config.DATABASE_PASSWORD,
|
|
|
|
- multipleStatements: true,
|
|
|
|
- });
|
|
|
|
|
|
|
|
const dbName = config.DATABASE_NAME;
|
|
const dbName = config.DATABASE_NAME;
|
|
|
|
|
|
- await connection.connect();
|
|
|
|
|
|
+ async function connectDB() {
|
|
|
|
+ try {
|
|
|
|
+ const connection = await mysql2.createConnection({
|
|
|
|
+ host: config.DATABASE_HOST,
|
|
|
|
+ port: config.DATABASE_PORT,
|
|
|
|
+ user: config.DATABASE_USER,
|
|
|
|
+ password: config.DATABASE_PASSWORD,
|
|
|
|
+ multipleStatements: true,
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ await connection.connect();
|
|
|
|
+
|
|
|
|
+ return connection;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.log(error);
|
|
|
|
+ process.exit(1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const connection = await connectDB();
|
|
|
|
|
|
async function createUserIfNotExists() {
|
|
async function createUserIfNotExists() {
|
|
await connection.query(`USE \`${dbName}\`;`);
|
|
await connection.query(`USE \`${dbName}\`;`);
|
|
@@ -80,13 +92,14 @@ async function initDatabase() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
async function main() {
|
|
async function main() {
|
|
-
|
|
|
|
- await initDatabase();
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- app.listen(SERVER_PORT, () => {
|
|
|
|
- console.log(`Marsview服务已启动 ${SERVER_HOST}:${SERVER_PORT}`);
|
|
|
|
- });
|
|
|
|
|
|
+ try {
|
|
|
|
+ await initDatabase();
|
|
|
|
+ app.listen(SERVER_PORT, () => {
|
|
|
|
+ console.log(`Marsview服务已启动 ${SERVER_HOST}:${SERVER_PORT}`);
|
|
|
|
+ });
|
|
|
|
+ } catch {
|
|
|
|
+ process.exit(1);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
main();
|
|
main();
|