-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (145 loc) · 5.05 KB
/
Copy pathserver.js
File metadata and controls
172 lines (145 loc) · 5.05 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require('dotenv').config();
const express = require('express');
const binance = require('./src/services/binance');
const signalStore = require('./src/services/signalStore');
const webhookRoutes = require('./src/routes/webhook');
const mcpRoutes = require('./src/routes/mcp');
const testRoutes = require('./src/routes/test');
const logger = require('./src/services/logger');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static('public'));
// CORS middleware for frontend
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-api-key');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
// API Key protection for MCP endpoints
const apiKeyAuth = (req, res, next) => {
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== process.env.API_KEY) {
return res.status(401).json({ error: 'Invalid API key' });
}
next();
};
// ===== STATIC ROUTES (NO ASYNC OPERATIONS) =====
app.use('/webhook', webhookRoutes);
app.get('/health', (req, res) => res.json({ status: 'OK' }));
// Serve main GUI
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Serve signals chart page
app.get('/signals', (req, res) => {
res.sendFile(__dirname + '/public/signals.html');
});
// ===== PUBLIC API ROUTES (NO AUTH REQUIRED) =====
// Get signals chart data
app.get('/api/signals-chart/:symbol?', async (req, res) => {
try {
const symbol = (req.params.symbol || 'BTCUSDT').toUpperCase();
const limit = parseInt(req.query.limit) || 20;
// Get market data for context
const data = await binance.getMarketData(symbol, '1h');
// Get signals for this symbol
const signals = signalStore.getSignalsBySymbol(symbol, limit);
// Get signal stats
const stats = signalStore.getSignalStats();
// Convert signals to chart markers
const markers = signals.map(signal => ({
time: Math.floor(new Date(signal.timestamp).getTime() / 1000),
position: signal.signal.toUpperCase() === 'BUY' ? 'belowBar' : 'aboveBar',
color: signal.signal.toUpperCase() === 'BUY' ? '#00ff88' : '#ff4444',
shape: signal.signal.toUpperCase() === 'BUY' ? 'arrowUp' : 'arrowDown',
text: `${signal.signal.toUpperCase()} @${signal.price}`,
id: signal.id
}));
res.json({
symbol,
marketData: data,
signals: signals.slice(0, 5),
allSignals: limit,
markers,
stats,
timestamp: new Date()
});
} catch (error) {
logger.error('Signals chart error', { error: error.message });
res.status(500).json({ error: error.message });
}
});
// Get signal history
app.get('/api/signals-history/:symbol?', async (req, res) => {
try {
const symbol = req.params.symbol ? req.params.symbol.toUpperCase() : null;
const limit = parseInt(req.query.limit) || 50;
const signals = symbol
? signalStore.getSignalsBySymbol(symbol, limit)
: signalStore.getSignalHistory(limit);
const stats = signalStore.getSignalStats();
res.json({
symbol: symbol || 'ALL',
signals,
total: signals.length,
stats,
timestamp: new Date()
});
} catch (error) {
logger.error('Signals history error', { error: error.message });
res.status(500).json({ error: error.message });
}
});
// Get signal statistics
app.get('/api/signals-stats', (req, res) => {
try {
const stats = signalStore.getSignalStats();
const latestSignal = signalStore.getLatestSignal();
res.json({
stats,
latestSignal,
timestamp: new Date()
});
} catch (error) {
logger.error('Signals stats error', { error: error.message });
res.status(500).json({ error: error.message });
}
});
// Public market data endpoint (supports both Binance and DexScreener)
app.get('/api/market-data/:symbol?', async (req, res) => {
try {
const symbol = (req.params.symbol || 'BTCUSDT').toUpperCase();
const interval = req.query.interval || '1h';
// Use multi-chain handler that supports both Binance and DexScreener
const data = await binance.getMarketDataMultichain(symbol, interval);
res.json(data);
} catch (error) {
logger.error('Market data error', { error: error.message, symbol: symbol });
res.status(500).json({ error: 'Failed to fetch market data', message: error.message });
}
});
// ===== DEBUG/TEST ROUTES (NO AUTH) =====
app.use('/test', testRoutes);
// ===== PROTECTED API ROUTES (WITH AUTH) =====
app.use('/api', apiKeyAuth, mcpRoutes);
app.use('/mcp', apiKeyAuth, mcpRoutes);
// Error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
// Export for Vercel
module.exports = app;
// Start locally only (not on Vercel)
if (require.main === module) {
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
}