Files
ai-scoring-application/COMPLETE_ENV_CONFIG_GUIDE.md

281 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 完整環境變量配置指南
## 概述
現在所有重要的配置都已經整合到環境變量中,包括:
- 應用配置URL、名稱
- 資料庫配置(主機、端口、用戶名、密碼、資料庫名)
- AI 配置Gemini API Key、模型名稱、最大 Token 數)
## 環境變量列表
### 🌐 應用配置
```bash
NEXT_PUBLIC_APP_URL=http://localhost:12024
NEXT_PUBLIC_APP_NAME=AI 智能評審系統
```
### 🗄️ 資料庫配置
```bash
DB_HOST=mysql.theaken.com
DB_PORT=33306
DB_NAME=db_AI_scoring
DB_USER=root
DB_PASSWORD=zh6161168
```
### 🤖 AI 配置
```bash
GEMINI_API_KEY=AIzaSyAN3pEJr_Vn2xkCidGZAq9eQqsMVvpj8g4
GEMINI_MODEL=gemini-1.5-pro
GEMINI_MAX_TOKENS=8192
```
## 完整的 .env.local 範例
```bash
# 應用配置
NEXT_PUBLIC_APP_URL=http://localhost:12024
NEXT_PUBLIC_APP_NAME=AI 智能評審系統
# 資料庫配置
DB_HOST=mysql.theaken.com
DB_PORT=33306
DB_NAME=db_AI_scoring
DB_USER=root
DB_PASSWORD=zh6161168
# AI 配置
GEMINI_API_KEY=AIzaSyAN3pEJr_Vn2xkCidGZAq9eQqsMVvpj8g4
GEMINI_MODEL=gemini-1.5-pro
GEMINI_MAX_TOKENS=8192
```
## 技術實現
### 🔧 配置工具類 (`lib/config.ts`)
```typescript
// 資料庫配置
export const dbConfig = {
host: process.env.DB_HOST || 'mysql.theaken.com',
port: parseInt(process.env.DB_PORT || '33306'),
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'zh6161168',
database: process.env.DB_NAME || 'db_AI_scoring',
// ... 其他配置
}
// AI 配置
export const aiConfig = {
geminiApiKey: process.env.GEMINI_API_KEY || 'fallback_key',
modelName: process.env.GEMINI_MODEL || 'gemini-1.5-pro',
maxTokens: parseInt(process.env.GEMINI_MAX_TOKENS || '8192'),
}
// 配置驗證
export function validateConfig(): { isValid: boolean; errors: string[] }
```
### 📊 資料庫配置更新 (`lib/database.ts`)
```typescript
import { dbConfig } from './config';
// 直接使用配置對象
const pool = mysql.createPool({
...dbConfig,
// ... 其他配置
});
```
### 🤖 AI 配置更新 (`lib/services/gemini.ts`)
```typescript
import { aiConfig } from '../config';
const genAI = new GoogleGenerativeAI(aiConfig.geminiApiKey);
```
## 使用方式
### 🚀 開發環境設置
1. **複製環境變量範例**
```bash
cp env.example .env.local
```
2. **編輯配置**
```bash
# 編輯 .env.local 文件
nano .env.local
```
3. **重啟服務器**
```bash
npm run dev
```
### 🌐 生產環境設置
在部署平台(如 Vercel、Netlify 等)設置環境變量:
```bash
# 應用配置
NEXT_PUBLIC_APP_URL=https://yourdomain.com
NEXT_PUBLIC_APP_NAME=AI 智能評審系統
# 資料庫配置
DB_HOST=your-production-db-host
DB_PORT=3306
DB_NAME=your_production_db
DB_USER=your_db_user
DB_PASSWORD=your_secure_password
# AI 配置
GEMINI_API_KEY=your_production_gemini_key
GEMINI_MODEL=gemini-1.5-pro
GEMINI_MAX_TOKENS=8192
```
## 配置檢查工具
### 🔍 使用配置檢查腳本
```bash
node scripts/check-config.js
```
這個腳本會:
- 顯示當前配置
- 驗證必要的環境變量
- 提供配置建議
### 📋 檢查結果範例
```
🔍 檢查環境變量配置...
📋 當前配置:
應用 URL: http://localhost:12024
應用名稱: AI 智能評審系統
資料庫主機: mysql.theaken.com
資料庫名稱: db_AI_scoring
Gemini 模型: gemini-1.5-pro
最大 Token 數: 8192
✅ 所有必要的環境變量都已設置
```
## 不同環境的配置範例
### 🏠 本地開發環境
```bash
NEXT_PUBLIC_APP_URL=http://localhost:12024
DB_HOST=localhost
DB_PORT=3306
DB_NAME=ai_scoring_dev
```
### 🧪 測試環境
```bash
NEXT_PUBLIC_APP_URL=https://test.yourdomain.com
DB_HOST=test-db.yourdomain.com
DB_PORT=3306
DB_NAME=ai_scoring_test
```
### 🚀 生產環境
```bash
NEXT_PUBLIC_APP_URL=https://yourdomain.com
DB_HOST=prod-db.yourdomain.com
DB_PORT=3306
DB_NAME=ai_scoring_prod
```
## 安全注意事項
### 🔒 敏感信息保護
1. **API 密鑰**
- 不要將真實的 API 密鑰提交到版本控制
- 在生產環境中使用不同的 API 密鑰
2. **資料庫密碼**
- 使用強密碼
- 定期更換密碼
- 限制資料庫訪問權限
3. **環境變量文件**
- `.env.local` 已在 `.gitignore` 中
- 不要在代碼中硬編碼敏感信息
### ⚠️ 重要提醒
1. **環境變量命名**
- 客戶端變量必須以 `NEXT_PUBLIC_` 開頭
- 變量名稱區分大小寫
2. **回退機制**
- 所有配置都有合理的默認值
- 確保在環境變量未設置時仍能運行
3. **配置驗證**
- 使用 `validateConfig()` 檢查配置
- 在應用啟動時驗證關鍵配置
## 故障排除
### 🐛 常見問題
1. **資料庫連接失敗**
```bash
# 檢查資料庫配置
echo $DB_HOST
echo $DB_NAME
echo $DB_USER
```
2. **Gemini API 錯誤**
```bash
# 檢查 API 密鑰
echo $GEMINI_API_KEY
```
3. **分享連結錯誤**
```bash
# 檢查應用 URL
echo $NEXT_PUBLIC_APP_URL
```
### 🔧 解決步驟
1. **確認環境變量設置**
```bash
# 檢查 .env.local 文件
cat .env.local
```
2. **重啟服務器**
```bash
npm run dev
```
3. **運行配置檢查**
```bash
node scripts/check-config.js
```
## 結論
通過完整的環境變量配置,應用現在具備了:
-**靈活性**:支援多環境部署
-**安全性**:敏感信息通過環境變量管理
-**可維護性**:統一的配置管理
-**穩定性**:回退機制確保正常運行
-**可驗證性**:配置檢查工具
現在您可以輕鬆地在不同環境中部署應用,只需設置相應的環境變量即可!