Last active
September 7, 2022 07:58
-
-
Save obezuk/4394d1b2a1b057af997bab4363e631bc to your computer and use it in GitHub Desktop.
Cloudflare Worker - Link Shortener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<!-- Required meta tags --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<!-- Bootstrap CSS --> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | |
<title>Hello, world!</title> | |
</head> | |
<body> | |
<h1>Cloudflare Workers Link Shortener</h1> | |
<h2>Create a Link</h2> | |
<pre>$ curl https://l.obez.uk/?new=https://cloudflare.com</pre> | |
<!-- Optional JavaScript --> | |
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | |
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> | |
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function generate_rand(i) { // recursively fetch randon values incase there is a collision | |
if (typeof i == 'undefined') { | |
i = 0; | |
} | |
if (i >= 5) { | |
throw new Error('Limiting random key checks to 5'); | |
} | |
try { | |
var rand_response = await fetch(new Request('https://csprng.xyz/v1/api?length=6')); | |
var rand = await rand_response.json(); | |
var random_data = rand.Data; | |
var exists = await WORKERS_KV_LINKS.get(random_data); | |
if (exists) { | |
throw new Error('Collision!'); | |
} else { | |
return random_data; | |
} | |
} catch(e) { | |
i++; | |
return await generate_rand(i); | |
} | |
} | |
async function handleRequest(request) { | |
var request_url = new URL(request.url); | |
short_url = request_url.pathname.split('/'); | |
if (short_url[1]) { | |
var input = ''; | |
if (short_url.length > 1) { | |
short_url.shift(); | |
input = short_url.join('/'); | |
} else { | |
input = short_url[1]; | |
} | |
var url = await WORKERS_KV_LINKS.get(input); | |
if (url) { | |
var requested_url = new URL(url); | |
return Response.redirect(requested_url.toString(), 301); | |
} else { | |
return new Response(JSON.stringify({ | |
"Status" : 404, | |
"Message" : 'Short url not found', | |
"ShortUrl" : null | |
}), { | |
'status': 404, | |
'statusText': 'Not found', | |
'headers': { "Content-Type": "application/json" } | |
}); | |
} | |
} else { | |
if (request_url.searchParams.get('new')) { | |
try { | |
var new_url = new URL(request_url.searchParams.get('new')); | |
var rand_response = await fetch(new Request('https://csprng.xyz/v1/api?length=6')); | |
var rand = await rand_response.json() | |
try { | |
var randomKey = await generate_rand(); | |
} catch(e) { | |
return new Response(JSON.stringify({ | |
"Status" : 500, | |
"Message" : e.message, | |
"ShortUrl" : null | |
}), { | |
'status': 500, | |
'statusText': 'Server error', | |
'headers': { "Content-Type": "application/json" } | |
}); | |
} | |
var y = await WORKERS_KV_LINKS.put(randomKey, new_url.toString()); | |
var short_url = request_url.protocol + '//' + request_url.hostname + '/' + randomKey; | |
return new Response(JSON.stringify({ | |
"Status" : 200, | |
"Message" : "Successfully created new link", | |
"ShortUrl" : short_url | |
}), { | |
'headers': { "Content-Type": "application/json" } | |
}); | |
} catch (e) { | |
return new Response(JSON.stringify({ | |
"Status" : 400, | |
"Message" : e.message, | |
"ShortUrl" : null | |
}), { | |
'status': 400, | |
'statusText': 'Bad Request', | |
'headers': { "Content-Type": "application/json" } | |
}); | |
} | |
} else { | |
var info = ''; | |
info = info + '<h1>Cloudflare Workers Link Shortener</h1>'; | |
info = info + '<h2>Create a link</h2>'; | |
info = info + '<pre>$ curl ' + request_url.protocol + '//' + request_url.hostname + '/?new=https://cloudflare.com</pre>'; | |
return new Response(info, { | |
'headers': { "Content-Type": "text/html" } | |
}); | |
} | |
} | |
} |
Sorry for the n00b question, but where in here would you define the KV, and would you use the KV url, namespace ID, or the CNAME for the KV? Thanks!
Workers KV becomes available within the Workers runtime as a global variable via a Namespace Binding (https://developers.cloudflare.com/workers/reference/storage/namespaces/).
You only need the KV url and namespace ID if you are externally interacting with KV via the Cloudflare API.
I used WORKERS_KV_LINKS as my binding so WORKERS_KV_LINKS.get() and WORKERS_KV_LINKS.put() are all that is needed to interact with KV inside a Worker script.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for the n00b question, but where in here would you define the KV, and would you use the KV url, namespace ID, or the CNAME for the KV? Thanks!