-
Notifications
You must be signed in to change notification settings - Fork 2
/
mark-server.js
266 lines (234 loc) · 8.1 KB
/
mark-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// ==Headers==
// @Name: mark-server
// @Description: mark-server
// @Version: 1.0.243
// @Author: dodying
// @Created: 2020-07-09 15:39:26
// @Modified: 2023-11-09 19:18:39
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: mysql2,express,body-parser
// ==/Headers==
// 设置
const port = 5556;
const config = {
host: process.env.MYSQl_HOST,
user: 'root',
password: '',
database: 'mark',
};
let connection = null;
let connectionLastTime = null;
const connectionTimeout = 5 * 60 * 1000;
const lastConnection = {
info: {},
result: [],
};
let connecting = false;
const columns = {
name: 'VARCHAR(511) NOT NULL UNIQUE', // 名称
mark: 'VARCHAR(20) NOT NULL',
};
// 导入原生模块
// const path = require('path');
// 导入第三方模块
const mysql = require('mysql2/promise');
const express = require('express');
const bodyParser = require('body-parser');
const waitInMs = require('../_lib/waitInMs');
const wait = require('../_lib/wait');
// Function
const createConnection = async (obj) => {
/* eslint-disable no-unmodified-loop-condition */
while (connecting) {
await waitInMs(200);
}
connecting = true;
let isInfoSame = lastConnection.info.host === obj.host;
isInfoSame = isInfoSame && lastConnection.info.user === obj.user;
isInfoSame = isInfoSame && lastConnection.info.password === obj.password;
if (lastConnection.result[1] === 1 && isInfoSame && new Date().getTime() - connectionLastTime < connectionTimeout) {
connecting = false;
return lastConnection.result;
}
lastConnection.info = {
host: obj.host,
user: obj.user,
password: obj.password,
};
console.log('re-connection');
try {
if (connection && connection.end && typeof connection.end === 'function') connection.end();
} catch (error) {
console.log({ err: error, msg: error.message });
}
try {
const { database, ...objLeft } = obj;
connection = await mysql.createConnection({
host: obj.host,
user: obj.user,
password: obj.password,
keepAliveInitialDelay: 10000,
enableKeepAlive: true,
dateStrings: true,
// timezone: '+00:00',
...objLeft,
});
connection.on('error', (err) => {
if (['PROTOCOL_CONNECTION_LOST'].includes(err.code)) {
createConnection(obj);
} else {
console.log('Database error:', { err });
}
});
connectionLastTime = new Date().getTime();
} catch (error) {
if (error.message.match('Too many connections')) {
connecting = false;
return createConnection(obj);
}
console.log({ err: error, msg: error.message });
connection = null;
connecting = false;
connectionLastTime = null;
return ['Connection Failed, please check info', -1];
}
const [rows] = await connection.query('SHOW DATABASES');
if (rows.filter((i) => i.Database === obj.database).length) {
await connection.query(`USE ${obj.database}`);
connecting = false;
return ['Connection Success, and you can update', 1];
}
connecting = false;
connectionLastTime = null;
return ['Connection Success, but you need to init', 0];
};
const createDatabase = async (name) => {
let queryString = `CREATE DATABASE IF NOT EXISTS \`${name}\` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci`;
await connection.query(queryString);
queryString = `USE \`${name}\``;
await connection.query(queryString);
};
const createTable = async (name) => {
const queryString = `CREATE TABLE IF NOT EXISTS \`${name}\` (${[
'id INT UNSIGNED NOT NULL AUTO_INCREMENT',
...Object.keys(columns).map((i) => `\`${i}\` ${columns[i]}`),
'PRIMARY KEY (id)',
'INDEX (name)',
].join(', ')})`;
await connection.query(queryString);
};
const updateExistsTables = async () => {
const queryString = 'SHOW TABLES';
const [rows] = await connection.query(queryString);
return rows.map((i) => Object.values(i)).flat();
};
// Main
const main = async () => {
const [status, code] = await createConnection(config);
lastConnection.result = [status, code];
if (code === -1) {
console.log('Exit: Connection Failed');
return;
} if (code === 0) {
await createDatabase(config.database);
}
let existsTable = await updateExistsTables();
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false, parameterLimit: 1000000 }));
let busy = false;
app.all('*', async (req, res, next) => {
await wait.for(() => !busy, 30 * 1000);
busy = true;
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
res.header('X-Powered-By', ' 3.2.1');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
app.post('/query', async (req, res, next) => {
if (!connection || new Date().getTime() - connectionLastTime >= connectionTimeout) await createConnection(config);
const result = [];
console.log('QUERY /', req.body);
const { category } = req.body;
if (!(existsTable.includes(category))) {
await createTable(category);
existsTable = await updateExistsTables();
}
for (const name of [].concat(req.body.names)) {
const [rows] = await connection.query(`SELECT * FROM \`${category}\` WHERE name=${mysql.escape(name)}`);
result.push(rows.length ? rows[0].mark : 'null');
}
console.log('RESPONSE /', result);
res.writeHead(200, {
'Content-Type': 'application/json;charset=utf-8',
});
res.end(JSON.stringify(result, null, 2));
next();
});
app.post('/search', async (req, res, next) => { // TODO
if (!connection || new Date().getTime() - connectionLastTime >= connectionTimeout) await createConnection(config);
let result = [];
console.log('SEARCH /', req.body);
const { category } = req.body;
if (!(existsTable.includes(category))) {
await createTable(category);
existsTable = await updateExistsTables();
}
const [rows] = await connection.query(`SELECT name FROM \`${category}\` WHERE name REGEXP ${mysql.escape(req.body.name)}`);
result = rows.length ? rows.map((i) => i.name) : [];
console.log('RESPONSE /', result);
res.writeHead(200, {
'Content-Type': 'application/json;charset=utf-8',
});
res.end(JSON.stringify(result, null, 2));
next();
});
app.post('/update', async (req, res, next) => {
if (!connection || new Date().getTime() - connectionLastTime >= connectionTimeout) await createConnection(config);
const result = true;
console.log('UPDATE /', req.body);
const { category } = req.body;
if (!(existsTable.includes(category))) {
await createTable(category);
existsTable = await updateExistsTables();
}
const valuesAdd = [];
const valuesDelete = [];
for (const item of [].concat(req.body.items)) {
const arr = item.split('|');
const [name, mark] = [arr.slice(0, -1).join('|'), arr.slice(-1)[0]];
if (arr.slice(-1)[0] === 'null') {
valuesDelete.push(name);
} else {
valuesAdd.push([name, mark]);
}
}
if (valuesAdd.length) {
const values = valuesAdd.map((i) => `(${i.map((j) => (j === 'NULL' ? 'NULL' : mysql.escape(j))).join(', ')})`).join(',\n');
await connection.query(`INSERT INTO \`${category}\` (${Object.keys(columns).join(', ')}) values ${values} ON DUPLICATE KEY UPDATE mark=VALUES(mark)`);
}
if (valuesDelete.length) {
await connection.query(`DELETE FROM \`${category}\` WHERE ${valuesDelete.map((i) => `name=${mysql.escape(i)}`).join(' OR ')}`);
}
console.log('RESPONSE /', result);
res.writeHead(200, {
'Content-Type': 'application/json;charset=utf-8',
});
res.end(String(result));
next();
});
app.all('*', (req, res, next) => {
busy = false;
});
app.listen(port);
console.log(`Listening at http://localhost:${port}`);
};
main().then(async () => {
//
}, async (err) => {
console.error(err);
process.exit(1);
});