Spaces:
Sleeping
Sleeping
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { chromium } = require('playwright');
|
| 3 |
+
const cors = require('cors');
|
| 4 |
+
const fs = require('fs').promises;
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const PORT = process.env.PORT || 7860;
|
| 9 |
+
|
| 10 |
+
app.use(cors());
|
| 11 |
+
app.use(express.json({ limit: '50mb' }));
|
| 12 |
+
app.use('/files', express.static('public'));
|
| 13 |
+
|
| 14 |
+
const publicDir = path.join(__dirname, 'public');
|
| 15 |
+
fs.mkdir(publicDir, { recursive: true }).catch(console.error);
|
| 16 |
+
|
| 17 |
+
app.post('/api/s-playwright', async (req, res) => {
|
| 18 |
+
const { code, lang } = req.body;
|
| 19 |
+
|
| 20 |
+
if (!code || !lang) {
|
| 21 |
+
return res.status(400).json({
|
| 22 |
+
success: false,
|
| 23 |
+
error: 'Missing required fields: code and lang'
|
| 24 |
+
});
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const supportedLangs = ['javascript', 'python', 'java', 'csharp'];
|
| 28 |
+
const normalizedLang = lang.toLowerCase();
|
| 29 |
+
|
| 30 |
+
if (!supportedLangs.includes(normalizedLang)) {
|
| 31 |
+
return res.status(400).json({
|
| 32 |
+
success: false,
|
| 33 |
+
error: `Unsupported language: "${lang}". Supported: ${supportedLangs.join(', ')}`
|
| 34 |
+
});
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
let browser;
|
| 38 |
+
try {
|
| 39 |
+
browser = await chromium.launch({
|
| 40 |
+
headless: true,
|
| 41 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
const context = await browser.newContext();
|
| 45 |
+
const page = await context.newPage();
|
| 46 |
+
|
| 47 |
+
let result;
|
| 48 |
+
if (normalizedLang === 'javascript') {
|
| 49 |
+
result = await page.evaluate(code);
|
| 50 |
+
} else {
|
| 51 |
+
result = { message: `Execution for ${normalizedLang} not yet implemented` };
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
const timestamp = Date.now();
|
| 55 |
+
const screenshotPath = path.join(publicDir, `screenshot-${timestamp}.png`);
|
| 56 |
+
await page.screenshot({ path: screenshotPath, fullPage: true });
|
| 57 |
+
|
| 58 |
+
await browser.close();
|
| 59 |
+
|
| 60 |
+
res.json({
|
| 61 |
+
success: true,
|
| 62 |
+
data: {
|
| 63 |
+
result: result,
|
| 64 |
+
files: [{
|
| 65 |
+
name: `screenshot-${timestamp}.png`,
|
| 66 |
+
publicURL: `/files/screenshot-${timestamp}.png`
|
| 67 |
+
}]
|
| 68 |
+
}
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
} catch (error) {
|
| 72 |
+
if (browser) await browser.close();
|
| 73 |
+
|
| 74 |
+
res.status(500).json({
|
| 75 |
+
success: false,
|
| 76 |
+
error: error.message
|
| 77 |
+
});
|
| 78 |
+
}
|
| 79 |
+
});
|
| 80 |
+
|
| 81 |
+
app.get('/', (req, res) => {
|
| 82 |
+
res.json({
|
| 83 |
+
message: 'Playwright API is running',
|
| 84 |
+
endpoints: {
|
| 85 |
+
'POST /api/s-playwright': 'Execute playwright code'
|
| 86 |
+
}
|
| 87 |
+
});
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
app.listen(PORT, '0.0.0.0', () => {
|
| 91 |
+
console.log(`Server running on port ${PORT}`);
|
| 92 |
+
});
|