Home

Awesome

no-db-shorturl

It should not be this hard to have a URL shortened. I don't want to configure a SQL database, run a docker container, install thousands of NPM packages, or configure PHP to redirect URLs.

There are lots of URL shorteners out there, but they mostly use a database as a backend. This has:

This stores each link in its own individual file, in the ./data directory.

That is all I want.

This allows for programmatic shorturl generation on my server, using anything that can read/write to files, as well as simple aliases to grab one from my machine by sshing to my server, like:

# list all shorturls
alias shorturls="ssh myserver 'ls shorturls'"  # directory name
# copy a shorturl to my clipboard
alias shz="shorturls | fzf | sed -e 's|^|https://purarue.xyz/s/|' | tee /dev/tty | clipcopy"

You can also build this from source instead:

go install -v "github.com/purarue/no-db-shorturl@latest"

Usage:

Usage of no-db-shorturl:
  -data-folder string
    	directory to store data in (default "./data")
  -port int
    	port to serve shorturl on (default 8040)
  -secret-key string
    	secret key to authenticate POST requests

By default, anyone can create a URL, so I'd recommend providing a secret key with the -secret-key flag.

You can also set the SHORTURL_KEY environment variable, instead of -secret-key flag.

To add a shortened URL, make a POST request to the / endpoint

To use curl as an example:

curl --header "Content-Type: application/json" --request POST --data '{"key":"your_secret_key","url":"https://purarue.xyz"}' http://localhost:8040

or to specify the path to create the shortcut on:

curl --header "Content-Type: application/json" --request POST --data '{"key":"your_secret_key","url":"https://purarue.xyz","hash":"short"}' http://localhost:8040

I use this with nginx, like so:

server {
  listen 443 ssl;
  ssl_certificate ....

  location /s/ {
    proxy_pass https://127.0.0.1:8040/;
  }
}

Which makes this accessible at https://mywebsite/s/.

I have a script here which I use to create new URLs.