implementig http approach
This commit is contained in:
@@ -8,9 +8,9 @@ const categoryConverter = new CategoryConverterUseCase({categoriesMapping});
|
||||
|
||||
module.exports = async function app(domain){
|
||||
const category = await getCategory.execute(domain)
|
||||
|
||||
console.log(category)
|
||||
const categoryConverted = await categoryConverter.execute(category)
|
||||
|
||||
console.log(categoryConverted)
|
||||
return categoryConverted
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,14 +2,21 @@ require('dotenv').config()
|
||||
|
||||
const dgram = require('dgram');
|
||||
const app = require('./app')
|
||||
const bodyParser = require('body-parser')
|
||||
const cron = require('./cron')
|
||||
const express = require('express')
|
||||
|
||||
const httpServer = express()
|
||||
httpServer.use(bodyParser.json()) // for parsing application/json
|
||||
httpServer.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
|
||||
cron()
|
||||
|
||||
const PORT = process.env.PORT
|
||||
const UDP_PORT = process.env.UDP_PORT
|
||||
const HTTP_PORT = process.env.HTTP_PORT
|
||||
|
||||
const server = dgram.createSocket('udp4');
|
||||
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.log(`Server error:\n${err.stack}`);
|
||||
server.close();
|
||||
@@ -35,10 +42,34 @@ server.on('message', async (msg, rinfo) => {
|
||||
|
||||
server.on('listening', () => {
|
||||
const address = server.address();
|
||||
console.log(`Server listening ${address.address}:${address.port}`);
|
||||
console.log(`UDP Server listening ${address.address}:${address.port}`);
|
||||
});
|
||||
|
||||
server.bind(PORT);
|
||||
server.bind(UDP_PORT);
|
||||
|
||||
httpServer.post('/', async (req, res) => {
|
||||
try {
|
||||
const { fqdn } = req.body
|
||||
|
||||
const categories = await app(fqdn)
|
||||
|
||||
let result = {}
|
||||
|
||||
if (categories)
|
||||
result = {
|
||||
result: categories
|
||||
}
|
||||
|
||||
res.status(200).json(JSON.stringify(result))
|
||||
} catch (err) {
|
||||
res.status(500).json(err)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
httpServer.listen(HTTP_PORT, () => {
|
||||
console.log('HTTP server listening 3333')
|
||||
})
|
||||
|
||||
// Response
|
||||
// {"result":[10009]}
|
||||
Reference in New Issue
Block a user