From 58adbdc459718517d5c9e9d1d456f387301e76e7 Mon Sep 17 00:00:00 2001 From: Andrew Pietila Date: Fri, 24 Mar 2023 01:33:25 -0500 Subject: [PATCH] Allow routes to use app.route() Concerns were raised that the previous method potentially made middleware more complicated. In order to allay this, hoist the full route into the route file. Still a single route per file, but facilitates simpler middleware. --- app.js | 5 ++- routes/.well-known/webfinger.js | 48 +++++++++++++++-------------- routes/inbox.js | 54 ++++++++++++++++----------------- 3 files changed, 56 insertions(+), 51 deletions(-) diff --git a/app.js b/app.js index 4e06d97..66db1f3 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,7 @@ const { match: createPathMatch } = require('path-to-regexp'); dot: true, }); const pathMatches = []; - app.use((req, res, next) => { + app.use((req, _res, next) => { const requestUrl = new URL(req.url, 'https://example.com/'); let candidateUrl = ''; let secondCandidateUrl = ''; @@ -53,6 +53,9 @@ const { match: createPathMatch } = require('path-to-regexp'); if ( routeObj.post ) { app.post(`/${route}`, routeObj.post); } + if ( routeObj.route ) { + routeObj.route(app.route(`/${route}`)); + } } app.listen(process.env.PORT || 3000); })(); diff --git a/routes/.well-known/webfinger.js b/routes/.well-known/webfinger.js index d998fba..385bb7a 100644 --- a/routes/.well-known/webfinger.js +++ b/routes/.well-known/webfinger.js @@ -1,30 +1,32 @@ +'use strict'; + const express = require('express'); module.exports = { /** - * @param {express.Request} req - * @param {express.Response} res - * @param {express.NextFunction} _next + * @param {express.IRoute} routeObj */ - get: async (req, res, _next) => { - res.setHeader("content-type", "application/jrd+json"); - res.json( - { - "subject": `acct:${req.headers.host}@${req.headers.host}`, - "aliases": [`https://${req.headers.host}/actor`], - "links": [ - { - "rel": "http://webfinger.net/rel/profile-page", - "type": "text/html", - "href": `https://${req.headers.host}/about/more?instance_actor=true` - }, - { - "rel": "self", - "type": "application/activity+json", - "href": `https://${req.headers.host}/actor` - } - ] - } - ) + route: (routeObj) => { + routeObj.get(async (req, res, _next) => { + res.setHeader("content-type", "application/jrd+json"); + res.json( + { + "subject": `acct:${req.headers.host}@${req.headers.host}`, + "aliases": [`https://${req.headers.host}/actor`], + "links": [ + { + "rel": "http://webfinger.net/rel/profile-page", + "type": "text/html", + "href": `https://${req.headers.host}/about/more?instance_actor=true` + }, + { + "rel": "self", + "type": "application/activity+json", + "href": `https://${req.headers.host}/actor` + } + ] + } + ) + }) } } diff --git a/routes/inbox.js b/routes/inbox.js index 12e4107..c45e915 100644 --- a/routes/inbox.js +++ b/routes/inbox.js @@ -5,35 +5,35 @@ var jsonld = require('jsonld'); module.exports = { /** - * @param {express.Request} req - * @param {express.Response} res - * @param {express.NextFunction} _next + * @param {express.IRoute} routeObj */ - post: async (req, res, _next) => { - if ( req.body ) { - var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams'); - if ( bodyParsed.type === 'Create') { - var to; - var cc; - if ( typeof bodyParsed.object.to === 'string' ) { - to = [bodyParsed.object.to]; - } else { - to = bodyParsed.object.to; + route: (routeObj) => { + routeObj.post(async (req, res, _next) => { + if ( req.body ) { + var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams'); + if ( bodyParsed.type === 'Create') { + var to; + var cc; + if ( typeof bodyParsed.object.to === 'string' ) { + to = [bodyParsed.object.to]; + } else { + to = bodyParsed.object.to; + } + if ( typeof bodyParsed.object.cc === 'string' ) { + to = [bodyParsed.object.cc]; + } else { + cc = bodyParsed.object.cc; + } + await db('inbox').insert({ + origin: bodyParsed.object.attributedTo, + to: JSON.stringify(to), + cc: JSON.stringify(cc), + object: JSON.stringify(bodyParsed.object) + }); + res.status(204); + res.end(); } - if ( typeof bodyParsed.object.cc === 'string' ) { - to = [bodyParsed.object.cc]; - } else { - cc = bodyParsed.object.cc; - } - await db('inbox').insert({ - origin: bodyParsed.object.attributedTo, - to: JSON.stringify(to), - cc: JSON.stringify(cc), - object: JSON.stringify(bodyParsed.object) - }); - res.status(204); - res.end(); } - } + }); } };