Express + cliente RMQ asociado a casoso de uso
This commit is contained in:
@@ -1,50 +1,35 @@
|
||||
import express from "express"
|
||||
import cors from 'cors';
|
||||
import { simRoutes } from "infrastructure/simRoutes.http"
|
||||
import { rabbitmqEventBus } from '#config/eventBusConfig';
|
||||
import { env } from "#config/env"
|
||||
import { SimEvents } from "#shared/domain/SimEvents"
|
||||
import { RabbitMQEventBus, RMQConnectionParams } from "#shared/infrastructure/RabbitMQEventBus"
|
||||
|
||||
const rmqUser = env.RABBITMQ_USER
|
||||
const rmqPass = env.RABBITMQ_PASSWORD
|
||||
const rmqHost = env.RABBITMQ_HOST
|
||||
const rmqPort = Number(env.RABBITMQ_PORT)
|
||||
const rmqSecure = false
|
||||
const rmqVhost = env.RABBITMQ_VHOST
|
||||
const PORT = env.API_PORT
|
||||
const HOSTNAME = "0.0.0.0"
|
||||
const app = express()
|
||||
|
||||
|
||||
|
||||
async function test() {
|
||||
const connOptions = <RMQConnectionParams>{
|
||||
username: rmqUser,
|
||||
password: rmqPass,
|
||||
vhost: rmqVhost,
|
||||
hostname: rmqHost,
|
||||
port: rmqPort,
|
||||
secure: rmqSecure,
|
||||
}
|
||||
|
||||
const event = <SimEvents.activation>{
|
||||
key: "sim.activation",
|
||||
payload: {
|
||||
iccid: "1234"
|
||||
},
|
||||
options: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const rmqCli = new RabbitMQEventBus({
|
||||
connectionParams: connOptions
|
||||
rabbitmqEventBus.connect()
|
||||
.then(e => {
|
||||
console.log("[o] Cliente RMQ iniciado")
|
||||
})
|
||||
.catch(e => {
|
||||
console.error("[!] El cliente RMQ no se ha podido iniciar", e)
|
||||
})
|
||||
|
||||
await rmqCli.connect()
|
||||
|
||||
console.log("publicando", event)
|
||||
rmqCli.publish([event])
|
||||
.then(e => {
|
||||
console.log("Mensaje publicado", e)
|
||||
})
|
||||
.catch(err => console.error)
|
||||
}
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
|
||||
test()
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.use("/sim", simRoutes)
|
||||
|
||||
app.get("/health", (req, res) => {
|
||||
res.status(200).json({ status: "ok" })
|
||||
})
|
||||
|
||||
app.listen(PORT, HOSTNAME, () => {
|
||||
console.log("[o] Servidor iniciado en el puerto %d", PORT)
|
||||
})
|
||||
export default {}
|
||||
|
||||
Reference in New Issue
Block a user