# 🔗 Simple Dynamic Link System

Hệ thống tạo short links thông minh với khả năng redirect users dựa trên platform (Desktop/iOS/Android) và tracking clicks.

## ✨ Features

- 🔗 **Short Links**: Tạo short links dễ nhớ (vd: `link.site/abc123`)
- 📱 **Smart Redirect**: Tự động redirect dựa trên device
  - Desktop → Website
  - Mobile → App (nếu đã cài) hoặc App Store/Play Store
- 📊 **Click Tracking**: Theo dõi số clicks theo platform
- 🛠️ **Easy Config**: Admin UI để tạo và quản lý links
- 🚀 **Simple Deploy**: Docker setup sẵn sàng

## 🏗️ Tech Stack

- **Backend**: Node.js + Express + SQLite
- **Frontend**: Vanilla JavaScript (no frameworks)
- **Database**: SQLite (portable, no setup)
- **Deploy**: Docker

## 📁 Project Structure

```
deeplink/
├── frontend/
│   ├── redirect.html      # Trang redirect chính
│   ├── app.js            # Logic redirect
│   └── style.css         # Minimal styling
├── backend/
│   ├── server.js         # Express API server
│   ├── database.js       # SQLite queries
│   └── admin.html        # Simple admin UI
├── data/
│   └── links.db          # SQLite database
├── package.json          # Dependencies
├── Dockerfile            # Container image
└── docker-compose.yml    # Easy deployment
```

## 🚀 Quick Start

### Local Development

```bash
# Install dependencies
npm install

# Run server
npm start

# Open admin UI
http://localhost:3000/admin
```

### Docker Deployment

```bash
# Build and run with Docker Compose
docker-compose up -d

# Service will be available at http://localhost:3000
```

## 📱 How It Works

1. **Create Link**: Use admin UI to create new short link với URLs cho mỗi platform
2. **Share Link**: Share short URL (vd: `https://link.site/abc123`)
3. **Smart Redirect**:
   - Desktop users → Web URL
   - iOS users → Try app deeplink → Fallback to App Store
   - Android users → Try app deeplink → Fallback to Play Store
4. **Track Clicks**: View statistics by platform in admin panel

## 🛠️ Configuration

### Create New Link

```javascript
POST /api/links
{
  "name": "Download Our App",
  "web_url": "https://myapp.com",
  "ios_app_url": "myapp://home",
  "ios_store_url": "https://apps.apple.com/app/id123456",
  "android_app_url": "myapp://home", 
  "android_store_url": "https://play.google.com/store/apps/details?id=com.myapp"
}
```

### Environment Variables

```bash
BASE_URL=https://link.yoursite.com
PORT=3000
ADMIN_PASSWORD=your-secure-password
```

## 📊 API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/links` | Create new link |
| GET | `/api/links` | List all links |
| GET | `/api/links/:code` | Get link details |
| PUT | `/api/links/:id` | Update link |
| DELETE | `/api/links/:id` | Delete link |
| POST | `/api/clicks` | Track click |
| GET | `/api/stats/:linkId` | Get click statistics |

## 🔐 Admin Panel

Simple web interface để:
- ➕ Tạo short links mới
- 📝 Edit existing links
- 📊 Xem click statistics
- 🗑️ Delete links

Access: `http://your-domain/admin` (password protected)

## 📈 Click Analytics

Track được:
- Total clicks cho mỗi link
- Breakdown by platform (iOS/Android/Desktop)
- Click history by date

## 🐳 Production Deployment

### Using Docker

```yaml
version: '3'
services:
  app:
    build: .
    ports:
      - "80:3000"
    volumes:
      - ./data:/app/data
    environment:
      - NODE_ENV=production
      - BASE_URL=https://link.yoursite.com
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
    restart: unless-stopped
```

### Using PM2

```bash
pm2 start backend/server.js --name deeplink
pm2 save
pm2 startup
```

## 💰 Cost Estimate

- **Development**: 1-2 days
- **Hosting**: ~$5/month (small VPS) hoặc free tier services
- **Maintenance**: Minimal

## 📝 License

MIT

documentation button đang mở ra link github ảo