hedgedoc/old_src/lib/web/statusRouter.ts

107 lines
2.8 KiB
TypeScript
Raw Normal View History

import { config } from '../config'
import { Router } from 'express'
import { errors } from '../errors'
import { realtime } from '../realtime'
import { Temp } from '../models'
import { logger } from '../logger'
import { urlencodedParser } from './utils'
const StatusRouter = Router()
2017-04-11 17:39:41 -04:00
// get status
StatusRouter.get('/status', function (req, res, _) {
2017-04-11 17:39:41 -04:00
realtime.getStatus(function (data) {
res.set({
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
'Content-Type': 'application/json'
2017-04-11 17:39:41 -04:00
})
res.send(data)
})
})
// get status
StatusRouter.get('/temp', function (req, res) {
const host = req.get('host')
if (config.allowOrigin.indexOf(host) === -1) {
errors.errorForbidden(res)
2017-04-11 17:39:41 -04:00
} else {
const tempid = req.query.tempid
if (!tempid || typeof tempid !== 'string') {
errors.errorForbidden(res)
2017-04-11 17:39:41 -04:00
} else {
Temp.findOne({
2017-04-11 17:39:41 -04:00
where: {
id: tempid
}
}).then(function (temp) {
if (!temp) {
errors.errorNotFound(res)
2017-04-11 17:39:41 -04:00
} else {
res.header('Access-Control-Allow-Origin', '*')
res.send({
temp: temp.data
})
temp.destroy().catch(function (err) {
if (err) {
logger.error('remove temp failed: ' + err)
}
})
}
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
2017-04-11 17:39:41 -04:00
})
}
}
})
// post status
StatusRouter.post('/temp', urlencodedParser, function (req, res) {
const host = req.get('host')
if (config.allowOrigin.indexOf(host) === -1) {
errors.errorForbidden(res)
2017-04-11 17:39:41 -04:00
} else {
const data = req.body.data
2017-04-11 17:39:41 -04:00
if (!data) {
errors.errorForbidden(res)
2017-04-11 17:39:41 -04:00
} else {
logger.debug(`SERVER received temp from [${host}]: ${req.body.data}`)
Temp.create({
2017-04-11 17:39:41 -04:00
data: data
}).then(function (temp) {
if (temp) {
res.header('Access-Control-Allow-Origin', '*')
res.send({
status: 'ok',
id: temp.id
})
} else {
errors.errorInternalError(res)
2017-04-11 17:39:41 -04:00
}
}).catch(function (err) {
logger.error(err)
return errors.errorInternalError(res)
2017-04-11 17:39:41 -04:00
})
}
}
})
StatusRouter.get('/config', function (req, res) {
const data = {
domain: config.domain,
urlpath: config.urlPath,
debug: config.debug,
version: config.fullversion,
DROPBOX_APP_KEY: config.dropbox.appKey,
allowedUploadMimeTypes: config.allowedUploadMimeTypes,
linkifyHeaderStyle: config.linkifyHeaderStyle
}
res.set({
'Cache-Control': 'private', // only cache by client
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
'Content-Type': 'application/javascript'
})
res.render('../js/lib/common/constant.ejs', data)
})
export { StatusRouter }