Spaces:
Runtime error
Runtime error
File size: 3,220 Bytes
09eb9d4 | 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 | /**
* Graph Viewer API for HuggingClaw
* Simple D3-based graph visualization
*/
const express = require('express');
const neo4j = require('neo4j-driver');
const app = express();
const PORT = process.env.GRAPH_VIEWER_PORT || 7861;
// Connect to Memgraph using Bolt protocol
const driver = neo4j.driver(
'bolt://localhost:7687',
neo4j.auth.basic('', '')
);
// Security & Robustness Configuration (Bugs #19 & #20)
const cors = require('cors');
const rateLimit = require('express-rate-limit');
// CORS Configuration for Obsidian integration
app.use(cors({
origin: ['obsidian://*', 'https://*.hf.space']
}));
// API Rate Limiting to prevent exhaustion
app.use(rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
}));
// Serve static files
app.use(express.static('public'));
// API: Get full graph
app.get('/api/graph', async (req, res) => {
const session = driver.session();
try {
const result = await session.run('MATCH (n) RETURN n');
const edgesResult = await session.run('MATCH ()-[r]->() RETURN r');
res.json({
nodes: result.records.map(record => record.get('n').properties),
edges: edgesResult.records.map(record => ({
...record.get('r').properties,
type: record.get('r').type
}))
});
} catch (error) {
res.status(500).json({ error: error.message });
} finally {
await session.close();
}
});
// API: Get graph by project
app.get('/api/graph/:project', async (req, res) => {
const session = driver.session();
try {
const { project } = req.params;
const result = await session.run(
'MATCH (n {project: $project}) RETURN n',
{ project }
);
const edgesResult = await session.run(
'MATCH (n {project: $project})-[r]->(m {project: $project}) RETURN r',
{ project }
);
res.json({
nodes: result.records.map(record => record.get('n').properties),
edges: edgesResult.records.map(record => ({
...record.get('r').properties,
type: record.get('r').type
}))
});
} catch (error) {
res.status(500).json({ error: error.message });
} finally {
await session.close();
}
});
// API: Health check
app.get('/health', async (req, res) => {
let memgraphReady = false;
const session = driver.session();
try {
const result = await session.run('RETURN 1');
memgraphReady = result.records.length > 0;
} catch (e) {
memgraphReady = false;
} finally {
await session.close();
}
res.json({
status: memgraphReady ? 'healthy' : 'degraded',
version: '1.2.0',
build_date: new Date().toISOString().split('T')[0],
services: {
memgraph: memgraphReady,
graph_viewer: true,
omniroute: 'check-port-20128' // Placeholder for port-based check
},
timestamp: new Date().toISOString()
});
});
// Start server
app.listen(PORT, '0.0.0.0', () => {
console.log(`π Graph Viewer running on port ${PORT}`);
console.log(` http://localhost:${PORT}`);
console.log(` API: http://localhost:${PORT}/api/graph`);
});
// Graceful driver shutdown
process.on('SIGTERM', () => {
driver.close();
});
module.exports = app;
|