Skip to content

Udayan853/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener with Authentication

A simple yet powerful URL Shortener service built with Golang, PostgreSQL, and Goose migrations. This project allows users to shorten long URLs, manage their own links, and ensures that each shortened URL is securely associated with the authenticated user who created it.


Features

  • 🔐 User Authentication

    • Secure user registration and login
    • Token-based authentication (JWT)
    • Only logged-in users can create and manage URLs
  • ✂️ URL Shortening

    • Generate short, unique URLs for any valid link
    • Automatically map each shortened URL to the user who created it
  • 📊 User-specific Data

    • Users can view and manage only their own shortened URLs
    • Prevents cross-user access to private data

🛠️ Tech Stack

  • Backend: Golang
  • Database: PostgreSQL
  • Migrations: Goose
  • ORM / Queries: sqlc
  • Authentication: JWT

⚙️ Setup Instructions

1. Clone the Repository

git clone https://github.com/Udayan853/url-shortener.git
cd url-shortener

2. Install Dependencies

go mod tidy

3. Setup Database

Make sure you have PostgreSQL running locally or remotely. Create a new database:

CREATE DATABASE url_shortener;

4. Run Migrations

goose -dir ./sql/schema postgres "postgres://user:password@localhost:5432/url_shortener?sslmode=disable" up

5. Start the Server

go run main.go

Server will start at http://localhost:8080

Alternatively

docker pull udayank23/url-shortener:latest
docker run -d -p 8080:8080 --env-file .env udayank23/url-shortener:latest

Server will start at http://localhost:8080


🔑 API Endpoints

Authentication

  • POST /api/users → Register new user
  • POST /api/login → Login user and return JWT

URL Shortening

  • POST /api/shorten → Shorten a URL (requires authentication)
  • GET /api/urls → List all URLs for logged-in user
  • GET /api/:short_code → Redirect to the original URL

🧩 Example Request

Create Shortened URL

curl -X POST http://localhost:8080/api/shorten \
     -H "Authorization: Bearer <JWT_TOKEN>" \
     -H "Content-Type: application/json" \
     -d '{"url": "https://example.com/very/long/link", "custom_alias":"abc123"}'

Response:

{
  "short_code": "abc123",
  "original_url": "https://example.com/very/long/link"
}

🚧 Future Improvements

  • Frontend interface for easier management
  • Background Job Queue for asynchronous processing of analytics
  • Cron Job to remove expired shortened urls
  • Add Oauth for SSO (Google, Github, etc)

🤝 Contributing

Pull requests are welcome! If you’d like to contribute, feel free to fork the repo and submit a PR.

About

A Dockerised Url-Shortener API

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors