TinyLink
A production-grade URL shortener built as a full-stack monorepo β fast, trackable, and deployable on free-tier infrastructure.
Live: link.namtt.dev
πΈ Demo
β¨ Features
- Blazing-fast redirects β 302/307 redirects served via Redis cache, near-zero latency
- Click analytics β IP, User-Agent, referrer, geo-location (country/city), time series
- Password-protected links β optional password lock on both redirect and analytics
- Link expiration β set TTL per link
- QR code generation β built-in QR for every short link
- Guest β user link claiming β create links as a guest, claim them on sign-in
- OAuth authentication β Google & GitHub via Auth0
- User dashboard β manage, search, and delete your links
- Admin dashboard β full system analytics, link management, geo distribution charts
- Bot detection & edge middleware β filter bots at the edge, inject OpenGraph metadata
- Rate limiting β Redis-backed, prevents spam and brute-force
- Memory queue + batch insert β click events buffered in-memory, flushed to DB in batches
- Glassmorphism + Minimalist UI β Framer Motion animations, Lenis smooth scroll, dark/light/system theme
- Resilience pages β custom 404, error boundary, skeleton loading screens
π Architecture
βββββββββββββββββββββββββββββββββββββββββββββββ
β Client (Next.js) β
β Landing β Dashboard β Stats β Admin β
β BFF Proxy βββββββββββββββββββββββββββββββΊ β
βββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β HTTP (Internal API Key)
βββββββββββββββββββΌββββββββββββββββββββββββββββ
β Server (Fastify) β
β /api/links β /api/analytics β /api/authβ
β β β β
β βββββββββββββββββββββββββββββββββββββββ β
β β Memory Queue β Batch DB Insert β β
β βββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββ¬βββββββββββββββ¬βββββββββββββββββ
β β
βββββββββΌβββββββ βββββΌβββββββ
β PostgreSQL β β Redis β
β (Prisma) β β Cache β
ββββββββββββββββ ββββββββββββ
Click flow:
- User hits
/:codeβ Edge Middleware checks for bots - Fastify checks Redis cache β DB fallback
- 302 redirect sent immediately
- Click event pushed to in-memory queue
- Background worker batch-inserts to PostgreSQL every few seconds
π¦ Monorepo Structure
tiny-link/
βββ packages/
β βββ server/ # Fastify API β core shortening, analytics, auth
β βββ client/ # Next.js β public app, user dashboard, stats
β βββ admin/ # Next.js β admin dashboard, system analytics
β βββ db/ # Prisma schema + generated client (shared)
β βββ shared/ # TypeScript types, Zod/TypeBox schemas
βββ docs/ # ROADMAP, release templates, agent workflows
βββ scripts/ # Version bumping, utilities
βββ docker-compose.yml
βββ .github/workflows/pipeline.yml
π Tech Stack
| Layer | Technology |
|---|---|
| Frontend | Next.js 16, React 19, Tailwind CSS v4, Framer Motion, Lenis |
| Backend | Fastify 5, Node.js 24, Pino logging |
| Database | PostgreSQL 17 + Prisma 7 ORM |
| Cache / Rate Limit | Redis 7 + ioredis |
| Auth | Auth0 β Google & GitHub OAuth |
| Charts | Recharts |
| UI Components | shadcn/ui, Base UI |
| Validation | Zod (client), TypeBox (server) |
| Testing | Vitest + React Testing Library |
| DevOps | Docker, GitHub Actions, Self-hosted VPS (Cloudflare Tunnel) |
| Package Manager | pnpm 10 workspaces |
π Quick Start
Prerequisites
- Node.js β₯ 24 (matches CI and production; see
.node-versionβfnm use/nvm usepicks it up automatically) - pnpm β₯ 10
- Docker & Docker Compose
1. Clone & install
git clone https://github.com/thanhnam2811/tiny-link.git
cd tiny-link
pnpm install
2. Configure environment
# Server
cp packages/server/.env.example packages/server/.env
# Client
cp packages/client/.env.example packages/client/.env
Fill in the required values (see Environment Variables).
3. Start infrastructure
pnpm docker:up # starts PostgreSQL + Redis
4. Migrate database
cd packages/db
npx prisma migrate deploy
# or for development:
npx prisma db push
5. Run in development
pnpm dev # starts all packages in parallel
| Service | URL |
|---|---|
| Client | http://localhost:3000 |
| Server | http://localhost:3001 |
| Admin | http://localhost:3002 |
| API Docs (Swagger) | http://localhost:3001/documentation |
π³ Docker (Production)
# Build & run everything
pnpm docker:build
docker compose up
# Or just start DB + Redis, run app locally
pnpm docker:up
pnpm dev
The docker-compose.yml includes:
- PostgreSQL 17 (port 5432, persistent volume)
- Redis 7 (port 6379, 50MB LRU eviction)
- App (Fastify server, port 3001, multi-stage build)
π Environment Variables
Server (packages/server/.env)
| Variable | Description | Required |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | β |
REDIS_URL |
Redis connection string | β |
CLIENT_URL |
Frontend URL for CORS | β |
INTERNAL_API_KEY |
Shared secret for clientβserver calls | β |
JWT_SECRET |
JWT signing secret | β |
ADMIN_PASSWORD |
Admin dashboard password | β |
Client (packages/client/.env)
| Variable | Description | Required |
|---|---|---|
AUTH0_SECRET |
32-byte hex secret for encrypting session cookies | β |
AUTH0_DOMAIN |
Auth0 tenant domain | β |
AUTH0_CLIENT_ID |
Auth0 application client ID | β |
AUTH0_CLIENT_SECRET |
Auth0 application client secret | β |
APP_BASE_URL |
Public URL of the client app | β |
DATABASE_URL |
PostgreSQL URL (local User table synced from Auth0) | β |
INTERNAL_API_URL |
Fastify server base URL | β |
INTERNAL_API_KEY |
Shared secret matching server | β |
π§ Scripts
pnpm dev # Start all packages in parallel (development)
pnpm build # Clean + build shared β db β all packages
pnpm lint # ESLint across all packages
pnpm test # Vitest across all packages
pnpm format # Prettier format
pnpm docker:up # Start PostgreSQL + Redis
pnpm docker:down # Stop all Docker services
pnpm docker:build # Build Docker image
π’ CI/CD Pipeline
GitHub Actions pipeline (.github/workflows/pipeline.yml) β 4 stages:
PR to main ββββββββββββ Lint & Test only (gate)
Push to main (merge) βββ
β β
βΌ β
1. Lint & Test ββββββββ ESLint + Vitest (Node 24)
β
βΌ
2. Docker Build ββββββββ Multi-stage image β GHCR
β tags: latest | sha
βΌ
3. DB Migration ββββββββ prisma migrate deploy (production)
β
βΌ
4. Deploy ββββββββββββββ SSH (via Cloudflare Tunnel) β VPS pulls & restarts
π Analytics
Each short link tracks:
- Total clicks with time-series (last 7 days)
- Geographic distribution β country-level via geoip-lite
- Referrer breakdown
- User-Agent parsing (browser/device/OS)
Analytics are accessible via:
/stats/:codeβ public stats page (password-protected if set)- Admin dashboard β aggregate system-wide stats
π Security
- Rate limiting on all API endpoints (Redis-backed, per-IP)
- Argon2 password hashing for link passwords
- JWT authentication between services
INTERNAL_API_KEYheader for clientβserver requests- Bot detection at Next.js edge middleware
- Non-root Docker user (
nodeuser)
π Development Guide
Branch strategy
| Branch | Purpose | Environment |
|---|---|---|
feature/* or fix/* |
Feature development | Local |
main |
Production | Production (auto-deploy) |
Workflow: branch off main β PR into main β merge β CI builds, migrates, and deploys automatically.
Database changes
# Create a new migration
cd packages/db
npx prisma migrate dev --name your_migration_name
# Apply migrations (CI/production)
npx prisma migrate deploy
# Quick schema sync (dev only)
npx prisma db push
Build order
Workspace packages must be built in order:
pnpm --filter @tiny-link/shared run build
pnpm --filter @tiny-link/db run build
# then client/server/admin
π License
MIT