Primeiro commit
This commit is contained in:
commit
9135911bd7
33 changed files with 48927 additions and 0 deletions
19
ferramentas/ambiente.ts
Normal file
19
ferramentas/ambiente.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { z } from "zod";
|
||||
import { PORTA, PREFIXO } from "../PREFIXO";
|
||||
|
||||
const validar = z
|
||||
.object({
|
||||
PORTA: z.string(),
|
||||
PREFIXO: z.string().regex(/^\/\w+$/),
|
||||
})
|
||||
.safeParse({ PORTA, PREFIXO });
|
||||
|
||||
if ("error" in validar) {
|
||||
throw new Error(
|
||||
validar.error.errors
|
||||
.map((erro) => `${erro.path}: ${erro.message}`)
|
||||
.join("\n"),
|
||||
);
|
||||
}
|
||||
|
||||
export const ambiente = validar.data;
|
||||
87
ferramentas/listar_arquivos.ts
Normal file
87
ferramentas/listar_arquivos.ts
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { globSync } from "glob";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
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 });
|
||||
|
||||
const arquivo_ts = `
|
||||
|
||||
|
||||
|
||||
export const Prefixos = {
|
||||
relativo: "/estaticos",
|
||||
"link-local": "http://localhost:5020/estaticos",
|
||||
"link-servidor": "https://carro-de-boi.idz.one/estaticos",
|
||||
"relativo-disco": "~estaticos/estaticos",
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param caminho
|
||||
** "relativo" - Caminho relativo Ex: "/estaticos/favicon.ico"
|
||||
** "link-local" - Caminho link local Ex: "http://localhost:5020/estaticos/favicon.ico"
|
||||
** "link-servidor" - Caminho link servidor Ex: "https://carro-de-boi.idz.one/estaticos/favicon.ico"
|
||||
** "relativo-disco" - Caminho relativo ao disco Ex: "/~estaticos/estaticos/favicon.ico"
|
||||
* @returns String
|
||||
*/
|
||||
export const estaticos = (caminho: keyof typeof Prefixos) => {
|
||||
const prefixo = Prefixos[caminho] || Prefixos.relativo;
|
||||
|
||||
return ${(() => {
|
||||
/*
|
||||
|
||||
${files
|
||||
.map((f) => f.slice(pasta_estaticos.length))
|
||||
.map((f) => `"${f}": \`\${prefixo}${encodeURI(f)}\``)
|
||||
.join(",\n")}
|
||||
|
||||
|
||||
*/
|
||||
type tp = {
|
||||
[key: string]: string | 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify(arquivos, null, 2)
|
||||
.replace(/"~~~/g, "`")
|
||||
.replace(/~~~"/g, "`");
|
||||
})()};
|
||||
};
|
||||
`;
|
||||
|
||||
// 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");
|
||||
});
|
||||
}
|
||||
107
ferramentas/rodar_servidor.tsx
Normal file
107
ferramentas/rodar_servidor.tsx
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import { estaticos } from "../src";
|
||||
import path from "node:path";
|
||||
import express from "express";
|
||||
import fs from "node:fs";
|
||||
import { gerar } from "./listar_arquivos";
|
||||
import { ambiente } from "./ambiente";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
|
||||
// biome-ignore lint/style/useImportType: <explanation>
|
||||
import React, {} from "react";
|
||||
const { PORTA, PREFIXO } = ambiente;
|
||||
|
||||
const _iframe = String(Math.random());
|
||||
|
||||
const criarHtml = (entrada: {
|
||||
[key: string]: string | {};
|
||||
}): 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>,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return retorno;
|
||||
};
|
||||
|
||||
gerar.then(() => {
|
||||
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>
|
||||
);
|
||||
|
||||
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));
|
||||
|
||||
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}`);
|
||||
}
|
||||
});
|
||||
|
||||
app.all("*", (req, res) => {
|
||||
res.redirect(PREFIXO);
|
||||
});
|
||||
|
||||
app.listen(PORTA, () => {
|
||||
console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue