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.
- 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
| 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 |
- Docker and Docker Compose installed
- Git
git clone https://github.com/your-username/url-shortener
cd url-shortener
cp .env.example .envEdit .env with your values:
POSTGRES_USER=urluser
POSTGRES_PASSWORD=your_password
POSTGRES_DB=urlshortenerdocker-compose up --buildOpen http://localhost in your browser.
docker-compose -f docker-compose.dev.yml up --buildWait 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.
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 }
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)
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 │
└─────────────────┘
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
- Start with
guidance/docker/01-docker-compose.md— understand how the services are wired together - Read
guidance/docker/02-dockerfile.md— understand how the Go image is built - Read
guidance/nginx/01-nginx-conf.md— understand how requests are routed - Read
guidance/go/01-api-structure.md— understand the API internals - Read
guidance/javascript/01-frontend.md— understand how the UI communicates with the API - Read
guidance/00_overview/01-project-complete-and-debug.md— full picture, bugs, and debug setup
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."
- 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/sqland 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
MIT