-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain-server.js
More file actions
148 lines (129 loc) · 4.18 KB
/
Copy pathblockchain-server.js
File metadata and controls
148 lines (129 loc) · 4.18 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
/**
* Blockchain Server - Stand-alone Service
*
* This server runs the blockchain API endpoints on a dedicated port
* to avoid conflicts with other services. It uses the same routes
* as the main application but operates independently.
*/
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const http = require('http');
const { Pool } = require('@neondatabase/serverless');
const { WebSocketServer } = require('ws');
// Create Express app
const app = express();
const PORT = process.env.BLOCKCHAIN_PORT || 4545; // Use a dedicated port
// Middleware
app.use(express.json());
app.use(cors({
origin: true,
credentials: true
}));
// Session setup (using the same settings as the main app)
app.use(session({
secret: process.env.SESSION_SECRET || 'blockchain-secure-session-1080-bit',
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: 7 * 24 * 60 * 60 * 1000 // 7 days
}
}));
// Database connection - reusing the same connection info
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// Simple authentication middleware for testing
app.use((req, res, next) => {
// For development, allow setting a test user ID via header
if (req.headers['x-test-user-id'] && process.env.NODE_ENV !== 'production') {
req.session.userId = parseInt(req.headers['x-test-user-id']);
}
// For real authentication, this would integrate with the main app's auth
next();
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
service: 'blockchain-api',
timestamp: new Date().toISOString()
});
});
// API version endpoint
app.get('/version', (req, res) => {
res.json({
version: '1.0.0',
name: 'Blockchain API with 1080-bit protection',
description: 'Secure blockchain API with three-layer verification architecture',
environment: process.env.NODE_ENV || 'development'
});
});
// Proxy the blockchain API routes
app.use('/api/blockchain', (req, res) => {
// For now, we'll just return a placeholder response
// In production, this would import and use the real blockchain routes
res.json({
status: 'success',
message: 'Blockchain API running on dedicated port to avoid conflicts',
endpoint: req.path,
method: req.method,
timestamp: new Date().toISOString(),
userId: req.session.userId || 'not authenticated'
});
});
// Create HTTP server
const server = http.createServer(app);
// Add WebSocket support for real-time blockchain updates
const wss = new WebSocketServer({ server, path: '/blockchain/ws' });
wss.on('connection', (ws) => {
console.log('New WebSocket client connected to blockchain service');
// Send initial connection message
ws.send(JSON.stringify({
type: 'CONNECTED',
message: 'Connected to blockchain service with 1080-bit security',
timestamp: new Date().toISOString()
}));
// Handle messages
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
console.log('Received message:', data);
// Echo back with a timestamp
ws.send(JSON.stringify({
type: 'ECHO',
originalMessage: data,
timestamp: new Date().toISOString()
}));
} catch (error) {
console.error('WebSocket message error:', error);
ws.send(JSON.stringify({
type: 'ERROR',
message: 'Failed to process message',
error: error.message,
timestamp: new Date().toISOString()
}));
}
});
// Handle disconnection
ws.on('close', () => {
console.log('WebSocket client disconnected from blockchain service');
});
});
// Start server
server.listen(PORT, '0.0.0.0', () => {
console.log(`Blockchain server running on port ${PORT}`);
console.log(`REST API: http://localhost:${PORT}/api/blockchain`);
console.log(`WebSocket: ws://localhost:${PORT}/blockchain/ws`);
console.log(`Health Check: http://localhost:${PORT}/health`);
});
// Handle shutdown gracefully
process.on('SIGINT', () => {
console.log('Shutting down blockchain server...');
server.close(() => {
console.log('Blockchain server closed');
process.exit(0);
});
});