-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·129 lines (112 loc) · 3.31 KB
/
server.js
File metadata and controls
executable file
·129 lines (112 loc) · 3.31 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
// editor/server.js
const express = require('express');
const http = require('http');
const path = require('path');
const socketIo = require('socket.io');
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
require('dotenv').config();
// Constants
const PORT = process.env.PORT || 80;
const PREVIEW_URL = process.env.PREVIEW_URL || 'http://localhost:3010';
const CODEBASE_URL = process.env.CODEBASE_URL || 'http://localhost:3020';
// Initialize Express app
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
// Middleware
app.use(cors());
app.use(bodyParser.json({ limit: '10mb' }));
app.use(express.static(path.join(__dirname, 'dist')));
// Proxy endpoints to codebase service
app.use('/api', async (req, res) => {
try {
const url = `${CODEBASE_URL}${req.url}`;
const method = req.method.toLowerCase();
// Forward request to codebase service
const response = await axios({
method,
url,
data: method !== 'get' ? req.body : undefined,
headers: {
...req.headers,
host: new URL(CODEBASE_URL).host
},
withCredentials: true,
// Pass cookies for session tracking
headers: {
'Cookie': req.headers.cookie || ''
}
});
// Set headers
Object.entries(response.headers).forEach(([key, value]) => {
res.set(key, value);
});
// Set cookies if any are returned
if (response.headers['set-cookie']) {
res.set('set-cookie', response.headers['set-cookie']);
}
// Send response
res.status(response.status).send(response.data);
} catch (error) {
console.error(`API proxy error: ${error.message}`);
// Forward error response if available
if (error.response) {
res.status(error.response.status).send(error.response.data);
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
}
});
// Socket.IO connections
io.on('connection', (socket) => {
console.log('Client connected');
// Handle code update
socket.on('code-update', async (data) => {
try {
const { projectId, filePath, code } = data;
// Save file via API
await axios.put(`${CODEBASE_URL}/api/files/${projectId}/${filePath}`, {
content: code,
commitMessage: `Update ${filePath} via socket`
});
socket.emit('code-saved', {
success: true,
message: 'Code saved successfully'
});
} catch (error) {
console.error(`Error saving code: ${error.message}`);
socket.emit('code-saved', {
success: false,
error: error.message
});
}
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'ok',
message: 'Editor server is running',
timestamp: new Date().toISOString()
});
});
// Start server
server.listen(PORT, () => {
console.log(`Editor server running on port ${PORT}`);
console.log(`Connected to codebase service at: ${CODEBASE_URL}`);
console.log(`Connected to preview service at: ${PREVIEW_URL}`);
});