A lightweight URL shortener service built with Go and Gin, featuring click analytics and flexible database support for self-hosted deployments.

Problem

Public URL shorteners like bit.ly come with privacy concerns, usage limits, and reliance on third-party services. For those wanting to self-host their own short links, whether for privacy, branding, or control, there was a need for a simple, configurable solution that could run on modest hardware while providing useful analytics.

Approach

I chose Go with the Gin web framework for its simplicity and efficient resource usage. The architecture follows a clean separation: handlers for HTTP routing, services for business logic, and models for data structures. GORM serves as the ORM layer, abstracting database differences so users can choose between SQLite for quick setups or PostgreSQL for production use. The dynamic short code generation was implemented to keep URLs short initially while expanding automatically when collisions become more likely. For geographic tracking, I integrated the DB-IP database as an optional feature that can be enabled or disabled via configuration. I also choose go because it seems like a interesting language I want to learn.

Features

  • Short Link Creation: Generate short codes with automatic collision avoidance
  • Redirect Service: Fast URL redirection with click tracking
  • Click Analytics: Track clicks with timestamps, daily timelines, and country breakdowns
  • Token Authentication: Unique tokens for each URL to access statistics and delete links
  • Dual Database Support: SQLite for simplicity, PostgreSQL for production scalability
  • GeoIP Integration: Optional geographic tracking using DB-IP database
  • REST API: Programmatic access for creating links and retrieving statistics
  • Configurable Logging: Adjust output verbosity (silent, warn, error, info)

What it offers

This service gives you full control over your short links with detailed analytics to understand click patterns. The flexible database support means you can start with SQLite on a small VPS or Raspberry Pi, then migrate to PostgreSQL as needed. GeoIP tracking (when enabled) provides insights into where your links are being clicked, useful for understanding your audience.

Tech Stack:

  • Language: Go (1.25+)
  • Framework: Gin web framework
  • ORM: GORM (SQLite/PostgreSQL)
  • Optional: GeoIP via DB-IP database

Repository: git.jimmy-b.se/JimBer110/go_url_shortener