Skip to content

MagicTeaMC/yue-lat

Repository files navigation

yue.lat 🔗

A fast, lightweight URL shortener built with Rust.

🚀 Quick Start

Prerequisites

  • Rust (1.70.0 or later)
  • Cargo (comes with Rust)

Installation

  1. Clone the repository:
git clone https://github.com/MagicTeaMC/yue-lat.git
cd yue-lat
  1. Run the application:
cargo run

The server will start on http://localhost:3000 by default.

⚙️ Configuration

Configure the application using environment variables:

# Set custom port (default: 3000)
export PORT=8080

# Set base URL for generated links (default: https://yue.lat)
export BASE_URL=https://maoyue.tw

# Or use a .env file
echo "PORT=8080" > .env
echo "BASE_URL=https://maoyue.tw" >> .env

📖 Usage

Web Interface

Visit http://localhost:3000 in your browser to access the web interface. Simply enter a URL and click "Shorten" to generate a short link.

API Endpoints

Shorten a URL

POST /api/v1/shorten

curl -X POST http://localhost:3000/api/v1/shorten \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'

Response:

{
  "original_url": "https://example.com",
  "short_code": "aBc4",
  "short_url": "https://yue.lat/aBc4"
}

Access Shortened URL

GET /{short_code}

Redirects to the original URL with a 301 Permanent Redirect.

curl -I http://localhost:3000/aBc4
# HTTP/1.1 301 Moved Permanently
# Location: https://example.com

🏗️ Architecture

Database Schema

CREATE TABLE urls (
    short_code TEXT PRIMARY KEY CHECK(length(short_code) <= 20),
    original_url TEXT NOT NULL CHECK(length(original_url) <= 2048),
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

📦 Deployment

Docker

Create a Dockerfile:

FROM rust:1.87 as builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/target/release/yue-lat .
EXPOSE 3000
CMD ["./yue-lat"]

Build and run:

docker build -t yue-lat .
docker run -p 3000:3000 -e BASE_URL=https://yourdomain.com yue-lat

Environment Variables for Production

PORT=3000
BASE_URL=https://yourdomain.com
RUST_LOG=info

🤝 Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

📝 License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

📞 Connect


Made with 🦀 Rust and ❤️

About

A fast, lightweight URL shortener built with Rust.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •