webio-node/app.js

138 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const cors = require('cors');
const logger = require('morgan');
const path = require('path');
const config = require('./config');
const so = require('./includes/storeObject');
2017-04-18 01:24:41 +00:00
const exec = require('child_process').exec;
2017-05-04 06:36:29 +00:00
const fs = require('fs');
const archiver = require('archiver');
2017-03-22 05:35:45 +00:00
const app = express();
const api_route = require('./route/api');
// storeObject interval clear
2017-04-05 07:35:32 +00:00
const clearStore = setInterval(() => {
2017-03-22 05:35:45 +00:00
so.clear();
}, 30000)
app.set('port', process.env.PORT || config.system.port);
app.use(logger('short'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
resave: false,
saveUninitialized: true,
secret: '6520833345e05e0dcfce'
}));
app.use(cors());
app.use(express.static(path.resolve(__dirname, 'public')));
app.use('/semantic', express.static(path.resolve(__dirname, 'node_modules', 'semantic-ui-css')));
app.use('/react-datetime', express.static(path.resolve(__dirname, 'node_modules', 'react-datetime', 'css')));
2017-04-05 07:35:32 +00:00
app.use(/\/locales\/(\w+).json/, (req, res, n) => {
res.sendFile(path.resolve(__dirname, 'public', 'locales', 'zh.json'));
})
2017-03-22 05:35:45 +00:00
const server = app.listen(app.get('port'), () => {
console.log(`Server start on port ${server.address().port}`);
});
// use route
app.use('/api', api_route);
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'views', 'index.html'));
});
2017-04-05 07:35:32 +00:00
app.get(['/admin', '/admin/*'], (req, res) => {
2017-03-22 05:35:45 +00:00
res.sendFile(path.resolve(__dirname, 'views', 'admin.html'));
2017-03-28 05:53:16 +00:00
});
2017-05-03 09:15:08 +00:00
app.get('/camevent', (req, res) => {
2017-05-04 06:36:29 +00:00
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
ip = ip.split(':').pop();
let cmd = `camcam ${ip} ${Math.floor(Date.now() / 1000)}`;
exec(cmd, (err, sout, serr) => {
res.send({ ip });
});
})
app.get(['/viewcamimg/:dir/:file', '/dlcamvideo/:dir/:file'], async(req, res) => {
2017-05-04 06:36:29 +00:00
let dir = req.params.dir;
let file = req.params.file;
if (!dir || !file) return res.sendStatus(404);
2017-05-04 06:36:29 +00:00
try {
let stat = await new Promise((resolve, reject) => {
fs.stat(path.resolve(config.cmdpath.ipcamsave, dir, file), (err, stats) => {
2017-05-04 06:36:29 +00:00
if (err) return reject(err);
return resolve(stats);
})
})
2017-05-05 02:45:44 +00:00
if (!stat.isFile()) return res.sendStatus(404);
2017-05-04 06:36:29 +00:00
} catch (e) {
2017-05-05 02:45:44 +00:00
return res.sendStatus(404);
2017-05-04 06:36:29 +00:00
}
res.sendfile(path.resolve(config.cmdpath.ipcamsave, dir, file))
})
app.get('/dlevent/:dir', async(req, res) => {
let dir = req.params.dir;
if (!dir) return res.sendStatus(404);
let list = [];
try {
let stat = await new Promise((resolve, reject) => {
fs.stat(path.resolve(config.cmdpath.ipcamsave, dir), (err, stats) => {
if (err) return reject(err);
return resolve(stats);
})
});
if (!stat.isDirectory()) return res.sendStatus(404);
list = await new Promise((resolve, reject) => {
fs.readdir(path.resolve(config.cmdpath.ipcamsave, dir), (err, fis) => {
if (err) return reject(err);
return resolve(fis);
})
})
} catch (e) {
return res.sendStatus(404);
}
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=cam_event.zip'
});
let zip = archiver('zip', { store: true });
zip.pipe(res);
for (let i of list) {
zip.file(path.resolve(config.cmdpath.ipcamsave, dir, i), { name: i })
}
zip.finalize();
2017-05-03 09:15:08 +00:00
})
2017-04-05 07:35:32 +00:00
app.get('/servcmd', (req, res) => {
2017-05-04 09:29:20 +00:00
res.send({ status: `exit time ${(new Date()).toString()}` });
2017-04-05 07:35:32 +00:00
setTimeout(() => { process.exit(1) }, 2000);
2017-04-18 01:24:41 +00:00
});
app.get('/fixmysql', (req, res) => {
2017-04-28 02:12:50 +00:00
exec('systemctl stop mysqld', (err, sout, serr) => {
let cmd = 'rm /home/www/mydb/aria*';
let json = {
rm: '',
restart: ''
};
exec(cmd, (err, stdout, stderr) => {
2017-05-02 02:03:13 +00:00
let cmd = 'systemctl start mysqld';
2017-04-28 02:12:50 +00:00
json.rm = stdout;
exec(cmd, (err, sout, serr) => {
json.restart = sout;
res.send(json);
})
2017-04-18 01:24:41 +00:00
})
})
2017-04-27 10:26:40 +00:00
});