δΈζζζ‘£ | English
A high-performance, feature-rich URL shortener service built on Cloudflare Workers, providing comprehensive link management, detailed analytics, and a modern management dashboard.
- High-Performance Link Shortening - Powered by Cloudflare Workers edge computing
- Custom Short Code Support - Users can customize link suffixes
- Link Expiration Management - Support for setting link validity periods
- Link Status Control - Enable/disable links functionality
- Batch Link Management - Support for bulk operations and management
- Detailed Access Statistics - IP, device, browser, geographic location analysis
- Real-time Data Monitoring - Access trends, popular links ranking
- Data Export Features - Support for JSON/CSV format exports
- Access Log Queries - Complete access records with filtering
- Visual Charts - Chart.js powered data visualization
- Modern Dashboard - Responsive design, mobile-friendly
- Real-time Data Display - Statistics cards, trend charts
- Convenient Link Management - Create, edit, delete, toggle status
- Elegant User Experience - Smooth animations, smart notifications
- URL Validation & Sanitization - Prevent malicious links
- Rate Limiting - Prevent abuse
- Security Headers - CSP, HSTS security policies
- Error Handling - Graceful error pages
- Cloudflare Workers - Edge computing platform
- Hono - Lightweight web framework
- TypeScript - Type-safe JavaScript
- Cloudflare D1 - Edge SQLite database
- Zod - TypeScript-first data validation
- Native HTML/CSS/JavaScript - No framework dependencies
- Chart.js - Data visualization
- Responsive Design - Supports all devices
- Modern UI - CSS Grid, Flexbox, animations
- Wrangler - Cloudflare development tool
- Vitest - Unit testing framework
- ESLint + Prettier - Code quality assurance
- Node.js 18+
- npm or yarn
- Cloudflare account
git clone https://github.com/yourusername/shorty.git
cd shortynpm installCreate a .env file:
ENVIRONMENT=development
BASE_URL=http://localhost:8787
CORS_ORIGIN=*
DEFAULT_SHORT_LENGTH=6
MAX_URL_LENGTH=2048
RATE_LIMIT_PER_MINUTE=60# Create D1 database
npx wrangler d1 create shorty-db
# Run database migrations
npx wrangler d1 migrations apply shorty-db --localnpm run devThe server will start at http://localhost:8787.
Open your browser and visit:
- Homepage: http://localhost:8787
- Management Dashboard: http://localhost:8787/dashboard
POST /api/links
Content-Type: application/json
{
"originalUrl": "https://example.com",
"customSlug": "my-link", // Optional
"expireDays": 365 // Optional
}GET /api/links?page=1&limit=10GET /api/links/code/{shortCode}PUT /api/links/code/{shortCode}
Content-Type: application/json
{
"originalUrl": "https://new-url.com",
"isActive": true
}DELETE /api/links/code/{shortCode}POST /api/links/code/{shortCode}/toggleGET /api/analytics/overviewGET /api/analytics/links/code/{shortCode}GET /api/analytics/top-links?period=week&limit=10GET /api/analytics/access-logs?shortCode={shortCode}&page=1&limit=20GET /api/analytics/export?format=csv&startDate=2023-01-01&endDate=2023-12-31GET /{shortCode}
# Automatically redirects to original URLCREATE TABLE links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
original_url TEXT NOT NULL,
short_code TEXT UNIQUE NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME,
access_count INTEGER DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
last_accessed_at DATETIME
);CREATE TABLE access_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
link_id INTEGER NOT NULL,
short_code TEXT NOT NULL,
accessed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
ip_address TEXT,
user_agent TEXT,
referer TEXT,
country TEXT,
city TEXT,
device_type TEXT,
browser TEXT,
os TEXT,
response_time_ms INTEGER
);CREATE TABLE daily_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
link_id INTEGER NOT NULL,
short_code TEXT NOT NULL,
date TEXT NOT NULL,
total_visits INTEGER DEFAULT 0,
unique_visitors INTEGER DEFAULT 0,
mobile_visits INTEGER DEFAULT 0,
desktop_visits INTEGER DEFAULT 0,
tablet_visits INTEGER DEFAULT 0,
bot_visits INTEGER DEFAULT 0,
top_countries TEXT,
top_cities TEXT,
top_referers TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);name = "shorty"
main = "src/index.ts"
compatibility_date = "2024-01-15"
compatibility_flags = ["nodejs_compat"]
[env.production]
vars = { ENVIRONMENT = "production" }
[[env.production.d1_databases]]
binding = "DB"
database_name = "shorty-db"
database_id = "your-database-id"# Create production database
npx wrangler d1 create shorty-db-prod
# Run migrations
npx wrangler d1 migrations apply shorty-db-prod --env productionnpm run deployConfigure a custom domain in the Cloudflare Workers console, such as https://short.yourdomain.com
Set the following environment variables in the Cloudflare Workers console:
ENVIRONMENT=production
BASE_URL=https://short.yourdomain.com
CORS_ORIGIN=https://yourdomain.com
DEFAULT_SHORT_LENGTH=6
MAX_URL_LENGTH=2048
RATE_LIMIT_PER_MINUTE=100shorty/
βββ src/
β βββ handlers/ # Request handlers
β β βββ analytics.ts # Analytics API
β β βββ links.ts # Link management API
β β βββ redirect.ts # Redirect handling
β βββ middleware/ # Middleware
β β βββ cors.ts # CORS handling
β β βββ errorHandler.ts # Error handling
β βββ services/ # Business logic layer
β β βββ analyticsService.ts # Analytics service
β β βββ database.ts # Database service
β β βββ linkService.ts # Link service
β βββ utils/ # Utility functions
β β βββ analytics.ts # Analytics utilities
β β βββ slugGenerator.ts # Short code generation
β β βββ urlValidator.ts # URL validation
β β βββ validation.ts # General validation
β βββ types/ # TypeScript types
β β βββ index.ts
β βββ static/ # Static assets
β β βββ dashboard.html # Management interface
β βββ index.ts # Main application entry
βββ migrations/ # Database migrations
βββ tests/ # Test files
βββ wrangler.toml # Cloudflare configuration
npm run devnpm run testnpm run type-checknpm run format# View database
npx wrangler d1 execute shorty-db --local --command "SELECT * FROM links LIMIT 10"
# Backup database
npx wrangler d1 backup create shorty-db --local
# Restore database
npx wrangler d1 backup restore shorty-db backup-id --local# View production data
npx wrangler d1 execute shorty-db --env production --command "SELECT COUNT(*) FROM links"
# Production database backup
npx wrangler d1 backup create shorty-db --env production- CDN Caching: Static assets cached through Cloudflare CDN
- API Caching: Analytics data with appropriate cache headers
- Database Optimization: Index optimization and query performance tuning
- Response Time: Average < 100ms
- Availability: 99.9%+
- Error Rate: < 0.1%
- Cache Hit Rate: > 90%
We welcome contributions! Please follow these steps:
- Fork the Project
- Create Feature Branch:
git checkout -b feature/AmazingFeature - Commit Changes:
git commit -m 'Add some AmazingFeature' - Push to Branch:
git push origin feature/AmazingFeature - Create Pull Request
- Use TypeScript for type safety
- Follow ESLint and Prettier rules
- Write unit tests
- Update relevant documentation
When reporting bugs or requesting features, please provide:
- Detailed problem description
- Steps to reproduce
- Expected behavior
- Actual behavior
- Environment information
For detailed contribution guidelines, please see CONTRIBUTING.md.
This project is licensed under the MIT License - see the LICENSE file for details.
- Cloudflare Workers - Providing edge computing platform
- Hono - Excellent web framework
- Chart.js - Data visualization library
- All contributors and users
- Project Homepage: https://github.com/yourusername/shorty
- Issue Reports: https://github.com/yourusername/shorty/issues
- Discussions: https://github.com/yourusername/shorty/discussions
β If this project helps you, please give it a Star!