From c1cc4a02931aa59f5d9b86a290068747d76c0501 Mon Sep 17 00:00:00 2001 From: Danko Date: Sun, 3 Nov 2024 20:24:06 +0200 Subject: [PATCH] Use an actual config, systemd template, minor fixes --- .gitignore | 2 + README.md | 14 ++- build.js | 31 ++++--- config.example.js | 29 +++++++ template/index.html | 85 +++++++++++-------- .../this-computer-pong.service | 4 +- template/this-computer.conf | 2 +- 7 files changed, 111 insertions(+), 56 deletions(-) create mode 100644 config.example.js rename this-computer-pong.service => template/this-computer-pong.service (70%) diff --git a/.gitignore b/.gitignore index 0f25aeb..0f5c4c4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /node_modules /.idea + +/config.js diff --git a/README.md b/README.md index f3e9050..0cdd461 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,14 @@ Available at [this.computer](https://this.computer). * Have a hostname for the website. * Have two hostnames for the IP checks, one with just an A record, one with just AAAA. -Do this (substitute the hostnames): +Copy `config.example.js` to `config.js` and edit as needed. ```bash +# install the few dependencies (pnpm is preferred but npm will work too) +pnpm install -# substitute the hostnames. $PWD/dist is where the html file is -node build.js this.computer 4.this.computer 6.this.computer $PWD/dist +# this generates `dist/index.html` and nginx configs in conf/ +node build.js # LetsEncrypt setup (skip and edit the main config if you prefer handling it yourself) @@ -32,8 +34,14 @@ sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/nginx.sh sudo cp conf/this-computer.conf /etc/nginx/sites-enabled/ sudo systemctl reload nginx +# Enable the pinger service +sudo systemctl enable --now $PWD/conf/this-computer-pong.service + + ``` + + ## License MIT \ No newline at end of file diff --git a/build.js b/build.js index c92c63e..8339eb3 100644 --- a/build.js +++ b/build.js @@ -1,31 +1,30 @@ -import { mkdir, readdir, readFile, writeFile } from 'fs/promises'; +import { mkdir, readdir, readFile, writeFile, realpath } from 'fs/promises'; async function main() { - - const args = process.argv.slice(2); - - if (args.length != 4) { - console.log('Usage: node build.js base_host ip4_host ip6_host root_dir'); - return; - } - - const [BASE_HOST, IP4_HOST, IP6_HOST, DIST_DIR] = args; - const vars = { BASE_HOST, IP4_HOST, IP6_HOST, DIST_DIR }; - console.log(vars); - + + const { default: config } = await import('./config.js'); + await mkdir('dist', { recursive: true }); await mkdir('conf', { recursive: true }); - + + const DIST_DIR = await realpath('dist'); + const CONF_DIR = await realpath('conf'); + const [BASE_HOST, IP4_HOST, IP6_HOST, PONG_PORT] = [config.baseHost, config.ipv4Host, config.ipv4Host, config.pongServerPort]; + const CONFIG_JSON = JSON.stringify(config); + + const vars = { BASE_HOST, IP4_HOST, IP6_HOST, DIST_DIR, CONF_DIR, PONG_PORT, CONFIG_JSON }; + console.log(vars); + const files = await readdir('template'); for (const fn of files) { let src = await readFile(`template/${fn}`, 'utf-8'); for (const [key, val] of Object.entries(vars)) { src = src.replaceAll(key, val); } - const out = fn.match(/\.conf$/) ? 'conf' : 'dist'; + const out = fn.match(/\.(conf|service)$/) ? 'conf' : 'dist'; await writeFile(`${out}/${fn}`, src); } - + } await main(); diff --git a/config.example.js b/config.example.js new file mode 100644 index 0000000..87d8d2d --- /dev/null +++ b/config.example.js @@ -0,0 +1,29 @@ +const baseHost = 'this.computer'; + +export default { + + baseHost, + + // This should only have an A record. + ipv4Host: `4.${baseHost}`, + // This should only have an AAAA record. + ipv6Host: `6.${baseHost}`, + + graphWindow: 30_000, + jitterWindow: 10_000, + graphScale: 500, // ms + + pingServers: [ + { + label: 'EU', + host: baseHost, + }, + /*{ + label: 'EU', + host: baseHost, + },*/ + ], + + pingServerPort: 1234, + +}; diff --git a/template/index.html b/template/index.html index a071725..66028d0 100644 --- a/template/index.html +++ b/template/index.html @@ -25,19 +25,27 @@ } #graphs { display: flex; - flex-direction: column; + flex-wrap: wrap; gap: 16px; padding: 16px 0; + width: 100%; + } + .graph { + display: flex; + flex-direction: column; + gap: 4px; + width: 100%; max-width: 500px; } .legend { display: flex; - gap: 16px; + gap: 8px; align-items: center; flex-wrap: wrap; + max-width: 500px; } .legend span { - min-width: 80px; + min-width: 100px; } canvas { width: 100%; @@ -89,19 +97,22 @@

This Computer: