一個基於 Go + Vue 3 的短網址服務,支援 PostgreSQL 資料庫和 Vercel 部署。
- 短網址生成:自動生成短碼或支援自訂短碼
- QR Code 生成:自動為短網址生成 QR Code
- 點擊統計:詳細的點擊統計和分析
- 現代化 UI:基於 Vue 3 + Tailwind CSS 的美觀介面
- 雲端部署:支援 Vercel 一鍵部署
- 子路徑支援:可內嵌到主域名下 (如 xsong.us/shorturl/xxxxxxx)
- Go + Fiber 框架
- PostgreSQL 資料庫 (Supabase)
- pgx 資料庫驅動
- Vercel Serverless Functions
- Vue 3 + TypeScript
- Tailwind CSS v3
- Vue Router + Pinia
- Axios API 客戶端
go-shorturl/
├── api/ # Vercel Serverless Functions
│ ├── shorten/
│ ├── redirect/
│ └── stats/
├── cmd/server/ # 本地開發服務器
├── pkg/ # 共享包
│ ├── db/ # 資料庫連接
│ ├── handlers/ # API 處理器
│ └── models/ # 資料模型
├── frontend/ # Vue 前端
├── db/ # 資料庫 schema
├── vercel.json # Vercel 配置
└── README.md
- 啟動 PostgreSQL
docker-compose up -d- 設置環境變數
cp .env.example .env.local
# 編輯 .env.local 設置 DATABASE_URL- 啟動後端
cd cmd/server
go run main.go- 啟動前端
cd frontend
npm install
npm run dev-
連接 GitHub
- 將代碼推送到 GitHub
- 在 Vercel 中連接 GitHub 倉庫
-
設置環境變數
DATABASE_URL: Supabase 連接字串BASE_URL: 你的主域名 (如 https://xsong.us)
-
自動部署
- Vercel 會自動檢測並部署
-
創建專案
- 在 Supabase 創建新專案
- 記下連接字串
-
執行 Schema
-- 在 Supabase SQL Editor 中執行 db/schema.sql- 設置連接字串
postgresql://postgres.lypuiroafpvqutvetuov:rwuser123@aws-1-ap-southeast-1.pooler.supabase.com:5432/postgres
問題:invalid operation: operator || not defined on os.Getenv("VERCEL_REGION")
// ❌ 錯誤寫法
strings.Contains(os.Getenv("VERCEL_REGION") || "", "iad")
// ✅ 正確寫法
vercelRegion := os.Getenv("VERCEL_REGION")
strings.Contains(vercelRegion, "iad")問題:DATABASE_URL 無法讀取
解決方案:
- 移除
init()函數,改為在Handler中初始化 - 確保環境變數在 Vercel Dashboard 中正確設置
問題:hostname resolving error (lookup aws-1-ap-southeast-1.supabase.com)
解決方案:
# ❌ 錯誤
postgresql://user:pass@aws-1-ap-southeast-1.supabase.com:5432/db
# ✅ 正確
postgresql://user:pass@aws-1-ap-southeast-1.pooler.supabase.com:5432/db關鍵:必須使用 pooler 子域名!
問題:Handler redeclared 和 ServeHTTP undefined
解決方案:
- 將 API 文件分離到子目錄
- 使用
adaptor.FiberApp(app).ServeHTTP(w, r) - 更新
vercel.json配置
問題:use of internal package not allowed
解決方案:
- 將
internal/目錄重命名為pkg/ - 更新所有 import 路徑
問題:PostCSS 配置錯誤 解決方案:
// package.json
{
"tailwindcss": "3.4.14",
"postcss": "^8.4.47",
"autoprefixer": "^10.4.20"
}// postcss.config.js
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}log.Printf("Environment: VERCEL=%s", os.Getenv("VERCEL"))
log.Printf("DATABASE_URL length: %d", len(os.Getenv("DATABASE_URL")))log.Printf("Database error: %v", err)
return c.Status(500).JSON(fiber.Map{
"error": fmt.Sprintf("Database error: %v", err),
})- 在 Vercel Dashboard → Functions → 查看函數日誌
- 使用
vercel logs命令
創建短網址
{
"url": "https://example.com",
"custom_code": "optional"
}重定向到原始網址
獲取點擊統計
歡迎提交 Issue 和 Pull Request!
MIT License