2017-08-24 09:15:06 +00:00
|
|
|
var express = require('express')
|
2017-04-01 09:19:30 +00:00
|
|
|
|
2015-10-31 14:27:16 +00:00
|
|
|
module.exports = function (logsManager) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var router = express.Router()
|
2017-04-01 09:19:30 +00:00
|
|
|
|
|
|
|
router.get('/', function (req, res) {
|
|
|
|
logsManager.logFiles(function (err, files) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
res.status(500).send(err)
|
2017-04-01 09:19:30 +00:00
|
|
|
} else {
|
2017-08-24 09:15:06 +00:00
|
|
|
res.json(files)
|
2014-09-28 08:50:03 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
})
|
2014-09-28 08:50:03 +00:00
|
|
|
|
2017-10-01 15:46:43 +00:00
|
|
|
router.get('/:log/:mode', function (req, res) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var requestedFilename = req.params.log
|
2017-10-01 15:46:43 +00:00
|
|
|
var mode = req.params.mode === 'view' ? 'view' : 'download'
|
2017-04-01 09:19:30 +00:00
|
|
|
|
|
|
|
logsManager.getLogFile(requestedFilename, function (err, file) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
res.status(500).send(err)
|
2017-04-01 09:19:30 +00:00
|
|
|
} else {
|
|
|
|
if (file) {
|
2017-10-01 15:46:43 +00:00
|
|
|
if (mode === 'download') {
|
|
|
|
res.download(file.path)
|
|
|
|
} else {
|
|
|
|
logsManager.readLogFile(file.path, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).send(err)
|
|
|
|
}
|
|
|
|
res.contentType('text/plain')
|
|
|
|
res.send(data)
|
|
|
|
})
|
|
|
|
}
|
2015-10-31 14:27:16 +00:00
|
|
|
} else {
|
2017-08-24 09:15:06 +00:00
|
|
|
res.status(404).send(new Error('File not found'))
|
2015-10-31 14:27:16 +00:00
|
|
|
}
|
2017-04-01 09:19:30 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
})
|
2017-04-01 09:19:30 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
return router
|
|
|
|
}
|