ncantu dde1ccbb07 fix: Improve .env loading and add backup/startup scripts
**Motivations:**
- Fix 401 error on anchorage API due to .env not being loaded correctly
- Fix 502 error on dashboard due to service not running
- Add backup script for mining keys and wallet descriptors
- Improve service management with startup scripts and systemd services

**Root causes:**
- dotenv.config() was called without explicit path, causing .env to be loaded from wrong directory
- Services were not started automatically, causing 502 errors
- No backup mechanism for critical keys and wallet data

**Correctifs:**
- Improved .env loading in api-anchorage/src/server.js with explicit path
- Improved .env loading in signet-dashboard/src/server.js with explicit path
- Added backups/ directory to .gitignore to prevent committing sensitive data
- Created export-backup.sh script for backing up mining keys and wallet descriptors

**Evolutions:**
- Added api-anchorage/start.sh script for proper service startup
- Added api-anchorage/anchorage-api.service systemd service file
- Added fixKnowledge/api-anchorage-401-error.md documentation
- Added fixKnowledge/dashboard-502-error.md documentation
- Updated mempool submodule

**Pages affectées:**
- .gitignore (added backups/)
- api-anchorage/src/server.js (improved .env loading)
- api-anchorage/start.sh (new)
- api-anchorage/anchorage-api.service (new)
- signet-dashboard/src/server.js (improved .env loading)
- export-backup.sh (new)
- fixKnowledge/api-anchorage-401-error.md (new)
- fixKnowledge/dashboard-502-error.md (new)
- mempool (submodule updated)
2026-01-24 03:17:48 +01:00

133 lines
3.6 KiB
JavaScript

#!/usr/bin/env node
/**
* API d'Ancrage Bitcoin Signet
*
* Cette API permet d'ancrer des documents sur la blockchain Bitcoin Signet
* en créant des transactions qui incluent les hash des documents.
*
* Port: 3010
* Domaine: certificator.4nkweb.com
*/
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import { BitcoinRPC } from './bitcoin-rpc.js';
import { anchorRouter } from './routes/anchor.js';
import { healthRouter } from './routes/health.js';
import { logger } from './logger.js';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Charger les variables d'environnement depuis le répertoire racine du projet api-anchorage
// Cela garantit que le .env est chargé même si le script est exécuté depuis un autre répertoire
// Le .env est dans api-anchorage/.env, et server.js est dans api-anchorage/src/
const envPath = join(__dirname, '../.env');
dotenv.config({ path: envPath });
const app = express();
const PORT = process.env.API_PORT || 3010;
const HOST = process.env.API_HOST || '0.0.0.0';
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Middleware de logging
app.use((req, res, next) => {
logger.info(`${req.method} ${req.path}`, {
ip: req.ip,
userAgent: req.get('user-agent'),
});
next();
});
// Middleware d'authentification API Key
app.use((req, res, next) => {
// Exclure /health de l'authentification
if (req.path === '/health' || req.path === '/') {
return next();
}
const apiKey = req.headers['x-api-key'];
// Filtrer les clés vides pour éviter qu'une chaîne vide soit acceptée
const validKeys = process.env.API_KEYS?.split(',').map(k => k.trim()).filter(k => k.length > 0) || [];
// Vérifier que la clé API est présente, non vide, et dans la liste des clés valides
if (!apiKey || apiKey.trim().length === 0 || !validKeys.includes(apiKey.trim())) {
logger.warn('Unauthorized API access attempt', { ip: req.ip, path: req.path });
return res.status(401).json({
error: 'Unauthorized',
message: 'Invalid or missing API key',
});
}
next();
});
// Routes
app.use('/health', healthRouter);
app.use('/api/anchor', anchorRouter);
// Route racine
app.get('/', (req, res) => {
res.json({
service: 'bitcoin-signet-anchor-api',
version: '1.0.0',
endpoints: {
health: '/health',
anchor: '/api/anchor/document',
verify: '/api/anchor/verify',
},
});
});
// Gestion des erreurs
app.use((err, req, res, next) => {
logger.error('Unhandled error', { error: err.message, stack: err.stack });
res.status(500).json({
error: 'Internal Server Error',
message: process.env.NODE_ENV === 'development' ? err.message : 'An error occurred',
});
});
// Gestion des routes non trouvées
app.use((req, res) => {
res.status(404).json({
error: 'Not Found',
message: `Route ${req.method} ${req.path} not found`,
});
});
// Démarrage du serveur
const server = app.listen(PORT, HOST, () => {
logger.info(`API d'ancrage Bitcoin Signet démarrée`, {
host: HOST,
port: PORT,
environment: process.env.NODE_ENV || 'production',
});
});
// Gestion de l'arrêt propre
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});