Skip to content

Commit

Permalink
Sers une page de maintenance si MODE_MAINTENANCE_ACTIF vaut true
Browse files Browse the repository at this point in the history
  • Loading branch information
CadiChris committed Nov 21, 2024
1 parent 5e236d8 commit 82c2823
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions anssi-nis2-api/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ BREVO_API_BASE_URL= # URL de base de l'API Brevo. Exemple : https://api.brevo.co

# Filtrage IP
ADRESSES_IP_AUTORISEES= # IP autorisées, séparées par des virgules (si plusieurs). Laisser vide pour désactiver le filtrage

MODE_MAINTENANCE_ACTIF= # true pour activer la page de maintenance. Ne pas valoriser pour désactiver le mode maintenance.
4 changes: 3 additions & 1 deletion anssi-nis2-api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { creeServeur } from "./serveur";
import { DependanceServeur } from "./serveur.types";
import { AdaptateurPersistancePostgres } from "./adaptateurs/adaptateurPersistance.postgres";
import { AdaptateurJournalPostgres } from "./adaptateurs/adaptateurJournal.postgres";
import { AdaptateurCrmBrevo } from "./adaptateurs/adaptateurCrm.brevo";
import { AdaptateurGestionErreurSentry } from "./adaptateurs/adaptateurGestionErreur.sentry";
import { AdaptateurGestionErreurMemoire } from "./adaptateurs/adaptateurGestionErreur.memoire";
import { AdaptateurProtectionRateLimit } from "./adaptateurs/adaptateurProtection.rateLimit";
import { AdaptateurProtectionMemoire } from "./adaptateurs/adaptateurProtection.memoire";
import { AdaptateurEligibiliteCsv } from "./adaptateurs/adaptateurEligibilite.csv";
import { AdaptateurJournalPostgres } from "./adaptateurs/adaptateurJournal.postgres";
import { middleware } from "./middleware";

const portEcoute = Number(process.env.PORT) || 3000;

Expand All @@ -30,6 +31,7 @@ const dependances: DependanceServeur = {
? new AdaptateurProtectionRateLimit()
: new AdaptateurProtectionMemoire(),
adaptateurEligibilite: new AdaptateurEligibiliteCsv(),
middleware: middleware(),
};

creeServeur(portEcoute, dependances).then((serveur) =>
Expand Down
16 changes: 16 additions & 0 deletions anssi-nis2-api/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Middleware } from "./serveur.types";
import { RequestHandler } from "express";
import * as path from "node:path";

export function middleware(): Middleware {
const modeMaintenance: RequestHandler = (_req, reponse, suite) => {
const estEnModeMaintenance = process.env.MODE_MAINTENANCE_ACTIF === "true";
if (!estEnModeMaintenance) return suite();

reponse.sendFile(
path.join(__dirname, "../../../statique/maintenance.html"),
);
};

return { modeMaintenance };
}
2 changes: 2 additions & 0 deletions anssi-nis2-api/src/serveur.express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export async function creeServeurExpress(
app.disable("x-powered-by");
activeFiltrageIp(app);

app.use(dependances.middleware.modeMaintenance);

app.use(express.static(appReact().cheminDuBuild()));
app.use("/statique", express.static(join(__dirname, "../../../statique")));
app.use(express.json());
Expand Down
6 changes: 6 additions & 0 deletions anssi-nis2-api/src/serveur.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestHandler } from "express";
import { AdaptateurPersistance } from "./adaptateurs/adaptateurPersistance";
import { AdaptateurJournal } from "./adaptateurs/adaptateurJournal";
import { AdaptateurCrm } from "./adaptateurs/adaptateurCrm";
Expand All @@ -10,11 +11,16 @@ export type ServeurMonEspaceNIS2 = {
arrete: () => void;
};

export interface Middleware {
modeMaintenance: RequestHandler;
}

export type DependanceServeur = {
adaptateurCrm: AdaptateurCrm;
adaptateurGestionErreur: AdaptateurGestionErreur;
adaptateurJournal: AdaptateurJournal;
adaptateurPersistance: AdaptateurPersistance;
adaptateurProtection: AdaptateurProtection;
adaptateurEligibilite: AdaptateurEligibilite;
middleware: Middleware;
};
2 changes: 2 additions & 0 deletions anssi-nis2-api/src/test/routes/routesApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AdaptateurProtectionMemoire } from "../../adaptateurs/adaptateurProtect
import { creeServeurExpress } from "../../serveur.express";
import { Express } from "express";
import { AdaptateurEligibiliteHardCode } from "../../adaptateurs/adaptateurEligibilite.hardCode";
import { middlewareFantaisie } from "../utilitaires/middlewareFantaisie";

describe("Le routeur '/api/", () => {
let serveur: { app: Express };
Expand All @@ -33,6 +34,7 @@ describe("Le routeur '/api/", () => {
adaptateurGestionErreur: new AdaptateurGestionErreurMemoire(),
adaptateurProtection: new AdaptateurProtectionMemoire(),
adaptateurEligibilite: new AdaptateurEligibiliteHardCode(),
middleware: middlewareFantaisie(),
});
});

Expand Down
5 changes: 5 additions & 0 deletions anssi-nis2-api/src/test/utilitaires/middlewareFantaisie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Middleware } from "../../serveur.types";

export const middlewareFantaisie = (): Middleware => ({
modeMaintenance: (_req, _res, suite) => suite(),
});
36 changes: 36 additions & 0 deletions anssi-nis2-api/statique/maintenance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<html lang="fr">
<head>
<meta charset="UTF-8" />
<link
rel="preload"
href="/dsfr/fonts/Marianne-Regular.woff2"
as="font"
crossorigin="anonymous"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MonEspaceNIS2 - MAINTENANCE</title>
</head>

<body style="margin: 0;">
<div
style="
font-family: Marianne, arial, sans-serif;
background: #272771;
color: white;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
padding: 0;
flex: 1;
flex-direction: column;
height: 100%;
font-size: 2rem;
text-align: center;
"
>
MonEspaceNIS2<br/>
est actuellement en maintenance.
</div>
</body>
</html>

0 comments on commit 82c2823

Please sign in to comment.