This commit is contained in:
Erin 2023-07-01 15:37:34 -04:00 committed by Erin
commit 06547a0e2d
5 changed files with 1090 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
webring.txt

33
index.mjs Normal file
View file

@ -0,0 +1,33 @@
import * as fs from 'node:fs';
import express from 'express';
const sites = fs.readFileSync('./webring.txt', 'utf-8')
// split lines
.split(/\r?\n/)
// trim whitespace from each line
.map(site => site.trim())
// exclude empty lines and lines that start with # because why not
.filter(site => site && !site.startsWith('#'));
console.log(sites);
// modulo that does the right thing with negatives
// https://stackoverflow.com/a/4467559
const mod = (n, m) => ((n % m) + m) % m;
function handle (offset, request, response) {
const from = request.query.from;
const index = sites.findIndex(site => site === from);
if (!from || index === -1) {
response.status(400).send('the provided site is not in the webring');
return;
}
response.redirect(301, sites[mod(index + offset, sites.length)]);
}
express()
.get('/prev', (request, response) => handle(-1, request, response))
.get('/next', (request, response) => handle(+1, request, response))
.listen(process.env.PORT || 8080, () => {
console.log('doing the thing');
})

1036
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

15
package.json Normal file
View file

@ -0,0 +1,15 @@
{
"name": "webring-shit",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}

4
webring.txt.sample Normal file
View file

@ -0,0 +1,4 @@
https://example.com
https://example.org
https://example.net/~someone
https://example.net/~otherone