Git Product home page Git Product logo

cloudflare-shorten-worker's Introduction

cloudflare-shorten-worker

A Cloudflare Worker that provides shorten link service.

Detail

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.

Deployment / Installation

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.

  1. (Optional) Install wangler, and log in with your Cloudflare account.
  2. Clone this repository.
  3. cd cloudflare-shorturl-worker then npm install.
  4. run wrangler kv:namespace create links to create a new KV namespace, then replace the id to [[kv_namespaces]] -> id in wrangler.toml, do NOT change the value of binding.
  5. run wrangler whoami to get your account_id, then replace the account_id in wrangler.toml.
  6. Change the $whiteList in src/index.ts to your domains.
  7. Deploy workers by running wrangler deploy.
  8. Done!

Usage

Preflight

By sending OPTIONS request to any path, you will get the HTTP 204 No Content response.

Visit / Redirect

By visiting https://example.com/AbxY in browser, if the key is correct, you will be redirected to the actual link.

Save URL

POST /save-url
Content-Type: application/json
Accept: application/json

{"url": "<LINK_TO_BE_SHORTEN>", "hash": "<LINK_STRING_MD5>", "key": "<SPECIFY_KEY_TO_OVERRIDE>"}

Payload

  • url: A link that you want to shorten it.
  • hash: (Optional) A MD5 value of url.
  • key: (Optional) A specify key that you want to override with new link.

Response

{
	"key": "AbxY",
	"ttl": 3600
}
  • key: The shorten key of your link, append to https://example.com/<key> to visit, such as https://example.com/AbxY.
  • ttl: An expiry time in seconds, and value -1 means no expiry.

cloudflare-shorten-worker's People

Contributors

ayakurayuki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.