mirror of
https://github.com/maxboeck/webring
synced 2024-11-10 14:04:16 +00:00
set up lambda endpoints
This commit is contained in:
parent
8c8ba3d345
commit
095a4cfd74
4 changed files with 64 additions and 0 deletions
40
lambda/common/utils.js
Normal file
40
lambda/common/utils.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
import meta from '../src/data/meta'
|
||||
import members from '../src/data/members'
|
||||
|
||||
export const redirect = site => {
|
||||
const location = site && site.url ? site.url : meta.url
|
||||
return {
|
||||
statusCode: 302,
|
||||
headers: {
|
||||
Location: location,
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate'
|
||||
},
|
||||
body: `Redirecting to "${site.title}"`
|
||||
}
|
||||
}
|
||||
|
||||
export const getIndex = url => members.findIndex(site => site.url.includes(url))
|
||||
|
||||
export const getNext = url => {
|
||||
const index = getIndex(url)
|
||||
if (index !== -1) {
|
||||
const nextIndex = index < members.length - 1 ? index + 1 : 0
|
||||
return members[nextIndex]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export const getPrevious = url => {
|
||||
const index = getIndex(url)
|
||||
if (index !== -1) {
|
||||
const prevIndex = index > 0 ? index - 1 : members.length - 1
|
||||
return members[prevIndex]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export const getRandom = url => {
|
||||
const randomIndex = Math.floor(Math.random() * members.length)
|
||||
const filtered = members.filter(site => !site.url.includes(url))
|
||||
return filtered[randomIndex]
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import { redirect, getNext } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
const next = getNext(host)
|
||||
|
||||
callback(null, redirect(next))
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import { redirect, getPrevious } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
const prev = getPrevious(host)
|
||||
|
||||
callback(null, redirect(prev))
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import { redirect, getRandom } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
const random = getRandom(host)
|
||||
|
||||
callback(null, redirect(random))
|
||||
}
|
Loading…
Reference in a new issue