Skip to content

Instantly share code, notes, and snippets.

@magicstone1412
Last active September 2, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magicstone1412/b47e2f5098016751d97e90066182aa15 to your computer and use it in GitHub Desktop.
Save magicstone1412/b47e2f5098016751d97e90066182aa15 to your computer and use it in GitHub Desktop.
// from https://maltechx.de/en/2020/06/host-static-page-on-backblaze-with-cloudflare-workers/
// Backblaze Url
const baseURL = "https://f000.backblazeb2.com/file/skywirex"
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
// only allow get requests
if (event.request.method !== 'GET') {
return new Response('Method not allowed', { status: 405 })
}
// return a cached response if we have one
const cache = caches.default
let cachedResponse = await cache.match(event.request)
if (cachedResponse) {
return cachedResponse
}
const parsedUrl = new URL(event.request.url)
let path = parsedUrl.pathname
// check if a file or folder and 301 when tailing slash is missing
let lastSegment = path.substring(path.lastIndexOf('/'))
if (lastSegment.indexOf('.') === -1) {
if (!lastSegment.endsWith('/')){
return Response.redirect('https://' + parsedUrl.hostname + path + '/', 301)
} else {
path += 'index.html'
}
}
// fetch content from B2
const b2Response = await fetch(`${baseURL}${path}`)
// add some headers
const headers = {
'cache-control': 'public, max-age=14400',
'content-type': b2Response.headers.get('Content-Type')
}
const response = new Response(b2Response.body, { ...b2Response, headers })
// all is well, return the response
if (response.status < 400){
event.waitUntil(cache.put(event.request, response.clone()))
return response
} else if (response.status == 404){
// return error page
return fetch(baseURL + "/404.html")
}
// return minimal error page
if (response.status > 399) {
return new Response(response.statusText, { status: response.status })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment