How to host your own CDN for free in less than 10 minutes
Kevin Nielsen
Posted on December 16, 2023
github.com/kevinanielsen/go-fast-cdn recently reached version 0.1.0 and I thought a great way to celebrate that would be to show you how to use the pocketbase of CDNs to host for free and use it as your private CDN.
To do this, there are very few requirements:
- Basic understanding of RESTful APIs
- Very basic understanding of dockerfiles
- Basic understanding of using your terminal of choice.
Go-fast-cdn is a very minimal and easy-to-use CDN, and this is used as an advantage - With CDN's speed is the key, and that is no different for go-fast-cdn. Fast is not just a part of the name for the sake of marketing. Go-fast-cdn is, as you might be able to tell, written in Golang which is known for its simplicity and its speed.
Other than its speed, functionality is, as stated above, also a big factor for why you should try out go-fast-cdn. It has a straightforward API, with great documentation at go-fast-cdn.redoc.ly. It also, like Pocketbase, has a very minimalistic UI to assist you when uploading and listing content.
For now, the CDN only supports images and documents, but expect more to come in the future.
How do I use it?
Well, that's the easy part - All you need is Docker installed on your local machine and an internet connection.
First, head over to fly.io and sign up. Then download the flyctl cli with one of the following commands.
MacOS
brew install flyctl
Windows
pwsh -Command "iwr https://fly.io/install.ps1 -useb | iex"
Linux
curl -L https://fly.io/install.sh | sh
Then, create a file, where you want to store the Dockerfile and create a Dockerfile.
touch Dockerfile
Then open said Dockerfile with your favorite text editor, and paste the following.
FROM alpine:latest
ARG GO_FAST_VERSION=0.1.0
RUN apk add --no-cache unzip openssh
# download and unzip go-fast-cdn
ADD https://github.com/kevinanielsen/go-fast-cdn/releases/download/${GO_FAST_VERSION}/go-fast-cdn-x86_64-linux.zip /tmp/cdn.zip
RUN unzip /tmp/cdn.zip -d /cdn/
EXPOSE 8080
# start go-fast-cdn
CMD ["/cdn/go-fast-cdn-linux"]
Save the file, then return to your terminal and run the following commands. Make sure that Docker is running on your machine.
docker build -t cdn
If this ran without issues, you should be ready to go, and you can quickly test it out by running the following command.
docker run -p 8080:8080 cdn
Now, you should be able to go to your browser, visit localhost:8080, and see go-fast-cdn running. If this is the case, then you are ready to deploy it on fly.io. Run the following command:
flyctl launch
If everything looks correct, then go ahead and type n on your keyboard. Otherwise, you can tweak the settings by pressing y and get taken to fly.io to adjust the settings to your liking.
The flyctl launcher should now be running and after a minute or two, the following text will appear
Visit your newly deployed app at https://<your-chosen-name>.fly.dev/
Now, you're good to go. If you want to contribute to go-fast-cdn, you're in luck - It's open-source! Head over to github.com/kevinanielsen/go-fast-cdn and open a PR if you want to see a change.
If you like the project and want to support it, the best way to do so is by giving it a star on GitHub. It lets more people know about the project and gives me motivation to continue developing it. Otherwise, go ahead and share this article with a friend or coworker.
Posted on December 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.