arma-server-web-admin/routes/logs.js

56 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

var express = require('express')
2017-04-01 09:19:30 +00:00
module.exports = function (logsManager) {
var router = express.Router()
2017-04-01 09:19:30 +00:00
router.get('/', function (req, res) {
logsManager.logFiles(function (err, files) {
if (err) {
res.status(500).send(err)
2017-04-01 09:19:30 +00:00
} else {
res.json(files)
}
})
})
2017-10-03 23:04:09 +00:00
router.delete('/:log', function (req, res) {
var filename = req.params.log
logsManager.delete(filename, function (err) {
if (err) {
res.status(500).send(err)
} else {
res.status(204).send()
}
})
})
router.get('/:log/:mode', function (req, res) {
var requestedFilename = req.params.log
var mode = req.params.mode === 'view' ? 'view' : 'download'
2017-04-01 09:19:30 +00:00
logsManager.getLogFile(requestedFilename, function (err, file) {
if (err) {
res.status(500).send(err)
2017-04-01 09:19:30 +00:00
} else {
if (file) {
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)
})
}
} else {
res.status(404).send(new Error('File not found'))
}
2017-04-01 09:19:30 +00:00
}
})
})
2017-04-01 09:19:30 +00:00
return router
}