add: forum router

This commit is contained in:
Renge 2022-09-24 22:42:02 -04:00
parent eba9846e60
commit fb4a5806b3
2 changed files with 130 additions and 0 deletions

124
api/apis/forum-apis.js Normal file
View File

@ -0,0 +1,124 @@
const uuid = require('uuid');
const config = require('../config')
const rabbitMQ = config.rabbitMQ
const amqp = require('amqplib/callback_api');
postThread = async (req, res) => {
req = req.body;
return await assert("postThread", req, res);
}
getCookThreadList = async (req, res) => {
req = req.body;
return await assert("getCookThreadList", req, res);
}
getEatThreadList = async (req, res) => {
req = req.body;
return await assert("getEatThreadList", req, res);
}
getCookThread = async (req, res) => {
req = req.body;
return await assert("getCookThread", req, res);
}
searchCookThread = async (req, res) => {
req = req.body;
return await assert("searchCookThread", req, res);
}
searchEatThread = async (req, res) => {
req = req.body;
return await assert("searchEatThread", req, res);
}
// updateThread = async (req, res) => {
// req = req.body;
// return await assert("updateThread", req, res);
// }
// logoutUser = async (req, res) => {
// return await res.cookie("token", '', {
// httpOnly: true,
// secure: true,
// sameSite: "lax"
// }).status(200).json({
// success: true
// })
// }
// /deletethread
// /favorite
// /unfavorite
// /getrandomthreadlist:num
const assert = async (queueName, req, res) => {
amqp.connect(rabbitMQ, function (error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
channel.assertQueue('', {
exclusive: true
}, function (error2, q) {
if (error2) {
throw error2;
}
let correlationId = uuid.v4();
console.log(' [x] Requesting: ', req);
channel.consume(q.queue, function (msg) {
console.log(msg)
if (msg.properties.correlationId == correlationId) {
console.log(' [.] Got %s', msg.content.toString());
msg = JSON.parse(msg.content)
console.log(msg.cookie)
if (msg.cookie) {
res.cookie("token", msg.cookie, {
httpOnly: true,
secure: true,
sameSite: "lax"
}).status(msg.status).send(msg.body)
}
else {
res.status(msg.status).send(msg.body)
}
setTimeout(function () {
connection.close();
}, 500);
}
else {
res.status(500).send("Internal server error")
}
}, {
noAck: true
});
channel.sendToQueue(queueName,
Buffer.from(JSON.stringify(req)), {
correlationId: correlationId,
replyTo: q.queue
});
});
});
});
}
module.exports = {
postThread,
getCookThreadList,
getEatThreadList,
getCookThread,
searchCookThread,
searchEatThread,
}

View File

@ -1,9 +1,15 @@
const express = require('express') const express = require('express')
const router = express.Router() const router = express.Router()
const UserAPI = require('../apis/user-apis') const UserAPI = require('../apis/user-apis')
const ForumAPI = require('../apis/forum-apis')
router.post('/login', UserAPI.loginUser) router.post('/login', UserAPI.loginUser)
router.post('/register', UserAPI.registerUser) router.post('/register', UserAPI.registerUser)
router.get('/logout', UserAPI.logoutUser) router.get('/logout', UserAPI.logoutUser)
router.post('/postthread', ForumAPI.postThread)
router.get
module.exports = router module.exports = router