From 44c5e205938344b4948f8e5f92f53ebf205e94ec Mon Sep 17 00:00:00 2001 From: seaerchin Date: Thu, 4 Apr 2024 13:24:07 +0800 Subject: [PATCH] feat(support): connect db --- support/index.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/support/index.ts b/support/index.ts index 0bf78d014..2b67d1e7d 100644 --- a/support/index.ts +++ b/support/index.ts @@ -2,7 +2,7 @@ import "module-alias/register" import express from "express" -import { infraService } from "@common/index" +import { infraService, sequelize } from "@common/index" import { useSharedMiddleware } from "@common/middleware" import { config } from "@root/config/config" import logger from "@root/logger/logger" @@ -18,11 +18,23 @@ infraService.pollMessages() useSharedMiddleware(app) -// TODO: prefix under infra -// FormSG Backend handler routes app.use("/", v2Router) app.use("/v2/ping", (req, res) => res.status(200).send("Ok")) -app.listen(PORT, () => { - logger.info(`Infra container started on port ${PORT}`) -}) +sequelize + .authenticate() + .then(() => { + logger.info( + "Connection to db has been established successfully on support service." + ) + app.listen(PORT, () => { + logger.info(`Infra container started on port ${PORT}`) + }) + }) + .catch((err) => { + logger.error(`Unable to connect to the database: ${err}`) + + // If we cannot connect to the db, report an error using status code + // And gracefully shut down the application since we can't serve client + process.exit(1) + })