melhorias em estáticos

This commit is contained in:
Luiz Silva 2024-05-07 08:10:35 -03:00
parent 942c1e7dfc
commit 5c7a3ee19a
7 changed files with 173 additions and 160 deletions

View file

@ -1,25 +1,23 @@
import { globSync } from "glob";
import path from "node:path";
import fs from "node:fs";
import { cdn_carro_de_boi } from "~comuns";
const pasta_estaticos = path.resolve(process.cwd(), "estaticos");
const pasta_src = path.resolve(process.cwd(), "src");
import { ambiente } from "./ambiente";
const { PREFIXO, PORTA } = ambiente;
const enderecoLocal = `http://127.0.0.1:${PORTA}${PREFIXO}`;
export const gerar = (async () => {
// listar arquivos da pasta estáticos
const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true });
// listar arquivos da pasta estáticos
const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true });
const arquivo_ts = `
const arquivo_ts = `
export const Prefixos = {
relativo: "/estaticos",
"link-local": "http://localhost:5020/estaticos",
"link-servidor": "https://carro-de-boi.idz.one/estaticos",
"link-servidor": "${cdn_carro_de_boi}/estaticos",
"relativo-disco": "~estaticos/estaticos",
};
@ -36,7 +34,7 @@ export const gerar = (async () => {
const prefixo = Prefixos[caminho] || Prefixos.relativo;
return ${(() => {
/*
/*
${files
.map((f) => f.slice(pasta_estaticos.length))
@ -45,43 +43,43 @@ export const gerar = (async () => {
*/
type tp = {
[key: string]: string | tp;
};
type tp = {
[key: string]: string | tp;
};
const arquivos = {} as tp;
const arquivos = {} as tp;
for (const arquivo of files) {
const partes = arquivo
.slice(pasta_estaticos.length)
.split("/")
.slice(1);
let pasta = arquivos;
for (const [i, parte] of partes.entries()) {
if (i === partes.length - 1) {
pasta[parte] = `~~~\${prefixo}${encodeURI(
arquivo.slice(pasta_estaticos.length),
)}~~~`;
} else {
pasta[parte] = pasta[parte] || {};
pasta = pasta[parte] as tp;
}
}
}
for (const arquivo of files) {
const partes = arquivo
.slice(pasta_estaticos.length)
.split("/")
.slice(1);
let pasta = arquivos;
for (const [i, parte] of partes.entries()) {
if (i === partes.length - 1) {
pasta[parte] = `~~~\${prefixo}${encodeURI(
arquivo.slice(pasta_estaticos.length),
)}~~~`;
} else {
pasta[parte] = pasta[parte] || {};
pasta = pasta[parte] as tp;
}
}
}
return JSON.stringify(arquivos, null, 2)
.replace(/"~~~/g, "`")
.replace(/~~~"/g, "`");
})()};
return JSON.stringify(arquivos, null, 2)
.replace(/"~~~/g, "`")
.replace(/~~~"/g, "`");
})()};
};
`;
// escrever arquivo
fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts);
// escrever arquivo
fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts);
})();
if (process.argv.includes("gerar")) {
gerar.then(() => {
console.log("Arquivo gerado com sucesso");
});
gerar.then(() => {
console.log("Arquivo gerado com sucesso");
});
}

View file

@ -13,95 +13,95 @@ const { PORTA, PREFIXO } = ambiente;
const _iframe = String(Math.random());
const criarHtml = (entrada: {
[key: string]: string | {};
// biome-ignore lint/complexity/noBannedTypes: <explanation>
[key: string]: string | {};
}): React.JSX.Element[] => {
const retorno = [] as React.JSX.Element[];
const retorno = [] as React.JSX.Element[];
for (const [k, v] of Object.entries(entrada)) {
if (typeof v === "string") {
retorno.push(
<p key={Math.random()}>
<a target={_iframe} href={`${v}?aleatório=${Math.random()}`}>
{v}
</a>
</p>,
);
} else {
retorno.push(
<div
key={Math.random()}
style={{
margin: 10,
padding: 5,
borderColor: "black",
borderWidth: "1px",
borderStyle: "solid",
}}
>
<details open>
<summary>{k}</summary>
{criarHtml(v)}
</details>
</div>,
);
}
}
for (const [k, v] of Object.entries(entrada)) {
if (typeof v === "string") {
retorno.push(
<p key={Math.random()}>
<a target={_iframe} href={`${v}?aleatório=${Math.random()}`}>
{v}
</a>
</p>,
);
} else {
retorno.push(
<div
key={Math.random()}
style={{
margin: 10,
padding: 5,
borderColor: "black",
borderWidth: "1px",
borderStyle: "solid",
}}
>
<details open>
<summary>{k}</summary>
{criarHtml(v)}
</details>
</div>,
);
}
}
return retorno;
return retorno;
};
gerar.then(() => {
const app = express();
app.use(express.json());
app.use(express.urlencoded());
const app = express();
app.use(express.json());
app.use(express.urlencoded());
app.get([PREFIXO, `${PREFIXO}/`], (req, res) => {
const html = (
<html lang="pt-BR">
<head>
<title>Arquivos Estáticos</title>
</head>{" "}
<body>
<h4>Arquivos Estáticos</h4>
<table width={"100%"} >
<tr>
<td width={"50%"}> {criarHtml(estaticos("relativo"))} </td>
<td width={"50%"}>
<iframe
title="Iframe"
name={_iframe}
height={600}
width={"100%"}
/>
</td>
</tr>
</table>
</body>
</html>
);
app.get([PREFIXO, `${PREFIXO}/`], (req, res) => {
const html = (
<html lang="pt-BR">
<head>
<title>Arquivos Estáticos</title>
</head>{" "}
<body>
<h4>Arquivos Estáticos</h4>
<table width={"100%"}>
<tr>
<td width={"50%"}> {criarHtml(estaticos("relativo"))} </td>
<td width={"50%"}>
<iframe
title="Iframe"
name={_iframe}
height={600}
width={"100%"}
/>
</td>
</tr>
</table>
</body>
</html>
);
const renderHtml = renderToStaticMarkup(html, {});
const renderHtml = renderToStaticMarkup(html, {});
res.setHeader("content-type", "text/html");
res.send(renderHtml);
});
app.get(`${PREFIXO}/*`, (req, res) => {
const caminho_arquivo = path.join(process.cwd(), decodeURI(req.path));
res.setHeader("content-type", "text/html");
res.send(renderHtml);
});
app.get(`${PREFIXO}/*`, (req, res) => {
const caminho_arquivo = path.join(process.cwd(), decodeURI(req.path));
if (fs.existsSync(caminho_arquivo)) {
console.log(`200: ${req.url}`);
return res.sendFile(caminho_arquivo);
} else {
console.log(`404: ${req.url}`);
return res.status(404).send(`404: ${req.url}`);
}
});
if (fs.existsSync(caminho_arquivo)) {
console.log(`200: ${req.url}`);
return res.sendFile(caminho_arquivo);
}
console.log(`404: ${req.url}`);
return res.status(404).send(`404: ${req.url}`);
});
app.all("*", (req, res) => {
res.redirect(PREFIXO);
});
app.all("*", (req, res) => {
res.redirect(PREFIXO);
});
app.listen(PORTA, () => {
console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`);
});
app.listen(PORTA, () => {
console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`);
});
});