tejani commited on
Commit
c571419
·
verified ·
1 Parent(s): cc3cf50

Update src/server.js

Browse files
Files changed (1) hide show
  1. src/server.js +86 -42
src/server.js CHANGED
@@ -1,43 +1,87 @@
1
- const WebSocket = require('ws');
2
- const pty = require('node-pty');
3
- const http = require('http');
4
-
5
- const server = http.createServer();
6
- const wss = new WebSocket.Server({ server });
7
-
8
- wss.on('connection', (ws) => {
9
- console.log('New client connected');
10
-
11
- // Spawn a new shell (bash) for the connection
12
- const shell = pty.spawn('bash', [], {
13
- name: 'xterm-color',
14
- env: process.env,
15
- });
16
-
17
- // Relay terminal output to the client
18
- shell.on('data', (data) => {
19
- ws.send(data);
20
- });
21
-
22
- // Handle input from the client
23
- ws.on('message', (message) => {
24
- shell.write(message);
25
- });
26
-
27
- // Handle terminal exit
28
- shell.on('exit', () => {
29
- ws.close();
30
- });
31
-
32
- // Handle WebSocket close
33
- ws.on('close', () => {
34
- shell.kill();
35
- console.log('Client disconnected');
36
- });
37
- });
38
-
39
- // Start the server on port 7860 (required for Hugging Face Spaces)
40
- const PORT = process.env.PORT || 7860;
41
- server.listen(PORT, () => {
42
- console.log(`WebSocket server running on port ${PORT}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  });
 
1
+ const WebSocket = require('ws');
2
+ const pty = require('node-pty');
3
+ const http = require('http');
4
+ const express = require('express');
5
+ const path = require('path');
6
+
7
+ console.log('===== Application Startup =====');
8
+
9
+ // Initialize Express for serving static files and health check
10
+ const app = express();
11
+
12
+ // Serve static files from the public directory
13
+ app.use(express.static(path.join(__dirname, '../public')));
14
+
15
+ // Health check endpoint
16
+ app.get('/health', (req, res) => {
17
+ res.status(200).json({ status: 'OK', timestamp: new Date().toISOString() });
18
+ });
19
+
20
+ // Create HTTP server
21
+ const server = http.createServer(app);
22
+
23
+ // Initialize WebSocket server
24
+ const wss = new WebSocket.Server({ server });
25
+
26
+ wss.on('connection', (ws) => {
27
+ console.log('New WebSocket client connected');
28
+
29
+ try {
30
+ // Spawn a new shell (bash) for the connection
31
+ const shell = pty.spawn('bash', [], {
32
+ name: 'xterm-color',
33
+ env: process.env,
34
+ });
35
+
36
+ // Relay terminal output to the client
37
+ shell.onData((data) => {
38
+ ws.send(data);
39
+ });
40
+
41
+ // Handle input from the client
42
+ ws.on('message', (message) => {
43
+ shell.write(message.toString());
44
+ });
45
+
46
+ // Handle terminal exit
47
+ shell.onExit(({ exitCode }) => {
48
+ console.log(`Shell exited with code ${exitCode}`);
49
+ ws.close();
50
+ });
51
+
52
+ // Handle WebSocket close
53
+ ws.on('close', () => {
54
+ shell.kill();
55
+ console.log('WebSocket client disconnected');
56
+ });
57
+ } catch (error) {
58
+ console.error('Error spawning shell:', error.message);
59
+ ws.send(`Error: Failed to spawn shell - ${error.message}\r\n`);
60
+ ws.close();
61
+ }
62
+ });
63
+
64
+ wss.on('error', (error) => {
65
+ console.error('WebSocket server error:', error.message);
66
+ });
67
+
68
+ // Start the server on port 7860
69
+ const PORT = process.env.PORT || 7860;
70
+ server.listen(PORT, () => {
71
+ console.log(`Server running on port ${PORT}`);
72
+ console.log(`Health check available at http://localhost:${PORT}/health`);
73
+ });
74
+
75
+ server.on('error', (error) => {
76
+ console.error('HTTP server error:', error.message);
77
+ });
78
+
79
+ // Log Node.js and dependency versions for debugging
80
+ console.log(`Node.js version: ${process.version}`);
81
+ try {
82
+ console.log(`ws version: ${require('ws/package.json').version}`);
83
+ console.log(`node-pty version: ${require('node-pty/package.json').version}`);
84
+ console.log(`express version: ${require('express/package.json').version}`);
85
+ } catch (error) {
86
+ console.error('Error loading dependency versions:', error.message);
87
  });