-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (62 loc) · 2.36 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
const fs = require('fs');
const path = require('path');
const log4js = require('log4js');
const express = require('express');
const bodyParser = require('body-parser');
const speakeasy = require('speakeasy');
const containerController = require('./controllers/container.controller');
const imageController = require('./controllers/image.controller');
const repoController = require('./controllers/repo.controller');
const configController = require('./controllers/config.controller');
const taskController = require('./controllers/task.controller');
const app = express();
const logger = log4js.getLogger('Server');
const authUrls = [];
const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
const PORT = process.env.PORT || 4000;
const SECRET = process.env.SECRET || 'itworks@123';
logger.level = LOG_LEVEL;
if (!fs.existsSync(path.join(process.cwd(), 'db/repos.json'))) {
fs.writeFileSync(path.join(process.cwd(), 'db/repos.json'), '[]', 'utf8');
}
if (!fs.existsSync(path.join(process.cwd(), 'db/config.json'))) {
fs.writeFileSync(path.join(process.cwd(), 'db/config.json'), '{}', 'utf8');
}
if (!fs.existsSync(path.join(process.cwd(), 'db/releases.json'))) {
fs.writeFileSync(path.join(process.cwd(), 'db/releases.json'), '[]', 'utf8');
}
if (!fs.existsSync(path.join(process.cwd(), 'db/tasks.json'))) {
fs.writeFileSync(path.join(process.cwd(), 'db/tasks.json'), '[]', 'utf8');
}
app.use(express.static(path.join(__dirname, 'dist')));
app.use(bodyParser());
app.use(function (req, res, next) {
if (authUrls.indexOf(req.path) > -1) {
const token = req.params.token || req.query.token || req.body.token;
if (token) {
if (speakeasy.totp.verify({
secret: SECRET,
token: token
})) {
next();
} else {
res.status(401).json({ message: 'Unauthorised' });
}
} else {
res.status(401).json({ message: 'Unauthorised' });
}
} else {
next();
}
});
app.use('/api/container', containerController);
app.use('/api/image', imageController);
app.use('/api/repos', repoController);
app.use('/api/config', configController);
app.use('/api/task', taskController);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
app.listen(PORT, () => {
logger.info('Server is listening on port', PORT);
});