-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (59 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { mount } from "@fly/fetch/mount"
import { proxy } from "@fly/fetch/proxy"
import { Collection } from "@fly/data"
async function getTemplate() {
const resp = await fetch("file://index.html")
return await resp.text()
}
async function getHost(req) {
const url = new URL(req.url)
return await `${url.protocol}\/\/${url.host}`
}
const storeEventLink = async function(req, init) {
const host = await getHost(req)
const template = await getTemplate()
const doc = Document.parse(template)
const label = doc.getElementById('host-label')
label.replaceWith(host)
const link = doc.getElementById('host-link')
link.setAttribute('href', host)
const html = doc.documentElement.outerHTML
if (req.method === 'POST') {
const data = await req.formData()
const eventLinkStore = new Collection('eventLinkStore')
let res = await eventLinkStore.put(host, data.get('link'))
return new Response(`${res}`,{
headers: {
"Location": req.url
},
status: 303
})
} else {
return new Response(html, { headers: {"Content-Type": "text/html"}})
}
}
const redirectToEvent = async function(req, init) {
const host = await getHost(req)
const eventLinkStore = new Collection('eventLinkStore')
const eventLink = await eventLinkStore.get(host)
console.log(`Redirect link for ${host}: ${eventLink}`)
if (eventLink === undefined) {
return new Response('Not Found', {status: 404})
}
return new Response('Redirecting', {
headers: {
'Location': eventLink
},
status: 303
})
}
const getFavicon = async function(req, init) {
return await fetch("file://favicon.ico")
}
const mounts = mount({
'/favicon.ico': getFavicon,
'/store': storeEventLink,
'/admin': storeEventLink,
'/': redirectToEvent
})
fly.http.respondWith(mounts)