First implementation of WebSub

This commit is contained in:
2021-11-10 15:14:41 +01:00
parent f2fb945064
commit 18fdf819f1
11 changed files with 328 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
// Declare a route
fastify.get('/auctions', async (request, reply) => {
console.log('content provided')
return [
{
id: '2',
content_text: 'This is a second item.',
url: 'https://example.org/second-item'
},
{
id: '1',
content_html: '<p>Hello, world!</p>',
url: 'https://example.org/initial-post'
}
]
})
fastify.get('/websub', async (request, reply) => {
console.log('content provided')
return {
topic: 'http://localhost:3100/auctions'
}
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3100)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()

View File

@@ -0,0 +1,17 @@
const axios = require('axios').default
// Run the server!
const start = async () => {
await axios
.post('http://localhost:3000/publish', {
'hub.mode': 'publish',
'hub.url': 'http://localhost:3100/auctions'
})
.then(response => {
console.log(response.data)
})
.catch(error => {
console.log(error)
})
}
start()

View File

@@ -0,0 +1,42 @@
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
const axios = require('axios').default
// Declare a route
fastify.get('/auction-created', async (request, reply) => {
console.log('subscription verified', request.query)
console.log(request.query)
return request.query
})
fastify.post('/auction-created', async (request, reply) => {
console.log('received blog content', request.body)
reply.send()
})
// Run the server!
const start = async () => {
// subscribe to the feed
try {
await fastify.listen(3200)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
await axios
.post('http://localhost:3000', {
'hub.callback': 'http://localhost:3200/auction-created',
'hub.mode': 'subscribe',
'hub.topic': 'http://localhost:3100/auctions',
'hub.ws': false
})
.then(response => {
console.log(response.data)
})
.catch(error => {
console.log(error)
})
}
start()