forked from Vesting-Vault/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (83 loc) · 2.35 KB
/
index.js
File metadata and controls
95 lines (83 loc) · 2.35 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express');
const cors = require('cors');
require('dotenv').config();
const { Client } = require('pg');
const { Server } = require('stellar-sdk');
const { validateNetworkOnStartup } = require('./backend/src/jobs/networkValidation');
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
// Health check endpoint
app.get('/health', async (req, res) => {
const health = {
status: 'ok',
timestamp: new Date().toISOString(),
services: {
database: 'unknown',
stellar: 'unknown'
}
};
let allHealthy = true;
// Check database connection
try {
const client = new Client({
connectionString: process.env.DATABASE_URL || {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
}
});
await client.connect();
await client.query('SELECT 1');
await client.end();
health.services.database = 'healthy';
} catch (error) {
health.services.database = 'unhealthy';
health.database_error = error.message;
allHealthy = false;
}
// Check Stellar RPC connection
try {
if (!process.env.STELLAR_RPC_URL) {
throw new Error('STELLAR_RPC_URL is not configured');
}
const stellarServer = new Server(process.env.STELLAR_RPC_URL);
await stellarServer.root();
health.services.stellar = 'healthy';
} catch (error) {
health.services.stellar = 'unhealthy';
health.stellar_error = error.message;
allHealthy = false;
}
if (allHealthy) {
res.status(200).json(health);
} else {
health.status = 'degraded';
res.status(503).json(health);
}
});
// Middleware
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
const dbStatus = dbManager.getStatus();
res.json({
project: 'Vesting Vault',
status: 'Tracking Locked Tokens',
contract: process.env.VAULT_CONTRACT_ADDRESS
contract: 'CD5QF6KBAURVUNZR2EVBJISWSEYGDGEEYVH2XYJJADKT7KFOXTTIXLHU',
database: dbStatus
});
});
async function startServer() {
try {
await validateNetworkOnStartup();
app.listen(port, () => console.log(`Vesting API running on port ${port}`));
} catch (error) {
console.error('\n❌ Fatal Startup Error:', error.message, '\n');
process.exit(1);
}
}
startServer();