Creating a simple tiny URL generator using Golang, PostgreSQL and Redis

“programming language illustration” by Christopher Robin Ebbinghaus on Unsplash
  1. Golang: We will need the Go compiler, basic libraries and tools to compile and run our code, which can be downloaded here.
  2. PostgreSQL: I assume that PostgreSQL DB is already setup and configured to accept external connections. The steps to download and install it can be found here. Once setup, please create a database with a name of your choice and keep the hostname, port, username, password and the database name handy. I would be using tiny_scale_go as my database name in this article.
  3. Redis: We will be using Redis for caching our URLs generated for faster access. I assume Redis server is setup and running. The steps to install the same can be found here. Once setup, please keep the hostname, port and password handy.
  4. Text editor: Any popular text editor can be used. Most of them will have a plugin for Golang, so please install it as it will aid greatly in the process of writing code.
export GOBIN=$GOPATH/binexport PATH=$PATH:$(go env GOPATH)/bin
go get -u github.com/jinzhu/gormgo get -u github.com/go-redis/redis
postgres", "host=127.0.0.1 port=5432 user=postgres dbname=tiny_scale_go password=<db password> sslmode=disable
&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
}
  • We generate the MD5 hash of the given long URL using the crypto module
  • We encode it into a base64 string to remove any special characters which may interfere with the operation of the URL. After encoding, as it might still contain + and / characters, we replace them with _
  • Once the string is generated, we extract first 6 characters of it to keep it short.
  • We initially check for the presence of the tinyURL in the DB for a different long URL. If its present, we recursively increase the start index for fetching the 6 characters until there is no conflict.
<domain>/tiny/?longUrl=<long URL>
<domain>/long/?tinyUrl=<tiny URL>

--

--

--

Full stack developer | Occasional Blogger | Love ☕ | Opinions here are my own | https://hpmahesh.me

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Distributed Tracing in Microservices

“The Zoom of Automotive Testing”. Intrepid’s solution to testing vehicles from home!

11 Programming Memes that will Lighten Up Your Mood

Tester’s Skills: Secure Your App From Negative Reviews

Proof of Sesh Explained | BluntDAO

Checking Nulls and Null-Aware Operators in Dart

Android FRAGMENT USE RecyclerView + CardView

Continuing lessons from my mistakes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahesh H P

Mahesh H P

Full stack developer | Occasional Blogger | Love ☕ | Opinions here are my own | https://hpmahesh.me

More from Medium

API development with type-safety across the entire stack

User Creation And Authentication in Golang Part 1

Go modules in mono-repo

Redis to REST, securely