-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal-server.js
160 lines (137 loc) · 3.94 KB
/
minimal-server.js
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
/**
* Minimal Test Server for ChimeraX Integration
*/
// Load environment variables from test file
require('dotenv').config({ path: '.env.test' });
const express = require('express');
const { spawn } = require('child_process');
const app = express();
app.use(express.json());
// Configuration
const PORT = process.env.PORT || 8765;
// Use hard-coded path to ensure correctness
const CHIMERAX_PATH = '/Applications/ChimeraX.app/Contents/MacOS/ChimeraX';
console.log(`Using ChimeraX path: ${CHIMERAX_PATH}`);
// State
let chimeraxProcess = null;
// Routes
app.get('/api/health', (req, res) => {
res.json({
status: 'success',
message: 'Hashi minimal test server running',
timestamp: new Date().toISOString()
});
});
app.get('/api/chimerax/status', (req, res) => {
const isRunning = chimeraxProcess !== null;
res.json({
status: 'success',
chimeraxPath: CHIMERAX_PATH,
running: isRunning,
pid: isRunning ? chimeraxProcess.pid : null
});
});
app.post('/api/chimerax/start', (req, res) => {
if (chimeraxProcess) {
return res.json({
status: 'success',
message: 'ChimeraX is already running',
pid: chimeraxProcess.pid
});
}
try {
// Start ChimeraX process
console.log('Starting ChimeraX process...');
chimeraxProcess = spawn(CHIMERAX_PATH, ['--nogui', '--silent']);
// Handle process output
chimeraxProcess.stdout.on('data', (data) => {
console.log(`ChimeraX output: ${data.toString().trim()}`);
});
chimeraxProcess.stderr.on('data', (data) => {
console.error(`ChimeraX error: ${data.toString().trim()}`);
});
// Handle process exit
chimeraxProcess.on('close', (code) => {
console.log(`ChimeraX process exited with code ${code}`);
chimeraxProcess = null;
});
res.json({
status: 'success',
message: 'ChimeraX started successfully',
pid: chimeraxProcess.pid
});
} catch (error) {
console.error('Failed to start ChimeraX:', error);
res.status(500).json({
status: 'error',
code: 'CHIMERAX_ERROR',
message: `Failed to start ChimeraX: ${error.message}`
});
}
});
app.post('/api/chimerax/command', (req, res) => {
const { command } = req.body;
if (!command) {
return res.status(400).json({
status: 'error',
code: 'VALIDATION_ERROR',
message: 'Command is required'
});
}
if (!chimeraxProcess) {
return res.status(400).json({
status: 'error',
code: 'CHIMERAX_ERROR',
message: 'ChimeraX is not running. Start it first.'
});
}
// For testing, just log the command and return success
console.log(`Executing ChimeraX command: ${command}`);
res.json({
status: 'success',
message: 'Command sent to ChimeraX',
command
});
});
app.post('/api/chimerax/stop', (req, res) => {
if (!chimeraxProcess) {
return res.json({
status: 'success',
message: 'ChimeraX is not running'
});
}
try {
chimeraxProcess.kill();
chimeraxProcess = null;
res.json({
status: 'success',
message: 'ChimeraX stopped successfully'
});
} catch (error) {
console.error('Error stopping ChimeraX:', error);
res.status(500).json({
status: 'error',
code: 'CHIMERAX_ERROR',
message: `Failed to stop ChimeraX: ${error.message}`
});
}
});
// Start server
app.listen(PORT, () => {
console.log(`Minimal Test Server running at http://localhost:${PORT}`);
console.log('Available endpoints:');
console.log(`- GET /api/health`);
console.log(`- GET /api/chimerax/status`);
console.log(`- POST /api/chimerax/start`);
console.log(`- POST /api/chimerax/command (requires {"command": "..."} in body)`);
console.log(`- POST /api/chimerax/stop`);
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('Shutting down server...');
if (chimeraxProcess) {
console.log('Stopping ChimeraX process...');
chimeraxProcess.kill();
}
process.exit(0);
});