Code Monkey home page Code Monkey logo

vhl.ink's Introduction

vhl.ink

Custom link shortener service using Cloudflare Workers + KV store on your domain. The Workers free tier is quite generous and perfectly suited for this since KV is optimized for high reads and infrequent writes, which is our use case.

Deploy to Cloudflare Workers

This API is easily consumed programmatically, such as through shell functions or slack slash commands, making it trivial to shorten links on the fly.

Additionally, it is possible to make a simple form to create short links right from a webpage if that is more to your liking. See this example for more info on that.

Usage

Creating short links

send POST request with form data url and path to redirect vhl.ink/path to url.

for authentication, pass a secret key in a x-preshared-key header.

if path exists already, the value will be overwritten (feature, not a bug).

API Example

curl --location --request POST "https://vhl.ink" \
    -H "x-preshared-key: your secret key goes here" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    --data-urlencode "url=$URL" \
    --data-urlencode "path=$NAME"

passing https://github.com/VandyHacks to url, and gh to path will make https://vhl.ink/gh redirect to it (this is a real example).

Deleting short links

Send DELETE request to the shortlink which should be deleted.

Authentication is required; pass the secret key in the x-preshared-key header.

This method is idempotent, being that successive attempts to delete an already-deleted shortlink will result in status 200 (OK).

API Example

curl --location --request DELETE "https://vhl.ink/gh" \
    -H "x-preshared-key: ${SECRET_KEY}"

Will delete the shortlink available at https://vhl.ink/gh

Listing short links

Sending an authenticated GET request to the vhl.ink domain root will respond with a list of all shortlinks maintained by the service.

Authentication is required; pass the secret key in the x-preshared-key header.

API Example

curl --location --request GET "https://vhl.ink" \
    -H "x-preshared-key: ${SECRET_KEY}"

Will return a JSON array of keys with one to three properties:

  [{ name: "gh", expiration: null, metadata: "https://github.com/VandyHacks/vaken"}, ...]

(From https://developers.cloudflare.com/workers/runtime-apis/kv#more-detail)

expiration and metadata are optional.

Consuming

this is the easy part, simply open the shortened link in your browser of choice!

Deploying

Automatically deploys to Cloudflare Workers on push using GitHub Actions. You will only need to modify the account and kv namespace values in wrangler.toml, and set the repo secrets CF_API_TOKEN and SECRET_KEY (this is the preshared header authentication key) used in the workflow.

Oh, and run the worker on the route you want your shortener service to be on of course.

vhl.ink's People

Contributors

aadibajpai avatar ace314159 avatar daniel442li avatar leonm1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vhl.ink's Issues

possible enhancement: distinct namespaces per subdomain

So now that I've figured out why my setup wasn't working, I've noticed something missing that I imagine wouldn't be difficult to implement. My coding skills are probbaly just a bit too amateur for it.

It would be great to have a single worker recognise different routes/subdomains so that stored paths are associated with one subdomain but not another; e.g. worker routes could include https://personal.vhl.ink/* and https://work.vhl.ink/*, where personal.vhl.ink/meme would redirect successfully, but work.vhl.ink/meme would not. Currently, personal.vhl.ink/meme and work.vhl.ink/meme would both redirect.

short links generated but not resolving

Firstly, thanks for this cool project. My first time deploying to CF using github.

Deployment to CF was successful. I can access the worker on my subdomain test.jatx50.io.

POST seems to work:

curl --location --request POST "https://test.jatx50.io" \
    -H "x-preshared-key: ${SECRET_KEY}" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    --data-urlencode "url=https://github.com/rinkjames/vhl.ink" \
    --data-urlencode "path=test"
https://github.com/rinkjames/vhl.ink available at https://test.jatx50.io/test

GET works:

curl --location --request GET "https://test.jatx50.io" \
-H "x-preshared-key: ${SECRET_KEY}"
test
test1

DELETE doesn't seem to work, it just hangs:

curl --location --request DELETE "https://test.jatx50.io/test1" \
    -H "x-preshared-key: ${SECRET_KEY}"

Most importantly, test.jatx50.io/test doesn't redirect to github.com/rinkjames/vhl.ink, it hangs for a while then returns a 523 error. I have test set up in my DNS as a AAAA record that points to 100:: β€”Β is this wrong? Worker route is set to https://test.jatx50.io https://test.jatx50.io/*.

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.