-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (48 loc) · 1.83 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
import { errorMessage, otherMessage, warnMessage } from './src/logger';
import { PORT, url, key, allowConfigGen } from './config.json';
import express, { Request, Response } from 'express';
import { loadEndpoints } from './src/functions';
import { existsSync, mkdirSync } from 'fs';
import fileUpload from 'express-fileupload';
if (!existsSync('./src/files')) {
mkdirSync('./src/files');
}
const app = express();
try {
(async () => {
app.use(express.static('src/public'));
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use(fileUpload());
global.generateKey = allowConfigGen ? crypto.randomUUID() : null;
const result = await loadEndpoints(app);
if (result !== undefined) {
otherMessage(`Loaded ${result} endpoints`);
} else {
otherMessage('No endpoints found');
}
app.get('/', async (req: Request, res: Response) => {
try {
return res.status(200).render('pages/index');
} catch (err) {
errorMessage(err as string);
return res.status(500).send({ success: false, message: 'Internal server error' });
}
});
app.listen(PORT, () => {
otherMessage(`Server started on port ${PORT} @ http://localhost:${PORT}`);
if (key === 'API_KEY') {
warnMessage('The API Key is still the default key! It is recommended to change this in the config.json file.');
}
if (global.generateKey === null) return;
otherMessage(`Config is available to be generated @ ${url}/generate?key=${global.generateKey}`);
setTimeout(() => {
if (global.generateKey === null) return;
global.generateKey = null;
otherMessage(`Config is no longer available to be generated. Please restart to generate a new key.`);
}, 300000);
});
})();
} catch (error) {
errorMessage(`Error starting server: ${error}`);
}