summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoHong Li2017-04-12 05:39:41 +0800
committerRaccoon Li2017-05-08 19:24:37 +0800
commit766022378a2f276df2f2d2f003e2124044ab2df0 (patch)
treec9ecbaa7748454ef7413ddb5e848438817efbf50
parent66c68254b4c213fcfc03ce081c47d24bcd42bbd3 (diff)
refactor(app.js): Extract status pages
Diffstat (limited to '')
-rw-r--r--app.js79
-rw-r--r--lib/web/statusRouter.js92
2 files changed, 93 insertions, 78 deletions
diff --git a/app.js b/app.js
index f5f95573..f0ccd7d4 100644
--- a/app.js
+++ b/app.js
@@ -199,84 +199,6 @@ app.set('views', path.join(__dirname, '/public/views'))
app.engine('ejs', ejs.renderFile)
// set view engine
app.set('view engine', 'ejs')
-// get status
-app.get('/status', function (req, res, next) {
- realtime.getStatus(function (data) {
- res.set({
- 'Cache-Control': 'private', // only cache by client
- 'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
- 'HackMD-Version': config.version
- })
- res.send(data)
- })
-})
-// get status
-app.get('/temp', function (req, res) {
- var host = req.get('host')
- if (config.alloworigin.indexOf(host) === -1) {
- response.errorForbidden(res)
- } else {
- var tempid = req.query.tempid
- if (!tempid) {
- response.errorForbidden(res)
- } else {
- models.Temp.findOne({
- where: {
- id: tempid
- }
- }).then(function (temp) {
- if (!temp) {
- response.errorNotFound(res)
- } 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 response.errorInternalError(res)
- })
- }
- }
-})
-// post status
-app.post('/temp', urlencodedParser, function (req, res) {
- var host = req.get('host')
- if (config.alloworigin.indexOf(host) === -1) {
- response.errorForbidden(res)
- } else {
- var data = req.body.data
- if (!data) {
- response.errorForbidden(res)
- } else {
- if (config.debug) {
- logger.info('SERVER received temp from [' + host + ']: ' + req.body.data)
- }
- models.Temp.create({
- data: data
- }).then(function (temp) {
- if (temp) {
- res.header('Access-Control-Allow-Origin', '*')
- res.send({
- status: 'ok',
- id: temp.id
- })
- } else {
- response.errorInternalError(res)
- }
- }).catch(function (err) {
- logger.error(err)
- return response.errorInternalError(res)
- })
- }
- }
-})
function setReturnToFromReferer (req) {
var referer = req.get('referer')
@@ -417,6 +339,7 @@ if (config.email) {
})
}
app.use(require('./lib/web/baseRouter'))
+app.use(require('./lib/web/statusRouter'))
app.post('/login', urlencodedParser, function (req, res, next) {
if (!req.body.email || !req.body.password) return response.errorBadRequest(res)
diff --git a/lib/web/statusRouter.js b/lib/web/statusRouter.js
new file mode 100644
index 00000000..aa3a9b79
--- /dev/null
+++ b/lib/web/statusRouter.js
@@ -0,0 +1,92 @@
+'use strict'
+
+const Router = require('express').Router
+
+const response = require('../response')
+const realtime = require('../realtime')
+const config = require('../config')
+const models = require('../models')
+const logger = require('../logger')
+
+const {urlencodedParser} = require('./utils')
+
+const statusRouter = module.exports = Router()
+
+// get status
+statusRouter.get('/status', function (req, res, next) {
+ realtime.getStatus(function (data) {
+ res.set({
+ 'Cache-Control': 'private', // only cache by client
+ 'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
+ 'HackMD-Version': config.version
+ })
+ res.send(data)
+ })
+})
+// get status
+statusRouter.get('/temp', function (req, res) {
+ var host = req.get('host')
+ if (config.alloworigin.indexOf(host) === -1) {
+ response.errorForbidden(res)
+ } else {
+ var tempid = req.query.tempid
+ if (!tempid) {
+ response.errorForbidden(res)
+ } else {
+ models.Temp.findOne({
+ where: {
+ id: tempid
+ }
+ }).then(function (temp) {
+ if (!temp) {
+ response.errorNotFound(res)
+ } 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 response.errorInternalError(res)
+ })
+ }
+ }
+})
+// post status
+statusRouter.post('/temp', urlencodedParser, function (req, res) {
+ var host = req.get('host')
+ if (config.alloworigin.indexOf(host) === -1) {
+ response.errorForbidden(res)
+ } else {
+ var data = req.body.data
+ if (!data) {
+ response.errorForbidden(res)
+ } else {
+ if (config.debug) {
+ logger.info('SERVER received temp from [' + host + ']: ' + req.body.data)
+ }
+ models.Temp.create({
+ data: data
+ }).then(function (temp) {
+ if (temp) {
+ res.header('Access-Control-Allow-Origin', '*')
+ res.send({
+ status: 'ok',
+ id: temp.id
+ })
+ } else {
+ response.errorInternalError(res)
+ }
+ }).catch(function (err) {
+ logger.error(err)
+ return response.errorInternalError(res)
+ })
+ }
+ }
+})