2023-08-10 17:55:04 +00:00
|
|
|
import express from "express";
|
2023-08-10 18:36:32 +00:00
|
|
|
import bodyParser from "body-parser";
|
2023-08-11 14:21:24 +00:00
|
|
|
import logger from "./logger.mjs";
|
2023-08-11 14:30:47 +00:00
|
|
|
import pinoHTTP from 'pino-http'
|
2023-08-11 14:52:17 +00:00
|
|
|
import start from "./start.mjs";
|
2023-08-14 23:40:05 +00:00
|
|
|
import handleMention from "./social-interaction/handleMention.mjs";
|
2023-08-16 22:26:54 +00:00
|
|
|
import { db } from "./database-calls/db.mjs";
|
2023-08-10 17:55:04 +00:00
|
|
|
const app = express()
|
|
|
|
const port = 4000
|
2023-08-10 18:36:32 +00:00
|
|
|
app.use(bodyParser.json())
|
2023-08-11 14:30:47 +00:00
|
|
|
app.use(
|
|
|
|
pinoHTTP({
|
|
|
|
logger,
|
|
|
|
})
|
|
|
|
)
|
2023-08-14 23:40:05 +00:00
|
|
|
app.post('/api', async (req,res) => {
|
2023-08-13 13:00:24 +00:00
|
|
|
logger.info({body:req.body.body},"webhook received!")
|
2023-08-10 18:36:32 +00:00
|
|
|
res.sendStatus(200)
|
2023-08-16 22:26:54 +00:00
|
|
|
const result = await handleMention(db,req.body.body)
|
2023-08-14 23:40:05 +00:00
|
|
|
logger.info(`handleMention returned ${result.code}`)
|
2023-08-10 17:55:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
2023-08-11 14:52:17 +00:00
|
|
|
logger.trace(`listening on port ${port}`)
|
2023-08-11 14:21:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
|
|
// log the exception
|
|
|
|
logger.fatal(err, 'uncaught exception detected');
|
|
|
|
// shutdown the server gracefully
|
2023-08-11 14:52:17 +00:00
|
|
|
app.close(() => {
|
2023-08-11 14:21:24 +00:00
|
|
|
process.exit(1); // then exit
|
|
|
|
});
|
|
|
|
|
|
|
|
// If a graceful shutdown is not achieved after 1 second,
|
|
|
|
// shut down the process completely
|
|
|
|
setTimeout(() => {
|
|
|
|
process.abort(); // exit immediately and generate a core dump file
|
|
|
|
}, 1000).unref()
|
|
|
|
process.exit(1);
|
2023-08-11 14:52:17 +00:00
|
|
|
});
|
|
|
|
|
2023-08-16 22:26:54 +00:00
|
|
|
start(db)
|