-
Notifications
You must be signed in to change notification settings - Fork 172
/
index.mjs
58 lines (51 loc) · 1.63 KB
/
index.mjs
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
import express from 'express';
import path from 'path';
import fs from 'fs';
import ip from 'ip';
import { hostname } from 'os';
import { server as wisp } from '@mercuryworkshop/wisp-js';
import { publicPath } from 'sodium-static';
const app = express();
app.use(express.static(publicPath));
app.use((req, res, next) => {
res.status(404).sendFile(path.join(publicPath, '404.html'));
});
app.use((req, res, next) => {
if (req.url.endsWith("/wisp/")) {
wisp.routeRequest(req, res);
} else {
next();
}
});
async function checkForUpdates() {
try {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const localVersion = packageJson.version;
const versionTxt = fs.readFileSync('./sodium-static/public/version.txt', 'utf8').trim();
if (localVersion !== versionTxt) {
console.log(`\x1b[32m[Sodium] Update is available: ${versionTxt}. Check the GitHub for more info.\x1b[0m`);
} else {
console.log('\x1b[32m[Sodium] Your system is up to date!\x1b[0m');
}
} catch (error) {
console.error('\x1b[31mError checking for updates:', error, '\x1b[0m');
}
}
function logServerDetails(port) {
const ipAddress = ip.address();
console.log('Sodium is running on:');
console.log(`\thttp://localhost:${port}`);
console.log(`\thttp://${hostname()}:${port}`);
console.log(`\thttp://${ipAddress}:${port}`);
}
const port = parseInt(process.env.PORT, 10) || 8080;
app.listen(port, () => {
checkForUpdates();
logServerDetails(port);
});
const shutdown = () => {
console.log('Server Shutdown. Goodbye!');
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);