From f9831a0ca1f5ce717f9644b75cb82c7f9e315706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=C3=AFssata=20TRAORE?= Date: Tue, 30 Dec 2025 20:02:39 +0000 Subject: [PATCH 1/2] Add health (#7) --- Dockerfile | 23 ++++++----------------- src/config/rabbitmq.ts | 4 ++-- src/index.ts | 14 ++++++++++++++ src/routes/health.ts | 13 +++++++++++++ src/server.ts | 8 +++++++- 5 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 src/routes/health.ts diff --git a/Dockerfile b/Dockerfile index 915e565..6ba496b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,37 +1,26 @@ # ============================= -# Stage 1 : Build +# Stage 1 : Build # ============================= -FROM node:18-alpine AS builder +FROM node:20-alpine AS builder WORKDIR /app -# Copier package.json et lock file COPY package*.json ./ +RUN npm ci -# Installer toutes les dépendances -RUN npm install - -# Copier tout le code source COPY . . - -# Compiler Typescript -> JavaScript RUN npm run build # ============================= -# Stage 2 : Runtime -# ============================ -FROM node:18-alpine AS runner +# Stage 2 : Runtime +# ============================= +FROM node:20-alpine WORKDIR /app ENV NODE_ENV=production -# Copier uniquement ce qui sert à l'exécution COPY --from=builder /app/package*.json ./ COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/dist ./dist -# Exposer le port EXPOSE 3000 - -# Lancer la version compilée CMD ["node", "dist/server.js"] - diff --git a/src/config/rabbitmq.ts b/src/config/rabbitmq.ts index 535825e..d152ea7 100644 --- a/src/config/rabbitmq.ts +++ b/src/config/rabbitmq.ts @@ -2,7 +2,7 @@ import * as amqp from "amqplib"; import type { Connection, Channel } from "amqplib"; -let connection: amqp.Connection | null = null; +//let connection: amqp.Connection | null = null; let channel: Channel | null= null; /** Variables standardisées */ @@ -30,7 +30,7 @@ export async function ensureChannel(): Promise { conn.on("close", () => { console.error("RabbitMQ fermé – reconnexion..."); // channel = null; - // connection = null; + //connection = null; setTimeout(ensureChannel, 3000); }); diff --git a/src/index.ts b/src/index.ts index 049bb1f..604bd35 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,3 +13,17 @@ router.post("/otp/generate", generateOtp); router.post("/otp/verify", verifyOtp); export default router; + +require("dotenv").config(); +const express = require("express"); +const healthRoute = require("../routes/health"); + +const app = express(); +const PORT = process.env.SERVICE_PORT || 8000; + +app.use(express.json()); +app.use("/", healthRoute); + +app.listen(PORT, () => { + console.log(`🚀 Service running on port ${PORT}`); +}); \ No newline at end of file diff --git a/src/routes/health.ts b/src/routes/health.ts new file mode 100644 index 0000000..91f8f14 --- /dev/null +++ b/src/routes/health.ts @@ -0,0 +1,13 @@ +const express = require("express"); +import type { Request, Response } from "express"; +const router = express.Router(); + +interface HealthResponse { + status: string; +} + +router.get("/health", (req: Request, res: Response) => { + res.status(200).json({ status: "OK" }); +}); + +module.exports = router; diff --git a/src/server.ts b/src/server.ts index 4daecc0..1e25030 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,9 +6,13 @@ import { startConsumer } from "./messaging/consumer"; import { ensureChannel } from "./config/rabbitmq"; import { startExternalNotificationConsumer } from "./messaging/externalConsumer"; +const express = require("express"); +const healthRoute = require("../routes/health"); + + dotenv.config(); -const PORT = process.env.PORT ? Number(process.env.PORT) : 3000; +const PORT = process.env.SERVICE_PORT ? Number(process.env.SERVICE_PORT) : 8000; AppDataSource.initialize() @@ -22,6 +26,8 @@ AppDataSource.initialize() }) .catch((err) => console.error("Erreur de connexion :", err)); + app.use(express.json()); +app.use("/", healthRoute); /* async function startServer() { console.log("⏳ Initialisation du service de notifications..."); From aa6ff3201cca98bce699ea6515a186b49579fe9a Mon Sep 17 00:00:00 2001 From: ricashtrans Date: Wed, 31 Dec 2025 11:43:44 +0000 Subject: [PATCH 2/2] Update docker-build.yml --- .github/workflows/docker-build.yml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index 1f0d290..284e342 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -2,12 +2,17 @@ name: Build & Push Notification Service Docker Image on: push: - branches: ["main"] + branches: + - develop + - main permissions: contents: read packages: write +env: + IMAGE_NAME: ghcr.io/ricash-org/notification-service + jobs: build: runs-on: ubuntu-latest @@ -29,11 +34,13 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build & Push + - name: Build & Push Docker image uses: docker/build-push-action@v5 with: context: . - push: true - platforms: linux/amd64,linux/arm64 file: ./Dockerfile - tags: ghcr.io/ricash-org/notification-service:latest + platforms: linux/amd64,linux/arm64 + push: true + tags: | + ${{ env.IMAGE_NAME }}:${{ github.ref_name }} + ${{ env.IMAGE_NAME }}:${{ github.sha }}