-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
169 lines (144 loc) · 4.17 KB
/
index.mjs
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
import express from 'express';
import log4js from 'log4js';
import fs from 'node:fs';
log4js.configure('./logconfig.json');
const logger = log4js.getLogger();
logger.info(`Application is starting (NODE_ENV=${process.env.NODE_ENV})`);
const settings = JSON.parse(fs.readFileSync('./config.json').toString());
if (settings.port === undefined ||
settings.port === '' ||
isNaN(parseInt(settings.port))) {
logger.error('Invalid port number: ' + settings.port);
process.exit();
}
import { db } from './db.mjs';
const backupDir = settings.backup['dest.dir'];
const backupInterval = settings.backup['interval.minutes'];
if (backupDir.length > 0 && backupInterval > 0) {
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir);
}
setInterval(() => db.backup(backupDir), backupInterval * 1000 * 60);
}
const app = express();
app.use('/', express.static('./views'));
app.use('/socket.io', express.static('./node_modules/socket.io/client-dist'));
app.use(express.urlencoded({
extended: true
}));
app.use(express.json());
app.set('views', './views');
app.use(log4js.connectLogger(logger, {}));
import http from 'node:http';
http.createServer(app);
import { Server } from 'socket.io';
const server = http.createServer(app).listen(settings.port, () => logger.info('Listening to port: ' + settings.port));
const io = new Server(server);
/**
* @typedef {{
* id?: string,
* color?: string,
* pos?: {
* left: number,
* top: number
* },
* text?: string,
* }} TaskInfo
*/
io.on('connection', (socket) => {
logger.info(`User connected: ${socket.id}`);
const rows = db.getAll();
socket.emit('welcome', {
tasks: rows
});
socket.on('create', onmessage(create));
socket.on('color', onmessage(changeColor));
socket.on('text', onmessage(editText));
socket.on('move', onmessage(move));
socket.on('delete', onmessage(deleteTask));
socket.on('tofront', onmessage(updateToFront));
socket.on('disconnect', _ => logger.info(`User disconnected: ${socket.id}`));
});
app.get('/', (req, res) => {
res.header({
'Cache-Control': 'no-store'
})
.render('index');
});
function onmessage(callback) {
return function (message) {
try {
callback(message);
} catch (e) {
io.emit('error');
}
};
}
/**
* @param {TaskInfo} info
*/
function create(info) {
const result = {
pos: {
top: (info.pos && info.pos.top) || 0,
left: (info.pos && info.pos.left) || 0
},
text: info.text || '',
color: info.color || 'white',
id: createId()
};
io.emit('create', result);
db.createTask(result);
logger.info(`Task[${result.id}] is created.`);
}
function createId() {
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz';
const max = ALPHABET.length;
const abc = () => ALPHABET[Math.floor(Math.random() * max)];
return Date.now() + abc() + abc() + abc();
}
/**
* @param {{ ids: string[] }} message
*/
function deleteTask(message) {
io.emit('delete', message);
const { ids } = message;
db.deleteTask(ids);
logger.info(`Task[${ids.join(', ')}] is deleted.`);
}
/**
* @param {{ ids: string[], color?: string }} message
*/
function changeColor(message) {
io.emit('color', message);
const { ids, color } = message;
db.changeColor(ids, color);
logger.info(`Task[${ids.join(',')}] color changed: ${JSON.stringify(color)}`);
}
/**
* @param {{ id: string, text: string }} message
*/
function editText(message) {
io.emit('text', message);
const { id, text } = message;
db.editText(id, text);
logger.info(`Task[${id}] text changed: "${text}"`);
}
/**
* @param {{ tasks: { id: string, pos: { top: number, left: number }}[] }} message
*/
function move(message) {
io.emit('move', message);
const { tasks } = message;
db.moveTask(tasks);
logger.info(`Task moved: ${JSON.stringify(tasks)}`);
}
/**
* @param {{ ids: string[] }} message
*/
function updateToFront(message) {
io.emit('tofront', message);
const { ids } = message;
db.updateToFront(ids);
logger.info(`Task[${ids.join(', ')}] changed: to top.`);
}