2017-04-11 17:39:41 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
|
2019-10-27 08:51:53 -04:00
|
|
|
const errors = require('../errors')
|
2017-04-11 17:39:41 -04:00
|
|
|
const realtime = require('../realtime')
|
|
|
|
const config = require('../config')
|
|
|
|
const models = require('../models')
|
|
|
|
const logger = require('../logger')
|
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
const { urlencodedParser } = require('./utils')
|
2017-04-11 17:39:41 -04:00
|
|
|
|
|
|
|
const statusRouter = module.exports = Router()
|
|
|
|
|
2023-03-07 09:26:25 -05:00
|
|
|
statusRouter.get('/_health', function (req, res) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
})
|
|
|
|
res.send({
|
|
|
|
ready: !realtime.maintenance
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-11 17:39:41 -04:00
|
|
|
// get status
|
|
|
|
statusRouter.get('/status', function (req, res, next) {
|
2023-06-04 14:46:59 -04:00
|
|
|
if (!config.enableStatsApi) {
|
|
|
|
return errors.errorForbidden(res)
|
|
|
|
}
|
2017-04-11 17:39:41 -04:00
|
|
|
realtime.getStatus(function (data) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-07-03 14:36:40 -04:00
|
|
|
'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) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const host = req.get('host')
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const tempid = req.query.tempid
|
2017-04-11 17:39:41 -04:00
|
|
|
if (!tempid) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
|
|
|
models.Temp.findOne({
|
|
|
|
where: {
|
|
|
|
id: tempid
|
|
|
|
}
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (!temp) {
|
2019-10-27 08:51:53 -04:00
|
|
|
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)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// post status
|
|
|
|
statusRouter.post('/temp', urlencodedParser, function (req, res) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const host = req.get('host')
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const data = req.body.data
|
2017-04-11 17:39:41 -04:00
|
|
|
if (!data) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`SERVER received temp from [${host}]: ${req.body.data}`)
|
2017-04-11 17:39:41 -04:00
|
|
|
models.Temp.create({
|
2022-05-01 15:14:27 -04:00
|
|
|
data
|
2017-04-11 17:39:41 -04:00
|
|
|
}).then(function (temp) {
|
|
|
|
if (temp) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: temp.id
|
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-06-23 18:06:03 -04:00
|
|
|
|
|
|
|
statusRouter.get('/config', function (req, res) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const data = {
|
2018-06-23 18:06:03 -04:00
|
|
|
domain: config.domain,
|
|
|
|
urlpath: config.urlPath,
|
|
|
|
debug: config.debug,
|
2018-10-05 13:33:40 -04:00
|
|
|
version: config.fullversion,
|
2018-06-23 18:06:03 -04:00
|
|
|
DROPBOX_APP_KEY: config.dropbox.appKey,
|
2019-10-21 16:10:06 -04:00
|
|
|
allowedUploadMimeTypes: config.allowedUploadMimeTypes,
|
2020-08-26 20:04:49 -04:00
|
|
|
linkifyHeaderStyle: config.linkifyHeaderStyle,
|
|
|
|
cookiePolicy: config.cookiePolicy
|
2018-06-23 18:06:03 -04:00
|
|
|
}
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-07-03 14:36:40 -04:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
|
|
|
|
'Content-Type': 'application/javascript'
|
2018-06-23 18:06:03 -04:00
|
|
|
})
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('../js/lib/common/constant.ejs', data)
|
2018-06-23 18:06:03 -04:00
|
|
|
})
|