refactor(.umirc.ts): change proxy and request in dev mode and production mode
This commit is contained in:
59
config/proxy_prod.ts
Normal file
59
config/proxy_prod.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
// Hàm lấy IP từ hostname hiện tại (chỉ hoạt động runtime)
|
||||
const getCurrentIP = () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const hostname = window.location.hostname;
|
||||
// Nếu là localhost hoặc IP local, trả về IP mặc định
|
||||
if (
|
||||
hostname === 'localhost' ||
|
||||
hostname.startsWith('192.168.') ||
|
||||
hostname.startsWith('10.')
|
||||
) {
|
||||
console.log('Host name: ', hostname);
|
||||
|
||||
return hostname;
|
||||
}
|
||||
// Nếu là domain, có thể cần map sang IP tương ứng
|
||||
return hostname;
|
||||
}
|
||||
return process.env.REACT_APP_API_HOST || '192.168.30.102'; // fallback từ env
|
||||
};
|
||||
|
||||
// Hàm tạo proxy config động
|
||||
const createDynamicProxy = () => {
|
||||
const currentIP = getCurrentIP();
|
||||
const isLocalIP =
|
||||
currentIP.startsWith('192.168.') ||
|
||||
currentIP.startsWith('10.') ||
|
||||
currentIP === 'localhost';
|
||||
|
||||
return {
|
||||
dev: {
|
||||
'/api': {
|
||||
target: `http://${currentIP}:81`,
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
test: {
|
||||
'/test': {
|
||||
target: isLocalIP
|
||||
? `http://${currentIP}:81`
|
||||
: 'https://test-sgw-device.gms.vn',
|
||||
changeOrigin: true,
|
||||
secure: !isLocalIP,
|
||||
},
|
||||
},
|
||||
prod: {
|
||||
'/test': {
|
||||
target: isLocalIP
|
||||
? `http://${currentIP}:81`
|
||||
: 'https://prod-sgw-device.gms.vn',
|
||||
changeOrigin: true,
|
||||
secure: !isLocalIP,
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const proxyProduct: Record<string, any> = createDynamicProxy();
|
||||
|
||||
export default proxyProduct;
|
||||
Reference in New Issue
Block a user