Files
opal-ios-studio/serve.js
T
2026-09-01 09:01:09 +06:00

72 lines
2.1 KiB
JavaScript

const http = require('http');
const fs = require('fs');
const path = require('path');
const os = require('os');
const PORT = 8080;
const DIRECTORY = __dirname;
const MIME_TYPES = {
'.html': 'text/html; charset=UTF-8',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
function getLocalIp() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '127.0.0.1';
}
const server = http.createServer((req, res) => {
let reqPath = req.url.split('?')[0];
if (reqPath === '/' || reqPath === '') reqPath = '/index.html';
const filePath = path.join(DIRECTORY, reqPath);
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(`Server Error: ${err.code}`);
}
} else {
res.writeHead(200, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*'
});
res.end(content);
}
});
});
server.listen(PORT, '0.0.0.0', () => {
const localIp = getLocalIp();
console.log('='.repeat(60));
console.log('💎 Google Opal AI - iOS Web Studio is Live & Running!');
console.log('='.repeat(60));
console.log(`📱 Open on your iPhone Safari (connected to same Wi-Fi):`);
console.log(` 👉 http://${localIp}:${PORT}`);
console.log(`💻 Or on PC Browser: http://localhost:${PORT}`);
console.log('='.repeat(60));
console.log('💡 iPhone Instruction: Safari-তে গিয়ে "Share ➔ Add to Home Screen" দিন।');
console.log('='.repeat(60));
});