.
This commit is contained in:
parent
e8eda7ff32
commit
127061d04c
11 changed files with 912 additions and 914 deletions
|
|
@ -1,19 +1,19 @@
|
|||
import { z } from "zod";
|
||||
import { PORTA, PREFIXO } from "../PREFIXO";
|
||||
import { z } from "zod"
|
||||
import { PORTA, PREFIXO } from "../PREFIXO"
|
||||
|
||||
const validar = z
|
||||
.object({
|
||||
PORTA: z.string(),
|
||||
PREFIXO: z.string().regex(/^\/\w+$/),
|
||||
})
|
||||
.safeParse({ PORTA, PREFIXO });
|
||||
.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"),
|
||||
);
|
||||
throw new Error(
|
||||
validar.error?.errors
|
||||
.map((erro) => `${erro.path}: ${erro.message}`)
|
||||
.join("\n"),
|
||||
)
|
||||
}
|
||||
|
||||
export const ambiente = validar.data;
|
||||
export const ambiente = validar.data
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
import path from "node:path";
|
||||
import { cdn_carro_de_boi } from "p-comuns";
|
||||
import { mapearPasta } from "../src/mapear_pasta";
|
||||
import path from "node:path"
|
||||
import { cdn_carro_de_boi } from "p-comuns"
|
||||
import { mapearPasta } from "../src/mapear_pasta"
|
||||
|
||||
const pasta_estaticos = path.resolve(process.cwd(), "estaticos");
|
||||
const pasta_src = path.resolve(process.cwd(), "src");
|
||||
const version = process.env.npm_package_version;
|
||||
const pasta_estaticos = path.resolve(process.cwd(), "estaticos")
|
||||
const pasta_src = path.resolve(process.cwd(), "src")
|
||||
const _version = process.env.npm_package_version
|
||||
|
||||
export const gerar = (async () => {
|
||||
mapearPasta({
|
||||
prefixos: {
|
||||
relativo: "/estaticos",
|
||||
"link-local": "http://localhost:5020/estaticos",
|
||||
"link-servidor": `${cdn_carro_de_boi}/estaticos`,
|
||||
node_modules: "node_modules/p-estaticos/estaticos",
|
||||
},
|
||||
arquivoDestino: path.resolve(pasta_src, "index.ts"),
|
||||
pasta: pasta_estaticos,
|
||||
});
|
||||
})();
|
||||
mapearPasta({
|
||||
prefixos: {
|
||||
relativo: "/estaticos",
|
||||
"link-local": "http://localhost:5020/estaticos",
|
||||
"link-servidor": `${cdn_carro_de_boi}/estaticos`,
|
||||
node_modules: "node_modules/p-estaticos/estaticos",
|
||||
},
|
||||
arquivoDestino: path.resolve(pasta_src, "index.ts"),
|
||||
pasta: pasta_estaticos,
|
||||
})
|
||||
})()
|
||||
|
||||
if (process.argv.includes("gerar")) {
|
||||
gerar.then(() => {
|
||||
console.log("Arquivo gerado com sucesso");
|
||||
});
|
||||
gerar.then(() => {
|
||||
console.log("Arquivo gerado com sucesso")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,164 +1,163 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import cors from "@fastify/cors";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { estaticos } from "../src";
|
||||
import { ambiente } from "./ambiente";
|
||||
import { gerar } from "./listar_arquivos";
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
import cors from "@fastify/cors"
|
||||
import fastifyStatc from "@fastify/static"
|
||||
import dayjs from "dayjs"
|
||||
import Fastify from "fastify"
|
||||
import type React from "react"
|
||||
import { renderToStaticMarkup } from "react-dom/server"
|
||||
import { estaticos } from "../src"
|
||||
import { ambiente } from "./ambiente"
|
||||
import { gerar } from "./listar_arquivos"
|
||||
|
||||
// biome-ignore lint/style/useImportType: <explanation>
|
||||
import React, {} from "react";
|
||||
const { PORTA, PREFIXO } = ambiente
|
||||
|
||||
import fastifyStatc from "@fastify/static";
|
||||
import dayjs from "dayjs";
|
||||
import Fastify, {} from "fastify";
|
||||
|
||||
const { PORTA, PREFIXO } = ambiente;
|
||||
|
||||
const _iframe = String(Math.random());
|
||||
const _iframe = String(Math.random())
|
||||
|
||||
const criarHtml = (entrada: {
|
||||
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
||||
[key: string]: string | {};
|
||||
[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>
|
||||
<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>
|
||||
<summary>{k}</summary>
|
||||
{criarHtml(v)}
|
||||
</details>
|
||||
</div>,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return retorno;
|
||||
};
|
||||
return retorno
|
||||
}
|
||||
|
||||
gerar.then(() => {
|
||||
const fastify = Fastify({
|
||||
logger: false,
|
||||
const fastify = Fastify({
|
||||
logger: false,
|
||||
|
||||
ignoreTrailingSlash: true,
|
||||
});
|
||||
ignoreTrailingSlash: true,
|
||||
})
|
||||
|
||||
fastify.addHook("onSend", async (request, reply) => {
|
||||
console.log(
|
||||
[
|
||||
dayjs().format("YYYY-MM-DD HH:mm:ss"),
|
||||
request.method,
|
||||
request.url,
|
||||
reply.statusCode,
|
||||
request.headers.site || request.headers.referer,
|
||||
].join(" "),
|
||||
);
|
||||
});
|
||||
fastify.addHook("onSend", async (request, reply) => {
|
||||
console.log(
|
||||
[
|
||||
dayjs().format("YYYY-MM-DD HH:mm:ss"),
|
||||
request.method,
|
||||
request.url,
|
||||
reply.statusCode,
|
||||
request.headers.site || request.headers.referer,
|
||||
].join(" "),
|
||||
)
|
||||
})
|
||||
|
||||
//cors
|
||||
fastify.register(cors, {
|
||||
origin: "*",
|
||||
});
|
||||
//cors
|
||||
fastify.register(cors, {
|
||||
origin: "*",
|
||||
})
|
||||
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: "/",
|
||||
handler: (req, res) => {
|
||||
res.redirect(`${PREFIXO}/`);
|
||||
},
|
||||
});
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: "/",
|
||||
handler: (_req, res) => {
|
||||
res.redirect(`${PREFIXO}/`)
|
||||
},
|
||||
})
|
||||
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: `${PREFIXO}/`,
|
||||
handler: async (request, reply) => {
|
||||
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>
|
||||
);
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: `${PREFIXO}/`,
|
||||
handler: async (_request, reply) => {
|
||||
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, {})
|
||||
|
||||
// htlm utf-8
|
||||
reply.header("content-type", "text/html; charset=utf-8");
|
||||
reply.send(renderHtml);
|
||||
},
|
||||
});
|
||||
// htlm utf-8
|
||||
reply.header("content-type", "text/html; charset=utf-8")
|
||||
reply.send(renderHtml)
|
||||
},
|
||||
})
|
||||
|
||||
fastify.register(fastifyStatc, {
|
||||
root: path.join(process.cwd(), "estaticos"),
|
||||
prefix: PREFIXO,
|
||||
});
|
||||
fastify.register(fastifyStatc, {
|
||||
root: path.join(process.cwd(), "estaticos"),
|
||||
prefix: PREFIXO,
|
||||
})
|
||||
|
||||
// 404
|
||||
fastify.setNotFoundHandler((request, reply) => {
|
||||
const html404 = fs.readFileSync(
|
||||
path.join(process.cwd(), estaticos("relativo").html["404.html"]),
|
||||
"utf8",
|
||||
);
|
||||
// 404
|
||||
fastify.setNotFoundHandler((request, reply) => {
|
||||
const html404 = fs.readFileSync(
|
||||
path.join(process.cwd(), estaticos("relativo").html["404.html"]),
|
||||
"utf8",
|
||||
)
|
||||
|
||||
reply.header("content-type", "text/html; charset=utf-8");
|
||||
reply.header("content-type", "text/html; charset=utf-8")
|
||||
|
||||
reply
|
||||
.code(404)
|
||||
.send(html404.replace("{ERRO}", `Página não encontrada: ${request.url}`));
|
||||
});
|
||||
reply
|
||||
.code(404)
|
||||
.send(html404.replace("{ERRO}", `Página não encontrada: ${request.url}`))
|
||||
})
|
||||
|
||||
const jaEstaRodando = fetch(`http://0.0.0.0:${PORTA}`)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
const jaEstaRodando = fetch(`http://0.0.0.0:${PORTA}`)
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
|
||||
jaEstaRodando.then((jaEstaRodando) => {
|
||||
!jaEstaRodando &&
|
||||
fastify.listen(
|
||||
{ port: Number(PORTA), host: "0.0.0.0" },
|
||||
(err, address) => {
|
||||
if (err) throw err;
|
||||
console.log(
|
||||
`${new Date().toISOString()} Servidor ${PREFIXO} Rodando em ${address}`,
|
||||
);
|
||||
},
|
||||
);
|
||||
jaEstaRodando.then((jaEstaRodando) => {
|
||||
!jaEstaRodando &&
|
||||
fastify.listen(
|
||||
{ port: Number(PORTA), host: "0.0.0.0" },
|
||||
(err, address) => {
|
||||
if (err) throw err
|
||||
console.log(
|
||||
`${new Date().toISOString()} Servidor ${PREFIXO} Rodando em ${address}`,
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
jaEstaRodando && console.log(`Servidor ${PREFIXO} já está rodando`);
|
||||
});
|
||||
});
|
||||
jaEstaRodando && console.log(`Servidor ${PREFIXO} já está rodando`)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue