fix(frontend): use relative baseURL in production, avoid hardcoded localhost
Previously baseURL defaulted to 'http://localhost:5001' in all environments, causing issues when deployed on servers or Docker with port mapping. Changes: - Use relative path (empty string) in production for same-origin deployment - Keep localhost:5001 default for development environment - Still respects VITE_API_BASE_URL env var if set Fixes #93, related to #59, #57
This commit is contained in:
parent
985f89f49a
commit
1e3451b058
|
|
@ -1,8 +1,21 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
// 创建axios实例
|
// 创建axios实例
|
||||||
|
// 优先级: VITE_API_BASE_URL > 生产环境相对路径 > 开发环境localhost
|
||||||
|
const getBaseURL = () => {
|
||||||
|
if (import.meta.env.VITE_API_BASE_URL) {
|
||||||
|
return import.meta.env.VITE_API_BASE_URL
|
||||||
|
}
|
||||||
|
// 生产环境使用相对路径(同源部署)
|
||||||
|
if (import.meta.env.PROD) {
|
||||||
|
return '' // 相对路径,使用当前域名
|
||||||
|
}
|
||||||
|
// 开发环境默认localhost
|
||||||
|
return 'http://localhost:5001'
|
||||||
|
}
|
||||||
|
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5001',
|
baseURL: getBaseURL(),
|
||||||
timeout: 300000, // 5分钟超时(本体生成可能需要较长时间)
|
timeout: 300000, // 5分钟超时(本体生成可能需要较长时间)
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue