troca por Fastify
This commit is contained in:
parent
ad6dcda6a5
commit
67aeeea9b4
5 changed files with 685 additions and 798 deletions
|
|
@ -2,18 +2,18 @@ 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;
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
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";
|
||||
import bodyParser from 'body-parser'
|
||||
import { estaticos } from "../src";
|
||||
import { ambiente } from "./ambiente";
|
||||
import { gerar } from "./listar_arquivos";
|
||||
|
||||
// biome-ignore lint/style/useImportType: <explanation>
|
||||
import React, {} from "react";
|
||||
import cors from "cors";
|
||||
|
||||
import fastifyStatc from "@fastify/static";
|
||||
import Fastify, {} from "fastify";
|
||||
|
||||
const { PORTA, PREFIXO } = ambiente;
|
||||
|
||||
|
|
@ -55,58 +54,68 @@ const criarHtml = (entrada: {
|
|||
};
|
||||
|
||||
gerar.then(() => {
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded());
|
||||
app.use(cors());
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
const fastify = Fastify({
|
||||
logger: true,
|
||||
|
||||
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);
|
||||
}
|
||||
console.log(`404: ${req.url}`);
|
||||
return res.status(404).send(`404: ${req.url}`);
|
||||
ignoreTrailingSlash: true,
|
||||
});
|
||||
|
||||
app.all("*", (req, res) => {
|
||||
res.redirect(PREFIXO);
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: "/",
|
||||
handler: (req, res) => {
|
||||
res.redirect(`${PREFIXO}/`);
|
||||
},
|
||||
});
|
||||
|
||||
app.listen(PORTA, () => {
|
||||
console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`);
|
||||
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, {});
|
||||
|
||||
// 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,
|
||||
});
|
||||
|
||||
// 404
|
||||
fastify.setNotFoundHandler((request, reply) => {
|
||||
reply.code(404).send(`Não encontrado: ${request.url}`);
|
||||
});
|
||||
|
||||
fastify.listen({ port: Number(PORTA) }, (err, address) => {
|
||||
if (err) throw err;
|
||||
console.log(`Servidor ${PREFIXO} Rodando em ${address}`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue