diff --git a/backendV2/routes/api/api.route.js b/backendV2/routes/api/api.route.js index fc638d8..53bd852 100644 --- a/backendV2/routes/api/api.route.js +++ b/backendV2/routes/api/api.route.js @@ -1,5 +1,6 @@ import express from "express"; import { authenticate } from "../../services/authentication.js"; +import { checkIfServiceIsActive } from "../../services/functions.js"; const router = express.Router(); import dotenv from "dotenv"; dotenv.config(); @@ -39,6 +40,7 @@ router.post("/change-state/:key/:itemId", authenticate, async (req, res) => { router.get( "/get-loan-by-code/:key/:loan_code", authenticate, + checkIfServiceIsActive("Loan Service"), async (req, res) => { const loan_code = req.params.loan_code; const result = await getLoanByCodeV2(loan_code); @@ -54,6 +56,7 @@ router.get( router.post( "/set-return-date/:key/:loan_code", authenticate, + checkIfServiceIsActive("Loan Service"), async (req, res) => { const loanCode = req.params.loan_code; const result = await setReturnDateV2(loanCode); @@ -69,6 +72,7 @@ router.post( router.post( "/set-take-date/:key/:loan_code", authenticate, + checkIfServiceIsActive("Loan Service"), async (req, res) => { const loanCode = req.params.loan_code; const result = await setTakeDateV2(loanCode); diff --git a/backendV2/services/authentication.js b/backendV2/services/authentication.js index 4fadd38..feb8ed8 100644 --- a/backendV2/services/authentication.js +++ b/backendV2/services/authentication.js @@ -1,6 +1,7 @@ import { SignJWT, jwtVerify } from "jose"; import env from "dotenv"; import { verifyAPIKeyDB } from "./database.js"; +import { checkIfServiceIsActive2 } from "./functions.js"; env.config(); const secretKey = process.env.SECRET_KEY; @@ -45,6 +46,13 @@ export async function authenticate(req, res, next) { const apiKey = req.params.key; if (authHeader) { + const serviceActive = await checkIfServiceIsActive2("User Frontend"); + if (!serviceActive) { + return res + .status(503) + .json({ message: "User Frontend is currently unavailable." }); + } + const parts = authHeader.split(" "); const scheme = parts[0]; const token = parts[1]; @@ -61,6 +69,13 @@ export async function authenticate(req, res, next) { return res.status(403).json({ message: "Present token invalid" }); // present token invalid } } else if (apiKey) { + const serviceActive = await checkIfServiceIsActive2("API"); + if (!serviceActive) { + return res + .status(503) + .json({ message: "API Service is currently unavailable." }); + } + try { await verifyAPIKey(apiKey); return next();