85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
import { Express, Response } from "express";
|
|
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
|
|
const METASET_HOST = process.env.METASET_HOST || "metaset";
|
|
const METASET_PORT = parseInt(process.env.METASET_PORT || "8100", 10);
|
|
const METASET_TIMEOUT = 60000;
|
|
|
|
// Consulta o Kernel Registry para descobrir onde o Metaset está rodando
|
|
async function discoverMetasetEndpoint(): Promise<string | null> {
|
|
try {
|
|
// PRIORIDADE 1: Registry global (mesmo processo)
|
|
const globalRegistry = (global as any).arcadiaKernelRegistry;
|
|
if (globalRegistry) {
|
|
const services = globalRegistry.getServices();
|
|
const metasetService = services.find((s: any) =>
|
|
s.id === 'metaset' ||
|
|
s.name?.toLowerCase().includes('metaset') ||
|
|
s.displayName?.toLowerCase().includes('metaset')
|
|
);
|
|
|
|
if (metasetService?.endpoint) {
|
|
console.log(`[Metaset Proxy] Discovery via Kernel Registry: ${metasetService.endpoint}`);
|
|
return metasetService.endpoint;
|
|
}
|
|
}
|
|
|
|
// PRIORIDADE 2: HTTP request para porta 5001
|
|
const response = await fetch('http://127.0.0.1:5001/api/services');
|
|
if (response.ok) {
|
|
const services = await response.json();
|
|
const metasetService = services.find((s: any) =>
|
|
s.id === 'metaset' ||
|
|
s.name?.toLowerCase().includes('metaset')
|
|
);
|
|
|
|
if (metasetService?.endpoint) {
|
|
console.log(`[Metaset Proxy] Discovery via HTTP: ${metasetService.endpoint}`);
|
|
return metasetService.endpoint;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log('[Metaset Proxy] Discovery falhou, usando fallback:', error);
|
|
}
|
|
|
|
// FALLBACK: Configuração padrão
|
|
return null;
|
|
}
|
|
|
|
export async function setupMetasetProxy(app: Express): Promise<void> {
|
|
// Tenta descobrir endpoint via Kernel Discovery
|
|
const discoveredEndpoint = await discoverMetasetEndpoint();
|
|
const target = discoveredEndpoint || `http://${METASET_HOST}:${METASET_PORT}`;
|
|
|
|
console.log(`[Metaset Proxy] Configurando proxy -> ${target}`);
|
|
|
|
const metasetProxy = createProxyMiddleware({
|
|
target,
|
|
changeOrigin: true,
|
|
timeout: METASET_TIMEOUT,
|
|
proxyTimeout: METASET_TIMEOUT,
|
|
pathRewrite: { "^/metaset": "" },
|
|
on: {
|
|
error: (err, _req, res) => {
|
|
console.error("[Metaset Proxy] Error:", err.message);
|
|
if (res && typeof (res as Response).status === "function") {
|
|
(res as Response).status(502).json({
|
|
error: "Metaset indisponível",
|
|
message: "O Metaset BI está iniciando. Tente novamente em alguns segundos.",
|
|
target,
|
|
});
|
|
}
|
|
},
|
|
proxyRes: (proxyRes) => {
|
|
const location = proxyRes.headers["location"];
|
|
if (location && typeof location === "string" && location.startsWith("/")) {
|
|
proxyRes.headers["location"] = `/metaset${location}`;
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
app.use("/metaset", metasetProxy);
|
|
console.log(`[Metaset Proxy] Ativo em /metaset -> ${target}`);
|
|
}
|