Skip to content

Commit

Permalink
feat : add categories/hashtags update api
Browse files Browse the repository at this point in the history
  • Loading branch information
papicc45 committed Nov 22, 2023
1 parent 3998d4c commit b3a11ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
52 changes: 26 additions & 26 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ app.use('/users', usersRouter);

const Eureka = require('eureka-js-client').Eureka;

const client = new Eureka({
// Eureka 서버의 설정
eureka: {
host: process.env.EUREKA_HOST, // Eureka 서버의 주소
port: process.env.EUREKA_PORT, // Eureka 서버의 포트
servicePath: '/eureka/apps/'
},
instance: {
instanceId : 'category-service',
app: 'category-service', // 서비스의 이름
hostName: `${process.env.INSTANCE_HOSTNAME}`,
ipAddr: `${process.env.INSTANCE_IPADDRESS}`, // 서비스의 IP 주소
port: {
'$': process.env.PORT, // 서비스의 포트
'@enabled': 'true',
},
vipAddress: 'category-service', // 서비스의 VIP 주소
statusPageUrl: `http://${process.env.INSTANCE_IPADDRESS}:${process.env.PORT}`, // 서비스의 상태 페이지 URL
healthCheckUrl: `http://${process.env.INSTANCE_IPADDRESS}:${process.env.PORT}/health`, // 서비스의 상태 페이지 URL
dataCenterInfo: {
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
});
client.start(); // Eureka 서버에 서비스를 등록
// const client = new Eureka({
// // Eureka 서버의 설정
// eureka: {
// host: process.env.EUREKA_HOST, // Eureka 서버의 주소
// port: process.env.EUREKA_PORT, // Eureka 서버의 포트
// servicePath: '/eureka/apps/'
// },
// instance: {
// instanceId : 'category-service',
// app: 'category-service', // 서비스의 이름
// hostName: `${process.env.INSTANCE_HOSTNAME}`,
// ipAddr: `${process.env.INSTANCE_IPADDRESS}`, // 서비스의 IP 주소
// port: {
// '$': process.env.PORT, // 서비스의 포트
// '@enabled': 'true',
// },
// vipAddress: 'category-service', // 서비스의 VIP 주소
// statusPageUrl: `http://${process.env.INSTANCE_IPADDRESS}:${process.env.PORT}`, // 서비스의 상태 페이지 URL
// healthCheckUrl: `http://${process.env.INSTANCE_IPADDRESS}:${process.env.PORT}/health`, // 서비스의 상태 페이지 URL
// dataCenterInfo: {
// '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
// name: 'MyOwn',
// },
// },
// });
// client.start(); // Eureka 서버에 서비스를 등록
app.get('/health', (req, res) => {
res.json({status: 'UP'});
});
Expand Down
40 changes: 21 additions & 19 deletions controller/CategoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ const { Kafka } = require('kafkajs');
const Category = require('../schemas/Category');
const Hashtag = require('../schemas/Hashtag');

const update = { $setOnInsert : { count : 0 } };
const options = { upsert : true };
exports.test = async (req, res) => {
const { categoryName, temperature } = req.body;
const query = { categoryName : categoryName, temperature : temperature };

const insertOrUpdate = await Category.updateOne(query, update, options);
const updateCount = { $inc : { count : 1 } };
const again = await Category.updateOne(query, updateCount);
res.send({ createtest, again });
}

exports.getHashtagsInfo = async (req, res) => {
try {
const result = await Hashtag.find().sort('-count');
Expand Down Expand Up @@ -97,6 +85,8 @@ const kafka = new Kafka({
})

const consumer = kafka.consumer({ groupId : 'cate'});
const update = { $setOnInsert : { count : 0 } };
const options = { upsert : true };

exports.initKafka = async () => {
await consumer.connect();
Expand All @@ -107,18 +97,30 @@ exports.initKafka = async () => {
const replace = message.value.toString().replaceAll('"', '');
//카테고리
if(topic === "category") {
console.log('-----------------------카테고리---------------------');
const temperature = replace.split('/')[0];
console.log("temperature : ", temperature);
const categories = replace.split('/');
categories.forEach((val) =>{
console.log(val);
})
for(let i=1 ; i<categories.length ; i++) {
console.log(categories[i]);
const query = { categoryName : categories[i], temperature : temperature };

const insertOrUpdate = await Category.updateOne(query, update, options);
const updateCount = { $inc : { count : 1 } };
const again = await Category.updateOne(query, updateCount);
}
} else { //해시태그
console.log('----------------------해시태그----------------------');
const hashtags = replace.split('/');
hashtags.forEach((val) => {
console.log(val);
})
for(let i=0 ; i<hashtags.length ; i++) {
const query = { tagName : hashtags[i] };

const insertOrUpdate = await Hashtag.updateOne(query, update, options);
const updateCount = { $inc : { count : 1 } };
const again = await Hashtag.updateOne(query, updateCount);
}
}

}
})
}

1 change: 0 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ router.get('/hashtags', CategoryController.getHashtagsInfo);
router.get('/hashtag', CategoryController.getHashtagInfo);
router.get('/typing', CategoryController.searchByTyping);
router.get('/tops', CategoryController.getTop5);
router.post('/test', CategoryController.test);

module.exports = router;

0 comments on commit b3a11ed

Please sign in to comment.