-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (54 loc) · 1.51 KB
/
index.ts
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
import express from 'express';
import hbs from 'hbs';
import fs from 'fs';
import path from 'path';
import csv from 'csv-parser';
const app = express();
const PORT = 8000;
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.set('view engine', hbs);
interface ITask {
id: number,
name: string,
delay: number
};
interface IRuntimeTask extends ITask {
timeOfRunning: number,
currentTime: number
};
const tasks: ITask[] = [];
let runtimeTasks: IRuntimeTask[] = [];
app.get('/', (req, res) => {
if(runtimeTasks){
runtimeTasks.forEach(task => {
task.currentTime = Math.floor(task.delay - (Date.now() - task.timeOfRunning) / 1000);
});
}
res.render('main.hbs', {tasks: tasks, runtime: runtimeTasks});
});
app.post('/:id', (req, res) => {
let id = Number(req.params.id) - 1;
let runTask = {
id: tasks[id].id,
name: tasks[id].name,
delay: tasks[id].delay,
timeOfRunning: Date.now(),
currentTime: tasks[id].delay
};
runtimeTasks.push(runTask);
setTimeout(() => {
let index = runtimeTasks.findIndex(el => el.id == runTask.id);
runtimeTasks.splice(index, 1);
}, tasks[id].delay * 1000);
res.redirect('/');
});
app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
fs.createReadStream('table.csv')
.pipe(csv())
.on('data', (data) => tasks.push({id: data.ID, name: data.name, delay: data.delay}))
.on('end', () => {
console.log('Tasks loaded');
});
});