A URL shortener service that uses ATProto to allow self hosting and ensuring the user owns their data
Go 80.8%
CSS 10.1%
HTML 8.2%
Dockerfile 0.8%
7 1 0

Clone this repository

https://tangled.org/willdot.net/at-shorter-url
git@knot.willdot.xyz:willdot.net/at-shorter-url

For self-hosted knots, clone URLs may differ based on your setup.

README.md

The AT URL Shortner#

What is it?#

It's a URL shorten service that uses ATProto so that the users creating the short links will always own the links they are creating and gives them the option to host the service themselves so that they can be sure that the links will always be available.

Inspiration#

This project was inspired by a conversation with someone about the classic engineering interview question about creating a URL shortning service. One of the downfalls of this is that you're relying on the service to always be there so that your links don't die. I think Google announced that they were shutting down their service which caused uproar because it meant that existing links embeded in websites and other places would cease to work. Thankfully they announced that they would allow existing links to work, just not allow new ones to be created. But that scare of links no longer working, made me think about how the ATProto architecture is perfect for allowing users to own their links and be sure that they will always be available without depending on another service.

How it works#

When a user logs in with their ATProto account, they will be able to create a new short URL. This action creates a record in their PDS containing the URL to redirect to and in the future could contain other peices of metadata. The key bit here is that the record in their PDS is created with an RKey, which in this application is a TID (timestamp identifier) which is unique and that key becomes the "short URL" ID. Combine that with the host that created the ID and you get a short URL. The users DID, key and URL are then stored in a local database to the service.

As long as the service is running at that host, the short URL will be available to be redirected. When the short URL is hit, the service will take the key part of the URL, look it up in the database to find the real URL and then redirect to that URL.

If a user decides they want to delete the URL they can do so and it will delete the record from their PDS as well.

The service uses optimistic writes / deletes. It will create the database record straight away but should that operation fail, there is a Jetstream consumer that is listening for the short URL records that will then populate the database with (NOOP if the record already exists). However what is nice about doing this, is that should there be data loss, and the database containing all of the links disappears, all the data can be recovered by simply replaying the Jetstream consumer from a point in time and it will recreate the correct state of things.

Self hosting#

Another neat part of this architecture is that it allows the user to self host the service. Traditional short URL services turn out to be a lot of hassle. Read this really interesting thread on Bluesky about it from the creator of tny.im. Turns out hosting one for others to use isn't a great move these days, so by having one that you can host yourself and only you can use, could be a really handy tool. You create the short URLs and pass them out to people to use.

The way the service works, is that whe you start it up, you configure it to be for your DID (ATProto identifier) so that only you can log into the service to create short URLs. Then when the Jetstream consumer runs, it only listens to events for your DID which means you only get your short URL records in the local database.

Remember!!! All of the links you create are stored in your PDS which is public. So anyone that wants to see what links you've created and go to them, will be able to do that.