Skip to content

satya-sudo/go-url

Repository files navigation

🔗 URL Shortener

A scalable, microservice-based URL shortener built with Go, React, Postgres, and Redis — designed to run seamlessly in both Docker Compose (local dev) and Kubernetes (scalable deployments).

⚙️ Architecture Overview

Service Description
Gateway The single entrypoint to all APIs. Validates JWTs, routes requests to backend services (Auth, CRUD, Redirect).
Auth Service Manages signup/login. Issues JWTs and stores users in Postgres.
CRUD Service Handles URL creation, deletion, and stats for logged-in users. Uses Postgres for persistence.
Redirect Service Public endpoint /:code. Redirects short links via Redis (cache) → Postgres (fallback).
Job Runner Periodically syncs hit counters from Redis → Postgres.
Frontend (React + Nginx) User-facing dashboard and authentication UI. Proxies API + short URLs through the Gateway.
Postgres Primary database for users and URLs.
Redis High-speed cache for redirects and hit counts.

🏗️ Tech Stack

  • Go 1.21 (Gin / Fiber)
  • React + Vite + Nginx
  • Postgres 15
  • Redis 7
  • Docker Compose (development)
  • Kubernetes (Minikube) (scalable deployment)
  • JWT Authentication

🧱 Database Schema

Users

CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    role TEXT DEFAULT 'user',
    created_at TIMESTAMPTZ DEFAULT now()
);

URLs

CREATE TABLE IF NOT EXISTS urls (
    short_code TEXT PRIMARY KEY,
    long_url TEXT NOT NULL,
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ DEFAULT now(),
    expiration_at TIMESTAMPTZ,
    hits BIGINT DEFAULT 0
);

🚀 Running Locally (Docker Compose)

1️⃣ Clone the repo

git clone https://github.com/yourname/go-url.git
cd go-url

2️⃣ Start all services

docker-compose up --build

3️⃣ Access

Service URL
Frontend http://localhost:5173
Gateway http://localhost:8080
Auth http://localhost:8081
CRUD http://localhost:8082
Redirect http://localhost:8083

☸️ Running in Kubernetes (Minikube)

1️⃣ Start fresh

minikube start --driver=docker

2️⃣ Rebuild and deploy everything

./deploy-to-minikube.sh

This script:

  • Cleans the previous namespace (go-url)
  • Rebuilds all service images inside Minikube
  • Applies all manifests in k8s/
  • Waits for all pods to be ready
  • Optionally port-forwards services to localhost

3️⃣ Access in browser

Component URL
Frontend http://go-url.local (NodePort: 30080)
Gateway API http://go-url.local/api (NodePort: 30081, proxied internally via Nginx)

🔁 Scaling Example

kubectl scale deploy crud --replicas=3 -n go-url
kubectl get pods -n go-url

You'll see multiple CRUD pods handling traffic in parallel.

🔑 API Summary

Endpoint Method Description
/auth/signup POST Register a new user
/auth/login POST Login, returns JWT
/auth/me GET Current user (requires JWT)
/shorten POST Create a short URL (requires JWT)
/shorten/:id DELETE Delete a short URL (requires JWT)
/shorten/:id/stats GET URL stats (requires JWT)
/:code GET Redirect to original long URL

🔄 Background Job (jobRunner)

  • Runs periodically
  • Syncs hits:* keys from Redis → Postgres
  • Resets Redis counters post-sync

🧰 Frontend (React)

Built with Vite + Plain CSS (Dark theme).

Communicates with Gateway via internal Nginx reverse proxy.

Features:

  • ✅ Signup/Login forms with validation
  • ✅ JWT-based session handling
  • ✅ Dashboard to create/delete short URLs
  • ✅ URL stats (hits, creation date, expiry)
  • ✅ Confirmation modal for deletes

⚡ Scalability Highlights

  • 🧩 Microservice isolation — each service can scale independently via kubectl scale
  • 🔁 Kubernetes-native scaling — stateless services (Gateway, CRUD, Redirect) are horizontally scalable
  • 🧠 Caching with Redis — offloads redirect lookups
  • 🗄️ Persistent storage — Postgres backed by Kubernetes volumes
  • 🔒 JWT Security — Gateway enforces user auth for all protected routes
  • 🌍 Nginx Frontend Proxy — seamless redirects + unified domain for FE + API

📝 License

MIT License © 2025 Satyam Shree


Built with ❤️ using Go, React, and Kubernetes

About

distributed url shorter in go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors