-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (42 loc) · 1.28 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
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const auth = request.headers.get('Authorization')
if (!auth) {
return new Response(null, {
status: 401,
headers: { 'WWW-Authenticate': 'Basic realm="${REALM}"' },
})
}
const split = auth.split(' ')
const plain = atob(split[1])
const creds = plain.split(':')
if (creds[0] !== TFUSER || creds[1] !== TFPWD) {
return new Response(null, {
status: 401,
})
}
switch (request.method) {
case 'GET':
const state = await TERRAFORM.get('state')
return new Response(state, { status: 200 })
case 'POST':
const req = JSON.stringify(await request.json())
await TERRAFORM.put('state', req)
return new Response(null, { status: 200 })
case 'LOCK':
const lock = await TERRAFORM.get('lock')
if (lock === null) {
const req = JSON.stringify(await request.json())
await TERRAFORM.put('lock', req)
return new Response(null, { status: 200 })
}
return new Response(lock, { status: 423 })
case 'UNLOCK':
await TERRAFORM.delete('lock')
return new Response(null, { status: 200 })
default:
return new Response(null, { status: 400 })
}
}