File size: 2,131 Bytes
45fa65d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 7860;
// URL de ce Space (sera configurée via variable d'environnement ou détectée)
const SPACE_URL = process.env.SPACE_URL;
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<title>Persistent Space Keep-Alive</title>
<style>
body { font-family: sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background-color: #f0f2f5; }
.card { background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }
h1 { color: #2d3436; }
p { color: #636e72; }
.status { font-weight: bold; color: #00b894; }
</style>
</head>
<body>
<div class="card">
<h1>Space Persistant Actif</h1>
<p>Ce Space est configuré pour rester éveillé.</p>
<p>Statut: <span class="status">En ligne 24/7</span></p>
<hr>
<p><small>Dernier auto-ping: ${new Date().toISOString()}</small></p>
</div>
</body>
</html>
`);
});
app.get('/ping', (req, res) => {
console.log('Ping reçu à:', new Date().toISOString());
res.status(200).send('pong');
});
app.listen(PORT, () => {
console.log(`Serveur démarré sur le port ${PORT}`);
// Mécanisme de Keep-Alive interne (Auto-ping toutes les 5 minutes)
if (SPACE_URL) {
setInterval(async () => {
try {
console.log(`Auto-ping vers ${SPACE_URL}/ping...`);
await axios.get(`${SPACE_URL}/ping`);
} catch (error) {
console.error('Erreur lors de l\'auto-ping:', error.message);
}
}, 300000); // 5 minutes
} else {
console.log("SPACE_URL non définie. L'auto-ping interne est désactivé.");
}
});
|