feat: 添加完整的部署支持
- 后端多环境配置(dev, prod) - Docker 部署支持(Dockerfile, docker-compose.yml) - Java 部署脚本(deploy.sh) - 前端构建脚本(build.sh,支持自定义后端地址) - 健康检查接口(/api/health) - 完整的部署文档(docs/DEPLOYMENT.md) - 前端环境变量支持(VITE_API_URL, VITE_WS_URL)
This commit is contained in:
@@ -5,9 +5,22 @@ import { wsManager } from './websocket'
|
||||
|
||||
/**
|
||||
* API 基础配置
|
||||
* 支持通过环境变量 VITE_API_URL 配置后端地址
|
||||
* 默认使用相对路径 /api(适用于同域部署)
|
||||
* 如果设置了 VITE_API_URL,则使用完整 URL
|
||||
*/
|
||||
const getBaseURL = (): string => {
|
||||
const envApiUrl = import.meta.env.VITE_API_URL
|
||||
if (envApiUrl) {
|
||||
// 如果设置了环境变量,使用完整 URL
|
||||
return `${envApiUrl}/api`
|
||||
}
|
||||
// 否则使用相对路径(适用于开发环境代理或同域部署)
|
||||
return '/api'
|
||||
}
|
||||
|
||||
const apiClient: AxiosInstance = axios.create({
|
||||
baseURL: '/api',
|
||||
baseURL: getBaseURL(),
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
||||
@@ -325,16 +325,28 @@ class WebSocketManager {
|
||||
|
||||
/**
|
||||
* 获取 WebSocket URL(带token认证)
|
||||
* 支持通过环境变量 VITE_WS_URL 配置 WebSocket 地址
|
||||
*/
|
||||
private getWebSocketUrl(): string {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
const host = window.location.host
|
||||
const envWsUrl = import.meta.env.VITE_WS_URL
|
||||
let wsBaseUrl: string
|
||||
|
||||
if (envWsUrl) {
|
||||
// 如果设置了环境变量,使用完整 URL
|
||||
wsBaseUrl = envWsUrl
|
||||
} else {
|
||||
// 否则使用相对路径(适用于开发环境代理或同域部署)
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
const host = window.location.host
|
||||
wsBaseUrl = `${protocol}//${host}`
|
||||
}
|
||||
|
||||
const token = this.getToken()
|
||||
if (token) {
|
||||
// 通过查询参数传递token
|
||||
return `${protocol}//${host}/ws?token=${encodeURIComponent(token)}`
|
||||
return `${wsBaseUrl}/ws?token=${encodeURIComponent(token)}`
|
||||
}
|
||||
return `${protocol}//${host}/ws`
|
||||
return `${wsBaseUrl}/ws`
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user