From aae54718625fbcc9eecd6fc858721fba96c7d339 Mon Sep 17 00:00:00 2001 From: Ponte Date: Wed, 11 Feb 2026 14:53:13 +0100 Subject: [PATCH] Step 1 migration: add API service and dedicated PostgreSQL --- .env.example | 8 ++++++++ README.md | 13 +++++++++++++ api/Dockerfile | 7 +++++++ api/server.js | 32 ++++++++++++++++++++++++++++++++ docker-compose.yml | 33 ++++++++++++++++++++++++++++++++- 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 api/Dockerfile create mode 100644 api/server.js diff --git a/.env.example b/.env.example index 1ae2e87..16de408 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,10 @@ # Mets 7000 si le port est libre, sinon garde 7001 APP_PORT=7001 + +# API backend (etape 1 migration) +API_PORT=7002 + +# Base dediee a l'application video games +VG_DB_NAME=video_games +VG_DB_USER=video_games_user +VG_DB_PASSWORD=change_me diff --git a/README.md b/README.md index 436b47d..602eaa8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ Centraliser ta collection dans une interface rapide a utiliser, evolutive, et fa - Frontend: HTML, CSS, JavaScript vanilla - Serveur web local: Nginx (conteneur Docker) +- Backend API: Node.js (endpoint de sante pour migration progressive) +- Base de donnees: PostgreSQL dediee a l'application - Orchestration: Docker Compose ## Structure du projet @@ -38,6 +40,9 @@ Centraliser ta collection dans une interface rapide a utiliser, evolutive, et fa |- styles.css |- app.js |- Dockerfile +|- api/ +| |- Dockerfile +| |- server.js |- docker-compose.yml |- .dockerignore |- .env.example @@ -57,6 +62,10 @@ Par defaut: ```env APP_PORT=7001 +API_PORT=7002 +VG_DB_NAME=video_games +VG_DB_USER=video_games_user +VG_DB_PASSWORD=change_me ``` Tu peux mettre `7000` si ce port est libre sur ta machine. @@ -72,6 +81,10 @@ docker compose up -d --build - [http://localhost:7001](http://localhost:7001) (par defaut) - ou [http://localhost:7000](http://localhost:7000) si `APP_PORT=7000` +### 3-bis) Verifier l'API (etape 1 migration) + +- [http://localhost:7002/health](http://localhost:7002/health) + ### 4) Arreter ```bash diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..e76f355 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,7 @@ +FROM node:20-alpine + +WORKDIR /app +COPY api/server.js ./server.js + +EXPOSE 3001 +CMD ["node", "server.js"] diff --git a/api/server.js b/api/server.js new file mode 100644 index 0000000..93d30b0 --- /dev/null +++ b/api/server.js @@ -0,0 +1,32 @@ +const http = require("http"); + +const port = Number(process.env.API_INTERNAL_PORT || 3001); +const serviceName = process.env.SERVICE_NAME || "video-games-api"; + +function sendJson(response, statusCode, payload) { + response.writeHead(statusCode, { "Content-Type": "application/json; charset=utf-8" }); + response.end(JSON.stringify(payload)); +} + +const server = http.createServer((request, response) => { + const url = new URL(request.url || "/", `http://${request.headers.host || "localhost"}`); + + if (request.method === "GET" && url.pathname === "/health") { + sendJson(response, 200, { + status: "ok", + service: serviceName, + timestamp: new Date().toISOString(), + }); + return; + } + + sendJson(response, 404, { + status: "not_found", + message: "Route not found", + }); +}); + +server.listen(port, "0.0.0.0", () => { + // Keep startup logs minimal and readable in docker compose logs. + console.log(`${serviceName} listening on port ${port}`); +}); diff --git a/docker-compose.yml b/docker-compose.yml index 5941432..4219635 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,38 @@ services: + video-games-db: + image: postgres:16-alpine + container_name: video-games-db + restart: unless-stopped + environment: + - POSTGRES_DB=${VG_DB_NAME:-video_games} + - POSTGRES_USER=${VG_DB_USER:-video_games_user} + - POSTGRES_PASSWORD=${VG_DB_PASSWORD:-change_me} + volumes: + - video_games_data:/var/lib/postgresql/data + + video-games-api: + build: + context: . + dockerfile: api/Dockerfile + container_name: video-games-api + restart: unless-stopped + depends_on: + - video-games-db + environment: + - SERVICE_NAME=video-games-api + - API_INTERNAL_PORT=3001 + - DATABASE_URL=postgres://${VG_DB_USER:-video_games_user}:${VG_DB_PASSWORD:-change_me}@video-games-db:5432/${VG_DB_NAME:-video_games} + ports: + - "${API_PORT:-7002}:3001" + video-games-app: build: . container_name: video-games-app + restart: unless-stopped + depends_on: + - video-games-api ports: - "${APP_PORT:-7001}:80" - restart: unless-stopped + +volumes: + video_games_data: