Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (78 loc) · 2.92 KB
/
Copy pathserver.js
File metadata and controls
92 lines (78 loc) · 2.92 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
import express from 'express';
import 'dotenv/config';
const app = express();
const PORT = process.env.PORT || 3000;
// Parse raw body for SQL queries
app.use(express.text({ type: '*/*', limit: '10mb' }));
// ClickHouse connection details
const CLICKHOUSE_URL = process.env.CLICKHOUSE_URL;
const CLICKHOUSE_USER = process.env.CLICKHOUSE_USER;
const CLICKHOUSE_PASSWORD = process.env.CLICKHOUSE_PASSWORD;
// CORS middleware
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
next();
});
// Query endpoint
app.post('/api/query', async (req, res) => {
try {
const query = req.body;
if (!query || typeof query !== 'string') {
res.status(400).json({ error: 'Query must be a string in request body' });
return;
}
// Get format from query parameter (default: arrow)
const format = (req.query.format || 'arrow').toLowerCase();
const validFormats = ['arrow', 'json'];
if (!validFormats.includes(format)) {
res.status(400).json({ error: `Invalid format. Must be one of: ${validFormats.join(', ')}` });
return;
}
// Add FORMAT if not already present
const formatString = format === 'json' ? 'JSONCompact' : 'Arrow';
const queryWithFormat = query.trim().toUpperCase().includes('FORMAT')
? query
: `${query.trim()} FORMAT ${formatString}`;
// Make HTTP request to ClickHouse
const response = await fetch(process.env.CLICKHOUSE_URL, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'X-ClickHouse-User': process.env.CLICKHOUSE_USER,
'X-ClickHouse-Key': process.env.CLICKHOUSE_PASSWORD,
},
body: queryWithFormat,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`ClickHouse error: ${errorText}`);
}
if (format === 'json') {
// Return JSON response
const jsonData = await response.json();
res.setHeader('Content-Type', 'application/json');
res.setHeader('X-ClickHouse-Format', 'JSONCompact');
res.status(200).json(jsonData);
} else {
// Return Arrow binary response
const buffer = await response.arrayBuffer();
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('X-ClickHouse-Format', 'Arrow');
res.status(200).send(Buffer.from(buffer));
}
} catch (error) {
console.error('Query error:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`);
console.log(`ClickHouse URL: ${CLICKHOUSE_URL}`);
console.log(`Endpoint: POST http://localhost:${PORT}/api/query`);
console.log('\nIn another terminal, run: npm run frontend');
});