87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
// Lấy hostname từ URL hiện tại của trang web
|
|
const getCurrentHostname = () => {
|
|
// Trong build-time, sử dụng environment variable
|
|
if (typeof window === 'undefined') {
|
|
return process.env.REACT_APP_HOSTNAME || 'localhost';
|
|
}
|
|
|
|
// Trong runtime, lấy từ URL của trang web
|
|
return window.location.hostname;
|
|
};
|
|
|
|
// Lấy port cho GeoServer từ environment variable hoặc default là 8080
|
|
const getGeoserverPort = () => {
|
|
return process.env.REACT_APP_GEOSERVER_PORT || '8080';
|
|
};
|
|
|
|
// Tạo proxy config động dựa trên hostname hiện tại
|
|
const createDynamicProxy = () => {
|
|
const hostname = getCurrentHostname();
|
|
const geoserverPort = getGeoserverPort();
|
|
const isLocalhost =
|
|
hostname === 'localhost' ||
|
|
hostname.startsWith('192.168.') ||
|
|
hostname.startsWith('10.') ||
|
|
hostname.startsWith('127.');
|
|
|
|
// Với production, ta sẽ proxy:
|
|
// - /api đến cùng hostname với port 81
|
|
// - /geoserver đến cùng hostname với port GEOSERVER_PORT (default: 8080)
|
|
// Ví dụ: nếu truy cập https://example.com thì API sẽ đến http://example.com:81/api
|
|
// và GeoServer sẽ đến http://example.com:[GEOSERVER_PORT]/geoserver
|
|
return {
|
|
dev: {
|
|
'/api': {
|
|
target: `http://${hostname}:81`,
|
|
changeOrigin: true,
|
|
pathRewrite: { '^/api': '/api' },
|
|
},
|
|
'/geoserver': {
|
|
target: `http://${hostname}:${geoserverPort}`,
|
|
changeOrigin: true,
|
|
pathRewrite: { '^/geoserver': '/geoserver' },
|
|
},
|
|
},
|
|
test: {
|
|
'/api': {
|
|
target: isLocalhost
|
|
? `http://${hostname}:81`
|
|
: `https://${hostname}:81`,
|
|
changeOrigin: true,
|
|
secure: !isLocalhost,
|
|
pathRewrite: { '^/api': '/api' },
|
|
},
|
|
'/geoserver': {
|
|
target: isLocalhost
|
|
? `http://${hostname}:${geoserverPort}`
|
|
: `https://${hostname}:${geoserverPort}`,
|
|
changeOrigin: true,
|
|
secure: !isLocalhost,
|
|
pathRewrite: { '^/geoserver': '/geoserver' },
|
|
},
|
|
},
|
|
prod: {
|
|
'/api': {
|
|
target: isLocalhost
|
|
? `http://${hostname}:81`
|
|
: `https://${hostname}:81`,
|
|
changeOrigin: true,
|
|
secure: !isLocalhost,
|
|
pathRewrite: { '^/api': '/api' },
|
|
},
|
|
'/geoserver': {
|
|
target: isLocalhost
|
|
? `http://${hostname}:${geoserverPort}`
|
|
: `https://${hostname}:${geoserverPort}`,
|
|
changeOrigin: true,
|
|
secure: !isLocalhost,
|
|
pathRewrite: { '^/geoserver': '/geoserver' },
|
|
},
|
|
},
|
|
};
|
|
};
|
|
|
|
const proxyProd: Record<string, any> = createDynamicProxy();
|
|
|
|
export default proxyProd;
|