Skip to content
itefixPublic

About

Small self-hosted URL shortener that keeps mappings in plain JSON files. No database, hot reload, expiring links and CLI tools, in a tiny Go container.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ishrink URL Shortener

Version: 2.0.0 (CHANGELOG)

A small URL shortener that keeps its mappings in plain JSON files. It is written in Go and runs as a Docker container.

It is meant for operators who want fast redirects and transparent configuration without a database, with URLs that change now and then rather than constantly: every change to a file reloads all files.

Features

  • No database: every mapping is plain JSON, easy to version, audit and back up.
  • Small footprint: a single static binary in an otherwise empty image, using only the Go standard library.
  • No restarts: changes to the JSON files are picked up within about a second.
  • Expiry: a mapping stops redirecting the moment it expires.
  • Command line tools: add_url, list_urls and delete_url are part of the image.
  • Tolerant of mistakes: a bad entry is logged and skipped, and a file broken while being edited keeps its previous content.

Quick start

docker run -d \
  --name ishrink \
  --restart unless-stopped \
  -p 8080:8080 \
  -v ishrink-urls:/app/urls \
  -e SHORT_BASE_URL=https://go.example.com \
  ghcr.io/itefix/ishrink:latest

docker exec ishrink add_url links.json https://github.com --short gh

curl -I http://localhost:8080/gh

To build from a checkout and keep the JSON files in ./urls, use Docker Compose:

docker compose up --build -d
docker compose exec shortener add_url links.json https://github.com --short gh

The commands below use docker compose exec shortener; with docker run as above, use docker exec ishrink instead.

ishrink serves plain HTTP. Put a reverse proxy in front of it for HTTPS.

Published image

Tagged releases are built by GitHub Actions and published to the GitHub Container Registry for linux/amd64 and linux/arm64:

docker pull ghcr.io/itefix/ishrink:latest

Environment variables

Variable Default Description
SHORT_BASE_URL https://go.example.com Base URL that add_url prints in front of the short code
HTTP_PORT 8080 Port the server listens on
BIND_ADDRESS 0.0.0.0 Address the server listens on; the default is IPv4 only, :: accepts IPv6 and IPv4
HOME_REDIRECT_URL unset http:// or https:// URL that / redirects to; unset, / returns 404
FORWARD_QUERY false If true, the query string of a request is appended to the target URL
URLS_DIR /app/urls Directory holding the JSON files

The server refuses to start if a value is invalid.

Redirect behaviour

  • Redirects use status 302, so browsers ask again and changed, deleted or expired mappings take effect for returning visitors.
  • Short codes are matched ignoring case: /GH and /gh are the same.
  • One trailing slash is ignored: /gh/ works like /gh.
  • The query string does not take part in matching: /gh?utm=1 redirects to the target of gh. Set FORWARD_QUERY=true to pass it on.
  • Anything else returns 404.

Adding URLs

docker compose exec shortener add_url <file.json> <url> [OPTIONS]

The file is created if needed. Its name must end in .json. The command prints the short URL.

Option Description
--short CODE Use a specific short code instead of a generated one
--prefix PREFIX Add a prefix, separated by _
--suffix SUFFIX Add a suffix, separated by _
--desc DESCRIPTION Add a description
--expiry DATETIME Stop redirecting after this time
--verbose Also show the file and its mappings
--help Show usage

Short codes may contain letters, numbers, _ and -, and must be unique across all files.

# Generated code: https://go.example.com/gh_<six letters>
docker compose exec shortener add_url links.json https://github.com --prefix gh

# Specific code with description and expiry
docker compose exec shortener add_url links.json https://example.com/event --short evt \
  --desc "Spring event" --expiry 2027-03-31T23:59:59+02:00

# Quote URLs that contain special characters
docker compose exec shortener add_url links.json 'https://shop.example.net/pay?id=488&method=card'

Listing and deleting

# List the JSON files
docker compose exec shortener list_urls

# List the entries of one file with their status: active, expired or invalid
docker compose exec shortener list_urls links.json

# Delete one short code (case sensitive)
docker compose exec shortener delete_url links.json gh

# Delete by wildcard (* and ?), previewing first
docker compose exec shortener delete_url links.json 'promo-*' --wildcard --dry-run
docker compose exec shortener delete_url links.json 'promo-*' --wildcard

JSON files

Each file in the URL directory holds an array of entries, or a single entry. Files can be edited by hand.

[
  {
    "short": "gh",
    "url": "https://github.com",
    "description": "GitHub homepage",
    "expiry": "2099-12-31T23:59:59"
  }
]
Field Required Description
short Yes Short code (gh for /gh)
url Yes Target URL
description No Free text
expiry No Time after which the mapping no longer redirects

expiry takes an ISO 8601 date or date and time, such as 2027-03-31, 2027-03-31T23:59, 2027-03-31T23:59:59Z or 2027-03-31T23:59:59+02:00, or @ followed by a Unix timestamp. Values without a time zone are in UTC. A date alone means the start of that day.

Entries are skipped, with a message in the log, if short or url is missing or not a string, the expiry cannot be parsed, a value contains control characters, or the short code starts with / or contains a space, ? or #.

If a code is defined more than once, the first definition that has not expired wins, taking files in alphabetical order. Hidden files and files not ending in .json are ignored.

Container notes

The image holds no shell. The process runs as root, so that the commands can write to the volume. To run as another user, make the URL directory writable for that user and set --user.

The commands write a .lock file in the URL directory, which lets several of them run at the same time safely.

Troubleshooting

  • A URL does not redirect: check docker compose logs shortener for skipped entries or invalid files, and list_urls <file.json> for the entry's status.
  • The container exits at start: the log names the invalid setting.
  • Port already in use: choose another, for example HTTP_PORT=9000 docker compose up -d.

Upgrading from 1.x

Existing JSON files, the /app/urls volume and the command names and options carry over. Review the Changed and Removed sections of the CHANGELOG before upgrading, in particular:

  • Redirects are 302 instead of 301.
  • / returns 404 unless HOME_REDIRECT_URL is set.
  • Entries with an expiry that cannot be parsed no longer redirect.

Development

With Go 1.22 or later:

go test ./...
URLS_DIR=./urls go run .
URLS_DIR=./urls go run . add_url links.json https://example.com

Building the image (docker build -t ishrink:local .) also runs the tests.

Versioning and releases

The project follows Semantic Versioning and keeps a CHANGELOG. To release:

  1. Set the version in VERSION and in this README.
  2. Add a ## [X.Y.Z] - YYYY-MM-DD section to CHANGELOG.md, and add its link at the bottom.
  3. Commit and push to main.
  4. Tag and push: git tag X.Y.Z && git push origin X.Y.Z.

The tag push triggers build-image.yml, which publishes X.Y.Z, X.Y, X and latest. cleanup-images.yml removes untagged images older than 30 days; it runs weekly and can be started manually with an optional dry run.

License

This project is licensed under the BSD 2-Clause License; see LICENSE.

About

Small self-hosted URL shortener that keeps mappings in plain JSON files. No database, hot reload, expiring links and CLI tools, in a tiny Go container.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages