Skip to content

About

A self-hosted URL shortener built as a full-stack learning project. Paste a long URL, get a short one — with click tracking included. The goal was to practice building a production-like stack from scratch: a REST API in Go, a reverse proxy with Nginx, a PostgreSQL database, and a lightweight JavaScript frontend, all orchestrated with Docker Compose

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

url-shortener

Go Docker PostgreSQL Nginx License

A self-hosted URL shortener built as a portfolio and learning project. Paste a long URL, get a short one — with click tracking included.

The goal was to practice building a production-like stack from scratch, understanding how each layer communicates with the next: from a vanilla JavaScript frontend, through an Nginx reverse proxy, into a Go REST API, down to a PostgreSQL database — all orchestrated with Docker Compose.

This project was built step by step as a guided learning experience. The guidance/ folder documents the entire journey — every concept explained, every decision made, every question asked along the way.


Features

  • Shorten any URL and get a unique short code
  • Automatic redirect when visiting a short link
  • Click counter per short URL
  • Simple web UI — no frameworks, just vanilla JavaScript
  • Full Docker Compose setup — one command to run everything
  • Development environment with remote debugging via Delve

Tech stack

Layer Technology Role
Frontend HTML + Vanilla JS User interface, fetch API calls
Proxy Nginx Reverse proxy, static file server
API Go (net/http) REST API, business logic
Database PostgreSQL 16 Persistent storage
Infrastructure Docker Compose Container orchestration
Debugger Delve Remote debugging with breakpoints

Getting started

Prerequisites

  • Docker and Docker Compose installed
  • Git

Run in production mode

git clone https://github.com/your-username/url-shortener
cd url-shortener
cp .env.example .env

Edit .env with your values:

POSTGRES_USER=urluser
POSTGRES_PASSWORD=your_password
POSTGRES_DB=urlshortener
docker-compose up --build

Open http://localhost in your browser.

Run in development mode (with debugger)

docker-compose -f docker-compose.dev.yml up --build

Wait for:

API server listening at: [::]:2345

Then in VS Code or Cursor, press F5 to attach the debugger. Set breakpoints anywhere in the Go code.


API reference

POST  /api/shorten         { "url": "https://example.com" }
                           → { "short_url": "/r/abc123" }

GET   /r/:code             → 302 redirect to original URL

GET   /api/stats/:code     → { "url": "https://example.com", "clicks": 42 }

Project structure

url-shortener/
├── docker-compose.yml          production environment
├── docker-compose.dev.yml      development environment with Delve
├── .env.example                environment variables template
├── nginx/
│   └── nginx.conf              reverse proxy configuration
├── api/
│   ├── Dockerfile              production image (multi-stage, FROM scratch)
│   ├── Dockerfile.dev          development image (with Delve)
│   ├── main.go                 entrypoint, router setup
│   ├── handlers/
│   │   └── url.go              HTTP handlers (shorten, redirect, stats)
│   └── db/
│       └── db.go               database connection and schema
├── frontend/
│   ├── index.html              UI
│   └── app.js                  fetch API calls
└── guidance/                   study notes (see below)

Architecture

Browser
   │
   ▼
Nginx :80
   ├── GET  /          → serves frontend/
   ├── /api/*          → proxy → Go API :8080
   └── /r/*            → proxy → Go API :8080
                                    │
                                    ▼
                             PostgreSQL :5432
                             ┌─────────────────┐
                             │ urls            │
                             │ ─────────────── │
                             │ id              │
                             │ code (unique)   │
                             │ url             │
                             │ clicks          │
                             │ created_at      │
                             └─────────────────┘

Learning path

This project was built as a structured learning experience. If you want to follow the same path, the guidance/ folder contains detailed study notes for every topic covered:

guidance/
├── docker/
│   ├── 01-docker-compose.md    services, volumes, healthcheck, networking
│   └── 02-dockerfile.md        multi-stage builds, layer caching, FROM scratch
├── nginx/
│   └── 01-nginx-conf.md        reverse proxy, location blocks, mime types
├── go/
│   └── 01-api-structure.md     packages, modules, handlers, ServeMux, pointers, pool
├── javascript/
│   └── 01-frontend.md          fetch API, async/await, error handling
├── 06-project-complete-and-debug.md   full flow, bugs found, Delve setup
└── project-context.md          full session context for LLM continuity

Suggested reading order

  1. Start with guidance/docker/01-docker-compose.md — understand how the services are wired together
  2. Read guidance/docker/02-dockerfile.md — understand how the Go image is built
  3. Read guidance/nginx/01-nginx-conf.md — understand how requests are routed
  4. Read guidance/go/01-api-structure.md — understand the API internals
  5. Read guidance/javascript/01-frontend.md — understand how the UI communicates with the API
  6. Read guidance/00_overview/01-project-complete-and-debug.md — full picture, bugs, and debug setup

Using the LLM context file

The guidance/teacher.md file contains the complete project context — all files, architecture decisions, and a categorized list of every question covered during the build. You can feed it to any LLM to continue learning from where this project left off:

"Read the file project-context.md and continue the project from where we left off."


What I learned

  • How to structure a REST API in Go using only the standard library
  • Multi-stage Docker builds — keeping production images lean (~10MB)
  • Configuring Nginx as a reverse proxy inside a Docker network
  • Container networking and service discovery with Docker Compose
  • Connecting Go to PostgreSQL using database/sql and connection pooling
  • The difference between HTTP 301 and 302 and why it matters for click tracking
  • Setting up remote debugging with Delve inside a Docker container
  • Separating development and production Docker environments

License

MIT

About

A self-hosted URL shortener built as a full-stack learning project. Paste a long URL, get a short one — with click tracking included. The goal was to practice building a production-like stack from scratch: a REST API in Go, a reverse proxy with Nginx, a PostgreSQL database, and a lightweight JavaScript frontend, all orchestrated with Docker Compose

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages