-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (76 loc) · 2.39 KB
/
app.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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const csv_parse = require("csv-parse/lib/sync");
const path = require('path');
const https = require("https");
const fs = require("fs");
const apiRouter = require('./routes/api');
const option = require("./option");
const app = express();
process.env.TZ = "Asia/Seoul";
global.__registerTime = (new Date(
"2020-07-22 21:00:0"
)).getTime();
global.__finishTime = (new Date(
"2020-07-25 0:0:0"
)).getTime();
global.__database = null;
global.__student = csv_parse(fs.readFileSync("./grade.csv"), {
skip_empty_lines: true,
trim: true
}).slice(1);
let queue = [], running = [];
global.__pushApplicationTask = (groupId, cb) => {
if (queue[groupId] === undefined) {
queue[groupId] = [cb];
}
else queue[groupId].push(cb);
if (running[groupId] !== true) {
running[groupId] = true;
executeGroupTask(groupId);
}
};
const executeGroupTask = async(groupId) => {
if (queue[groupId] !== undefined) {
if (queue[groupId].length > 0) {
try {
let ret = await queue[groupId].shift()();
console.log(groupId, ret, 'app');
if (ret <= 0) {
let tmp = queue[groupId].slice(0);
queue[groupId] = [];
running[groupId] = false;
while (tmp.length > 0) tmp.shift()();
}else setTimeout(() => executeGroupTask(groupId), 3);
} catch (e) {
console.log("app.js queue process");
console.log(e);
}
}else running[groupId] = false;
}
};
//app.use(require("cors")());
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', apiRouter);
app.use((req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'))
});
(async () => {
await MongoClient.connect(option.url, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(d => global.__database = d.db('groups')).catch(e => {
console.log("DB Connection Fail");
console.log(e);
process.exit(0);
});
https.createServer({
cert: fs.readFileSync('./ssl/cert.pem'),
ca: fs.readFileSync('./ssl/fullchain.pem'),
key: fs.readFileSync('./ssl/privkey.pem')
}, app).listen(443, () => {
console.log('HTTPS Server started on port 443');
});
})();