-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
334 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Remote webserver for CPH | ||
|
||
Currently implements a heartbeat server for live user count. | ||
|
||
## Installation on a VM | ||
|
||
- Install node | ||
- Copy `server.js` to `/opt/` | ||
- Install the service: | ||
|
||
``` | ||
sudo cp server.service /etc/systemd/system | ||
sudo systemctl daemon-reload | ||
sudo systemctl start server.service | ||
sudo systemctl enable server.service | ||
``` | ||
- Check status using `sudo systemctl status server.service` | ||
- In CPH, configure the address + port of the server. | ||
## Uninstall | ||
``` | ||
sudo systemctl stop server.service | ||
sudo systemctl disable server.service | ||
sudo rm /etc/systemd/system/server.service | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const http = require('http'); | ||
const { env } = require('process'); | ||
|
||
class HeartbeatServer { | ||
constructor() { | ||
console.log("Creating new HeartbeatServer instance"); | ||
|
||
this.ips = new Map(); | ||
this.refresh_interval_seconds = parseInt(env.REFRESH_INTERVAL) || 30; | ||
this.port = parseInt(env.PORT) || 8080; | ||
console.log(`Refresh interval: ${this.refresh_interval_seconds} seconds`); | ||
|
||
this.runServer(this.port); | ||
|
||
setInterval(() => { | ||
this.runCleanup(); | ||
}, this.refresh_interval_seconds * 1000); | ||
} | ||
|
||
runServer(port) { | ||
console.log("Starting heartbeat server on port " + port); | ||
this.serverInstance = http.createServer((req, res) => { | ||
this.processHeartbeat(req.socket.remoteAddress); | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.end(this.ips.size.toString()); | ||
}); | ||
this.serverInstance.listen(port); | ||
console.log("Heartbeat server started."); | ||
} | ||
|
||
runCleanup() { | ||
const oldLen = this.ips.size; | ||
const now = Date.now(); | ||
for (const [ip, timestamp] of this.ips.entries()) { | ||
if (now - timestamp > this.refresh_interval_seconds * 1000) { | ||
this.ips.delete(ip); | ||
console.log("Deleted stale IP: " + ip); | ||
} | ||
} | ||
const newLen = this.ips.size; | ||
console.log(`Cleaned up ${oldLen - newLen} stale IPs. New count: ${newLen}`); | ||
} | ||
|
||
// Called when a heartbeat is received from an IP | ||
processHeartbeat(ip) { | ||
this.ips.set(ip, Date.now()); | ||
} | ||
} | ||
|
||
console.log("Starting app"); | ||
new HeartbeatServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[Unit] | ||
Description=Node.js heartbeat server | ||
After=network.target | ||
|
||
[Service] | ||
ExecStart=/usr/bin/env node /opt/server.js | ||
Restart=always | ||
RestartSec=5 | ||
Environment=PORT=4546 | ||
User=server | ||
Group=server | ||
WorkingDirectory=/opt | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.