mirror of
https://github.com/maxboeck/webring
synced 2024-11-10 14:04:16 +00:00
rewrite lambda functions
This commit is contained in:
parent
9007eaed56
commit
f75843f6db
4 changed files with 15 additions and 15 deletions
|
@ -2,18 +2,17 @@ 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,
|
||||
Location: site.url,
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate'
|
||||
},
|
||||
body: `Redirecting..."`
|
||||
body: `Redirecting to "${site.title}"`
|
||||
}
|
||||
}
|
||||
|
||||
export const getIndex = url => members.findIndex(site => site.url.includes(url))
|
||||
export const getIndex = url => members.findIndex(site => url.includes(site.url))
|
||||
|
||||
export const getNext = url => {
|
||||
const index = getIndex(url)
|
||||
|
@ -35,6 +34,8 @@ export const getPrevious = url => {
|
|||
|
||||
export const getRandom = url => {
|
||||
const randomIndex = Math.floor(Math.random() * members.length)
|
||||
const filtered = members.filter(site => !site.url.includes(url))
|
||||
return filtered[randomIndex]
|
||||
const sites = url
|
||||
? members.filter(site => !url.includes(site.url))
|
||||
: members
|
||||
return sites[randomIndex]
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import { redirect, getNext } from './common/utils'
|
||||
import { redirect, getNext, getRandom } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
console.log(event.headers)
|
||||
const next = getNext(host)
|
||||
const { referer } = event.headers
|
||||
const next = getNext(referer) || getRandom()
|
||||
|
||||
callback(null, redirect(next))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { redirect, getPrevious } from './common/utils'
|
||||
import { redirect, getPrevious, getRandom } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
const prev = getPrevious(host)
|
||||
const { referer } = event.headers
|
||||
const prev = getPrevious(referer) || getRandom()
|
||||
|
||||
callback(null, redirect(prev))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { redirect, getRandom } from './common/utils'
|
||||
|
||||
exports.handler = function(event, context, callback) {
|
||||
const { host } = event.headers
|
||||
const random = getRandom(host)
|
||||
const { referer } = event.headers
|
||||
const random = getRandom(referer)
|
||||
|
||||
callback(null, redirect(random))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue