From: Steph Ponzo Date: Sun, 1 Mar 2026 18:05:29 +0000 (+0100) Subject: feat(price): server.js + auto-refresh prix toutes les 5 minutes X-Git-Url: https://git.digitality.be/?a=commitdiff_plain;h=7bd94bd942e1e19b755ceb9c53aa64f1dc9d7f10;p=pdw25-26 feat(price): server.js + auto-refresh prix toutes les 5 minutes --- diff --git a/Wallette/server/modules/price/autoRefresh.js b/Wallette/server/modules/price/autoRefresh.js new file mode 100644 index 0000000..fe89582 --- /dev/null +++ b/Wallette/server/modules/price/autoRefresh.js @@ -0,0 +1,25 @@ +const PAIR_CODE = process.env.PRICE_PAIR_CODE || "BTC/EUR"; +const REFRESH_EVERY_MS = Number(process.env.PRICE_REFRESH_EVERY_MS || 300000); +const PORT = process.env.PRICE_PORT || 3001; +const BASE_URL = `http://127.0.0.1:${PORT}`; + +let refreshing = false; + +async function refreshPrice() { + if (refreshing) return; + refreshing = true; + try { + const url = `${BASE_URL}/api/price/refresh?pair=${encodeURIComponent(PAIR_CODE)}`; + const res = await fetch(url, { method: "POST" }); + const data = await res.json().catch(() => null); + console.log(`[auto-refresh] OK ${PAIR_CODE}`, data?.data?.current_price ?? ""); + } catch (err) { + console.log("[auto-refresh] exception:", err?.message || err); + } finally { + refreshing = false; + } +} + +refreshPrice(); +setInterval(refreshPrice, REFRESH_EVERY_MS); +console.log(`[auto-refresh] Prix ${PAIR_CODE} toutes les ${REFRESH_EVERY_MS/60000} min`); diff --git a/Wallette/server/modules/price/server.js b/Wallette/server/modules/price/server.js new file mode 100644 index 0000000..566194c --- /dev/null +++ b/Wallette/server/modules/price/server.js @@ -0,0 +1,58 @@ +// ========================================================= +// PRICE SERVICE - Serveur standalone +// ========================================================= +// Ce fichier encapsule le module price +// en microservice Express indépendant sur port 3001. +// +// USAGE : node modules/price/server.js (depuis Wallette/server/) +// PORT : process.env.PRICE_PORT || 3001 +// ========================================================= + +import cors from 'cors'; +import dotenv from 'dotenv'; +import express from 'express'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +// Charger .env depuis Wallette/server/.env +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +dotenv.config({ path: path.resolve(__dirname, '../../.env') }); + +// IMPORTANT : dotenv doit être chargé AVANT l'import du router +// car db.js lit process.env au moment de l'import +const { default: priceApiRouter } = await import('./routes/api.router.js'); + +const PORT = process.env.PRICE_PORT || 3001; + +const app = express(); +app.use(cors()); +app.use(express.json()); +app.use(express.static(path.join(__dirname, 'public'))); + +// ─── Health check (requis par le gateway) ──────────────── +app.get('/health', (req, res) => { + res.json({ ok: true, service: 'price-service', port: PORT }); +}); + +// ─── Routes API price ───────────────────────────────────── +// Le router expose /price/current, /price/history... +// On le monte sur /api pour que le gateway route /api/price/* correctement +app.use('/api', priceApiRouter); + +// ─── 404 ───────────────────────────────────────────────── +app.use((req, res) => { + res.status(404).json({ ok: false, error: { code: 'NOT_FOUND', message: 'Route non trouvée' } }); +}); + +// ─── Démarrage ──────────────────────────────────────────── +app.listen(PORT, () => { + console.log(` + ╔══════════════════════════════════════════╗ + ║ PRICE SERVICE ║ + ║ HTTP : http://localhost:${PORT} ║ + ║ → /health = OK ║ + ║ → /api/price/current?pair= = prix ║ + ║ → /api/price/history?pair= = historique ║ + ╚══════════════════════════════════════════╝`); +});