Step 1 migration: add API service and dedicated PostgreSQL
This commit is contained in:
@@ -1,2 +1,10 @@
|
|||||||
# Mets 7000 si le port est libre, sinon garde 7001
|
# Mets 7000 si le port est libre, sinon garde 7001
|
||||||
APP_PORT=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
|
||||||
|
|||||||
13
README.md
13
README.md
@@ -28,6 +28,8 @@ Centraliser ta collection dans une interface rapide a utiliser, evolutive, et fa
|
|||||||
|
|
||||||
- Frontend: HTML, CSS, JavaScript vanilla
|
- Frontend: HTML, CSS, JavaScript vanilla
|
||||||
- Serveur web local: Nginx (conteneur Docker)
|
- 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
|
- Orchestration: Docker Compose
|
||||||
|
|
||||||
## Structure du projet
|
## Structure du projet
|
||||||
@@ -38,6 +40,9 @@ Centraliser ta collection dans une interface rapide a utiliser, evolutive, et fa
|
|||||||
|- styles.css
|
|- styles.css
|
||||||
|- app.js
|
|- app.js
|
||||||
|- Dockerfile
|
|- Dockerfile
|
||||||
|
|- api/
|
||||||
|
| |- Dockerfile
|
||||||
|
| |- server.js
|
||||||
|- docker-compose.yml
|
|- docker-compose.yml
|
||||||
|- .dockerignore
|
|- .dockerignore
|
||||||
|- .env.example
|
|- .env.example
|
||||||
@@ -57,6 +62,10 @@ Par defaut:
|
|||||||
|
|
||||||
```env
|
```env
|
||||||
APP_PORT=7001
|
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.
|
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)
|
- [http://localhost:7001](http://localhost:7001) (par defaut)
|
||||||
- ou [http://localhost:7000](http://localhost:7000) si `APP_PORT=7000`
|
- 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
|
### 4) Arreter
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
7
api/Dockerfile
Normal file
7
api/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
FROM node:20-alpine
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY api/server.js ./server.js
|
||||||
|
|
||||||
|
EXPOSE 3001
|
||||||
|
CMD ["node", "server.js"]
|
||||||
32
api/server.js
Normal file
32
api/server.js
Normal file
@@ -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}`);
|
||||||
|
});
|
||||||
@@ -1,7 +1,38 @@
|
|||||||
services:
|
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:
|
video-games-app:
|
||||||
build: .
|
build: .
|
||||||
container_name: video-games-app
|
container_name: video-games-app
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- video-games-api
|
||||||
ports:
|
ports:
|
||||||
- "${APP_PORT:-7001}:80"
|
- "${APP_PORT:-7001}:80"
|
||||||
restart: unless-stopped
|
|
||||||
|
volumes:
|
||||||
|
video_games_data:
|
||||||
|
|||||||
Reference in New Issue
Block a user