A Cloudflare Worker that provides shorten link service.
This is a worker that provide shorten link service, you can submit a link to get a shorten key with/without expire seconds, then visit this short link by appending host before the key.
The appended host is the one that you registered to this worker.
Inside this worker, I made a built-in white list for some host that I don't want to set expire time as default.
To deploy this worker, you may want to modify some codes such as $whiteList
, $expireTimeout
and $defaultLen
in src/index.ts
.
And then you can follow the steps to deploy to your Cloudflare account.
- (Optional) Install
wangler
, and log in with your Cloudflare account. - Clone this repository.
cd cloudflare-shorturl-worker
thennpm install
.- run
wrangler kv:namespace create links
to create a new KV namespace, then replace theid
to[[kv_namespaces]] -> id
inwrangler.toml
, do NOT change the value ofbinding
. - run
wrangler whoami
to get youraccount_id
, then replace theaccount_id
inwrangler.toml
. - Change the
$whiteList
insrc/index.ts
to your domains. - Deploy workers by running
wrangler deploy
. - Done!
By sending OPTIONS
request to any path, you will get the HTTP 204 No Content response.
By visiting https://example.com/AbxY
in browser, if the key is correct, you will be redirected to the actual link.
POST /save-url
Content-Type: application/json
Accept: application/json
{"url": "<LINK_TO_BE_SHORTEN>", "hash": "<LINK_STRING_MD5>", "key": "<SPECIFY_KEY_TO_OVERRIDE>"}
url
: A link that you want to shorten it.hash
: (Optional) A MD5 value ofurl
.key
: (Optional) A specify key that you want to override with new link.
{
"key": "AbxY",
"ttl": 3600
}
key
: The shorten key of your link, append tohttps://example.com/<key>
to visit, such ashttps://example.com/AbxY
.ttl
: An expiry time in seconds, and value-1
means no expiry.