Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a development mode #7

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ tab_width = 2
indent_size = 2
tab_width = 2

[package-lock.json]
indent_size = 2
tab_width = 2

[package.json]
indent_size = 2
tab_width = 2

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ tdm-fe/.next/
.gitlab-ci.yml
helm/*
tdm-be/environment-recette.ts
.idea
.idea
tdm-be/uploads/*
!tdm-be/uploads/.keep
tdm-be/dynamic-config.json
tdm-be/logs/*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ FROM node:18.19-bullseye-slim AS application
# Add ezmaster config file
RUN echo '{ \
"httpPort": 3000, \
"configPath": "/app/config/default.json", \
"configPath": "/app/config/production.json", \
"dataPath": "/app" \
}' > /etc/ezmaster.json

Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ install:
cd ../tdm-fe && \
npm install && \
cd ..


run-dev:
docker compose -f docker-compose.dev.yml up --force-recreate

update-front-api:
cd tdm-be && \
npm run swagger-autogen && \
cd ../tdm-fe && \
npm run generate-api && \
cd ..
44 changes: 44 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: "3.5"

services:
tdm-factory-backend:
container_name: tdm-factory-dev-backend
image: node:18.19-bullseye-slim
environment:
NODE_ENV: development
ports:
- "3000:3000"
volumes:
- ./tdm-be:/app
working_dir: /app
command: npm run dev
networks:
- tdm-factory-network

tdm-factory-frontend:
container_name: tdm-factory-dev-frontend
image: node:18.19-bullseye-slim
environment:
NODE_ENV: development
ports:
- "8080:8080"
volumes:
- ./tdm-fe:/app
working_dir: /app
command: npm run dev
networks:
- tdm-factory-network

tdm-factory-maildev:
container_name: tdm-factory-dev-maildev
image: maildev/maildev
ports:
- "1025:1025"
- "1080:1080"
networks:
- tdm-factory-network

networks:
tdm-factory-network:
driver: bridge
name: tdm-factory-network
39 changes: 7 additions & 32 deletions tdm-be/config/default.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
{
"port": 3000,
"password": "",
"hosts": {
"internal": {
"host": "",
"isHttps": false
},
"external": {
"host": "",
"isHttps": false
"port": 3000,
"password": "",
"fileFolder": "uploads/",
"cron": {
"schedule": "0 0 * * *",
"deleteFileOlderThan": 7
}
},
"smtp": {
"host": "maildev",
"port": 1025,
"auth": {
"user": "",
"pass": ""
},
"secure": false,
"tls": {
"ignore": true,
"rejectUnauthorized": false
}
},
"fileFolder": "uploads/",
"dumpFile": "dump.tar.gz",
"finalFile": "final.tar.gz",
"cron": {
"schedule": "0 0 * * *",
"deleteFileOlderThan": 7
}
}
}
21 changes: 21 additions & 0 deletions tdm-be/config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"hosts": {
"internal": {
"host": "{ngrok host}",
"isHttps": true
},
"external": {
"host": "http://localhost:8080",
"isHttps": false
}
},
"smtp": {
"host": "tdm-factory-dev-maildev",
"port": 1025,
"secure": false,
"tls": {
"ignore": true,
"rejectUnauthorized": false
}
}
}
26 changes: 26 additions & 0 deletions tdm-be/config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"password": "{very secure password}",
"hosts": {
"internal": {
"host": "{machine ip or internal host}",
"isHttps": true
},
"external": {
"host": "{external host / revers proxy host}",
"isHttps": false
}
},
"smtp": {
"host": "{mail services host}",
"port": 1025,
"secure": false,
"tls": {
"ignore": true,
"rejectUnauthorized": false
}
},
"cron": {
"schedule": "0 0 * * *",
"deleteFileOlderThan": 7
}
}
39 changes: 39 additions & 0 deletions tdm-be/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tdm-be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cors": "^2.8.5",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"md5": "^2.3.0",
"multer": "^1.4.5-lts.1",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.9",
Expand All @@ -35,6 +36,7 @@
"@types/config": "^3.3.3",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/md5": "^2.3.5",
"@types/multer": "^1.4.11",
"@types/node": "^18.19.15",
"@types/node-cron": "^3.0.11",
Expand Down
8 changes: 4 additions & 4 deletions tdm-be/src/controller/traitment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import environment from '../lib/config';
import logger from '../lib/logger';
import { StatusEnum } from '../model/StatusEnum';
import currentTraitments from '../model/Traitment';
import { addTraitement, getTraitement } from '../model/Traitment';
import { sendEmail } from '../service/email-sender';
import axios from 'axios';
import express from 'express';
Expand Down Expand Up @@ -65,73 +65,73 @@
//Route to start traitment wrapping and then enrichment
router.post(
'/start',
(req: Request<{}, {}, Traitment>, res) => {
(req: Request<unknown, unknown, Traitment>, res) => {
const traitment: Traitment = req.body;
currentTraitments.push(traitment);
addTraitement(traitment);
const url = traitment.wrapper?.url ? traitment.wrapper?.url : '';
const urlEnrichment = traitment.enrichment?.url ? traitment.enrichment?.url : '';
const fileData = fs.readFileSync(`${environment.fileFolder}${traitment.file}`);
traitment.timestamp = new Date().getTime();
traitment.status = StatusEnum.WRAPPER_RUNNING;
res.send({
message: `Enrichissement démarré vous allez recevoir un email.`,
url: `Un suivi est disponible à l\'url https://${req.hostname}?id=${traitment.timestamp}`,
});
sendEmail({
to: req.body.mail,
subject: 'Votre traitement a bien démarré',
text: `Un suivi est disponible à l\'url http://${req.hostname}?id=${traitment.timestamp}`,
}).then(() => {
logger.info('mail envoyer pour début de traitement');
});
axios
.post(url, fileData, {
responseType: 'arraybuffer',
params: {
value: traitment.wrapper.parameters?.find((p) => p.name === 'value')?.value,
},
timeout: 600000,
})
.then(
(wrapperRes) => {
const bin: Buffer = Buffer.from(wrapperRes.data, 'binary');
fs.writeFileSync(environment.dumpFile, bin);
const fd = fs.readFileSync(environment.dumpFile);
logger.info(`Wrapper Done for ${traitment.timestamp}`);
const conf = {
headers: {
'X-Webhook-Success': `${
environment.hosts.internal.isHttps ? 'https' : 'http'
}://${environment.hosts.internal.host}/webhook/success?id=${traitment.timestamp}`,
'X-Webhook-Failure': `${
environment.hosts.internal.isHttps ? 'https' : 'http'
}://${environment.hosts.internal.host}/webhook/failure?id=${traitment.timestamp}`,
},
timeout: 600000,
};
traitment.status = StatusEnum.TRAITMENT_RUNNING;
axios.post(urlEnrichment, fd, conf).then(
(enrichmentRes) => {
traitment.status = StatusEnum.WAITING_WEBHOOK;
logger.info(`Traitment Done for ${traitment.timestamp}`);
traitment.retrieveValue = enrichmentRes.data[0].value;
},
(error) => {
traitment.status = StatusEnum.TRAITMENT_ERROR;
logger.error(`Traitment Error for ${traitment.timestamp}`);
logger.error(error);
res.status(500).send(error.response.data.message);
},
);
},
(error) => {
traitment.status = StatusEnum.WRAPPER_ERROR;
logger.error(`Wrapper Error for ${traitment.timestamp}`);
logger.error(error);
res.status(500).send(error.message);
},
);
},

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.
(error) => {
logger.error(error);
},
Expand Down Expand Up @@ -209,7 +209,7 @@
//Route to retrieve traitment status
router.get('/status', (req, res) => {
const { id } = req.query;
const traitment: Traitment = currentTraitments.filter((t) => t.timestamp + '' === id)[0];
const traitment: Traitment = getTraitement().filter((t) => t.timestamp + '' === id)[0];
let status: StatusEnum = StatusEnum.UNKNOWN;
if (traitment) {
status = traitment.status;
Expand Down
8 changes: 4 additions & 4 deletions tdm-be/src/controller/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logger from '../lib/logger';
import configModel from '../model/Config';
import { StatusEnum } from '../model/StatusEnum';
import currentTraitments from '../model/Traitment';
import { getTraitement, setTraitement } from '../model/Traitment';
import { sendEmail } from '../service/email-sender';
import axios from 'axios';
import express from 'express';
Expand All @@ -19,7 +19,7 @@ router.post(
'/success',
(req, res) => {
const { id } = req.query;
const traitment: Traitment = currentTraitments.filter((t) => t.timestamp + '' === id)[0];
const traitment: Traitment = getTraitement().filter((t) => t.timestamp + '' === id)[0];
const enrichment = config.enrichments.filter(
(enrichment: SwaggerApi) => traitment.enrichment.url.indexOf(enrichment.url) > -1,
)[0];
Expand Down Expand Up @@ -65,10 +65,10 @@ router.post(
'/failure',
(req, res) => {
const { id } = req.query;
const traitment: Traitment = currentTraitments.filter((traitment) => traitment.timestamp === id)[0];
const traitment: Traitment = getTraitement().filter((traitment) => traitment.timestamp === id)[0];
traitment.status = StatusEnum.FINISHED_ERROR;
if (traitment) {
currentTraitments = currentTraitments.filter((t) => t.timestamp !== traitment.timestamp);
setTraitement(getTraitement().filter((t) => t.timestamp !== traitment.timestamp));
// Process the payload as needed
logger.info('Received webhook error:', id);
const mailOptions: EmailOptions = {
Expand Down
14 changes: 12 additions & 2 deletions tdm-be/src/model/Traitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export class Traitment {
status: StatusEnum = StatusEnum.UNKNOWN;
}

const currentTraitments: Traitment[] = [];
let currentTraitments: Traitment[] = [];

export default currentTraitments;
export const addTraitement = (traitement: Traitment) => {
currentTraitments.push(traitement);
};

export const getTraitement = () => {
return currentTraitments;
};

export const setTraitement = (traitements: Traitment[]) => {
currentTraitments = traitements;
};
8 changes: 4 additions & 4 deletions tdm-be/src/swagger/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const swaggerOptions = {
},
],
},
apis: ['./routers/data-enrichment.ts', './routers/data-wrapper.ts', './routers/traitment.ts'], // Specify the file containing your routes
apis: ['./src/controller/data-enrichment.ts', './src/controller/data-wrapper.ts', './src/controller/traitment.ts'], // Specify the file containing your routes
};

const swaggerOptionsConfig = {
Expand All @@ -32,19 +32,19 @@ const swaggerOptionsConfig = {
},
],
},
apis: ['./routers/config.ts'], // Specify the file containing your routes
apis: ['./src/controller/config.ts'], // Specify the file containing your routes
};

const swaggerSpec = swaggerJsdoc(swaggerOptions);

// Write Swagger JSON to a file
const swaggerJson = JSON.stringify(swaggerSpec, null, 2);
fs.writeFileSync('swagger.json', swaggerJson);
fs.writeFileSync(__dirname + '/swagger.json', swaggerJson);

const swaggerSpecConfig = swaggerJsdoc(swaggerOptionsConfig);

// Write Swagger JSON to a file
const swaggerJsonConfig = JSON.stringify(swaggerSpecConfig, null, 2);
fs.writeFileSync('swagger-config.json', swaggerJsonConfig);
fs.writeFileSync(__dirname + '/swagger-config.json', swaggerJsonConfig);

console.log('Swagger JSON file generated successfully');
Empty file added tdm-be/uploads/.keep
Empty file.
Loading
Loading