API REST middleware entre C# (ASP.NET Core 8) et les programmes COBOL du projet MainFreem.
┌─────────────┐ HTTP/JSON ┌──────────────────────┐ docker exec ┌─────────────────┐
│ Client C# │ ──────────────────▶│ Middleware API │ ───────────────────▶│ COBOL │
│ (ou autre) │ ◀────────────────── │ ASP.NET Core 8 │ ◀─────────────────── │ GNU COBOL 4.0 │
└─────────────┘ │ :5000 │ │ MainFreem │
└──────────┬───────────┘ └─────────────────┘
│ SQL
▼
┌──────────────────────┐
│ PostgreSQL 16 │
│ (partagée COBOL/C#) │
└──────────────────────┘
- Un client C# (ou n'importe quel client HTTP) appelle l'API REST.
- L'API middleware traite la requête :
- Pour les données : lit/écrit dans PostgreSQL (base partagée avec COBOL).
- Pour les traitements métier COBOL : exécute un
docker execsur le conteneur MainFreem.
- Le programme COBOL reçoit les données via stdin, traite, renvoie via stdout.
- L'API parse la réponse COBOL et retourne du JSON au client.
- .NET 8 SDK
- Docker + Docker Compose
- Image
mainfreem-cobol:latestconstruite depuis le projet MainFreem
cd ../MainFreem
docker build -t mainfreem-cobol .cd MiddleFreeWare
docker-compose up -dCela démarre :
middlefreleware— API C# sur le port 5000mainfreem-cobol— conteneur COBOLmainfreem-postgres— PostgreSQL sur le port 5432
http://localhost:5000
cd src/MiddleFreeWare.Api
dotnet run
# Swagger disponible sur http://localhost:5000| Méthode | URL | Description |
|---|---|---|
GET |
/api/employes |
Liste tous les employés |
GET |
/api/employes/{matricule} |
Récupère un employé |
POST |
/api/employes |
Crée un employé |
PATCH |
/api/employes/{matricule}/salaire |
Met à jour le salaire |
DELETE |
/api/employes/{matricule} |
Supprime un employé |
POST |
/api/employes/{matricule}/paie |
Calcul de paie via COBOL |
| Méthode | URL | Description |
|---|---|---|
GET |
/api/cobol/programs |
Liste les programmes disponibles |
POST |
/api/cobol/run |
Exécute un programme COBOL |
POST |
/api/cobol/compile-run |
Compile et exécute du source COBOL |
| Méthode | URL | Description |
|---|---|---|
GET |
/api/health |
Statut API + PostgreSQL + Docker COBOL |
curl http://localhost:5000/api/employescurl -X POST http://localhost:5000/api/employes/EMP001/paie \
-H "Content-Type: application/json" \
-d '{ "matricule": "EMP001", "anciennete": 5 }'Réponse :
{
"success": true,
"data": {
"matricule": "EMP001",
"nomComplet": "Sophie MARTIN",
"anciennete": 5,
"salaireBrut": 3675.00,
"prime": 175.00,
"charges": 808.50,
"salaireNet": 2866.50,
"tauxPrime": 0.05
}
}curl -X POST http://localhost:5000/api/cobol/run \
-H "Content-Type: application/json" \
-d '{ "programName": "ex01_hello", "inputData": {} }'curl -X POST http://localhost:5000/api/cobol/compile-run \
-H "Content-Type: application/json" \
-d '{
"programName": "test_dynamic",
"source": " IDENTIFICATION DIVISION.\n PROGRAM-ID. TEST.\n PROCEDURE DIVISION.\n DISPLAY \"Hello depuis C# !\"\n STOP RUN."
}'MiddleFreeWare/
├── docker-compose.yml
├── docker/
│ └── Dockerfile.api # Image Docker de l'API
├── src/
│ └── MiddleFreeWare.Api/
│ ├── Controllers/
│ │ ├── CobolController.cs # Exécution COBOL directe
│ │ ├── EmployesController.cs # CRUD + calcul paie
│ │ └── HealthController.cs # Health checks
│ ├── Services/
│ │ ├── CobolRunner.cs # Cœur : docker exec vers COBOL
│ │ ├── CobolProgramService.cs # Gestion programmes COBOL
│ │ └── EmployeService.cs # Logique métier + DB
│ ├── Models/
│ │ └── Models.cs # DTOs et modèles
│ ├── Cobol/
│ │ └── ex15_calcsal_api.cobol # Programme COBOL dédié à l'API
│ ├── Program.cs # Configuration et DI
│ └── appsettings.json
└── tests/
└── MiddleFreeWare.Api.Tests/
└── CobolProgramServiceTests.cs # Tests unitaires
appsettings.json :
{
"ConnectionStrings": {
"PostgreSQL": "Host=postgres;Port=5432;Database=coboldb;Username=cobol;Password=cobol123"
},
"Cobol": {
"ProgramsPath": "/workspace/exercises",
"ExecutionTimeoutSeconds": 30,
"DockerContainerName": "mainfreem-cobol"
}
}cd tests/MiddleFreeWare.Api.Tests
dotnet testLe dossier exercises/ contient 13 exercices progressifs C# avec corrigés.
📄 Exercices — Du niveau Novice à Expert
📄 Corrigés — Solutions complètes et commentées
| Niveau | Ex. | Thèmes |
|---|---|---|
| 🟢 Novice | 1–3 | Premier appel REST, liste employés, POST |
| 🟡 Débutant | 4–6 | CRUD complet, calcul paie COBOL, health check |
| 🟠 Intermédiaire | 7–9 | IHttpClientFactory, COBOL dynamique, batch async |
| 🔴 Avancé | 10–11 | Polly retry/circuit breaker, webhook |
| ⚫ Expert | 12–13 | SDK NuGet, pipeline ETL complet |
- Projet COBOL : ../MainFreem
- Documentation GNU COBOL : https://gnucobol.sourceforge.io/