60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
// 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;
|