forked from wgroeneveld/go-jamming
first commit! 👏
This commit is contained in:
parent
92b528f3ef
commit
c0f0ecb48f
|
@ -0,0 +1,10 @@
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.{js,json,.yml}]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
|
@ -0,0 +1 @@
|
||||||
|
/.yarn/** linguist-vendored
|
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
.yarn/*
|
||||||
|
!.yarn/patches
|
||||||
|
!.yarn/plugins
|
||||||
|
!.yarn/releases
|
||||||
|
!.yarn/sdks
|
||||||
|
!.yarn/versions
|
||||||
|
|
||||||
|
*.sublime-workspace
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
||||||
|
yarnPath: ".yarn/releases/yarn-berry.cjs"
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"name": "serve-my-jams",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"repository": {
|
||||||
|
"url": "https://github.com/wgroeneveld/serve-my-jams",
|
||||||
|
"type": "git"
|
||||||
|
},
|
||||||
|
"author": "Wouter Groeneveld",
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "src/serve.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest",
|
||||||
|
"jam": "node src/serve.js"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"jest": "^26.6.3"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"koa": "^2.13.1",
|
||||||
|
"koa-body": "^4.2.0",
|
||||||
|
"koa-helmet": "^6.0.0",
|
||||||
|
"koa-logger": "^3.2.1",
|
||||||
|
"koa-router": "^10.0.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"folders":
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const Koa = require("koa");
|
||||||
|
const Logger = require("koa-logger");
|
||||||
|
const bodyParser = require('koa-body');
|
||||||
|
const koaRouter = require("koa-router");
|
||||||
|
const helmet = require("koa-helmet");
|
||||||
|
|
||||||
|
// koa docs: https://koajs.com/#application
|
||||||
|
const app = new Koa();
|
||||||
|
const router = new koaRouter();
|
||||||
|
|
||||||
|
// TODO not sure what to do on error yet
|
||||||
|
app.use(Logger());
|
||||||
|
|
||||||
|
// enable ctx.request.body parsing for x-www-form-urlencoded webmentions etc
|
||||||
|
app.use(bodyParser({
|
||||||
|
multipart: true,
|
||||||
|
urlencoded: true
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
// route docs: https://github.com/koajs/router/blob/HEAD/API.md#module_koa-router--Router+get%7Cput%7Cpost%7Cpatch%7Cdelete%7Cdel
|
||||||
|
require("./webmention/route").route(router)
|
||||||
|
|
||||||
|
app.use(helmet());
|
||||||
|
app.use(router.routes()).use(router.allowedMethods());
|
||||||
|
|
||||||
|
const port = process.env.PORT || 4000
|
||||||
|
|
||||||
|
app.listen(port, "localhost", () => {
|
||||||
|
console.log(`Started localhost at port ${port}`)
|
||||||
|
});
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
|
||||||
|
function isValidUrl(url) {
|
||||||
|
return url &&
|
||||||
|
(url.startsWith("http://") || url.startsWith("https://"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
https://www.w3.org/TR/webmention/#sender-notifies-receiver
|
||||||
|
example:
|
||||||
|
POST /webmention-endpoint HTTP/1.1
|
||||||
|
Host: aaronpk.example
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
source=https://waterpigs.example/post-by-barnaby&
|
||||||
|
target=https://aaronpk.example/post-by-aaron
|
||||||
|
|
||||||
|
|
||||||
|
HTTP/1.1 202 Accepted
|
||||||
|
*/
|
||||||
|
function validate(request) {
|
||||||
|
return request.type === "application/x-www-form-urlencoded" &&
|
||||||
|
request.body &&
|
||||||
|
isValidUrl(request.body.source) &&
|
||||||
|
isValidUrl(request.body.target)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function receive(body) {
|
||||||
|
// do stuff with it
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
receive,
|
||||||
|
validate
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
const { validate, receive } = require('./receive')
|
||||||
|
|
||||||
|
function route(router) {
|
||||||
|
router.post("webmention receive endpoint", "/webmention", async (ctx) => {
|
||||||
|
if(!validate(ctx.request)) {
|
||||||
|
ctx.throw(400, "malformed webmention request")
|
||||||
|
}
|
||||||
|
receive(ctx.request.body)
|
||||||
|
|
||||||
|
ctx.body = "Thanks, bro. Will process this webmention soon, pinky swear!";
|
||||||
|
ctx.status = 202
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
route
|
||||||
|
}
|
Loading…
Reference in New Issue