Refactor: / (main.ts) -> /get (get.ts), /start (start.ts) -> / (index.ts)

This commit is contained in:
DarkCat09 2023-08-15 16:03:28 +04:00
parent 4dbc4fc8a0
commit 871a3cecfd
No known key found for this signature in database
GPG Key ID: 4785B6FB1C50A540
7 changed files with 98 additions and 96 deletions

View File

@ -5,9 +5,9 @@ import Fastify from "fastify";
import fastifyView from "@fastify/view";
import ejs from "ejs";
import mainRoute from "./routes/main";
import getRoute from "./routes/get";
import parseRoute from "./routes/parse";
import startRoute from "./routes/start";
import indexRoute from "./routes/index";
class App {
config: IConfigService;
@ -27,9 +27,9 @@ class App {
},
});
fastify.register(mainRoute);
fastify.register(indexRoute);
fastify.register(getRoute);
fastify.register(parseRoute);
fastify.register(startRoute);
fastify.listen({ port: Number(this.config.get("PORT")) }, (err) => {
err && console.log(err);

View File

@ -1,16 +1,18 @@
import { IHandlerOutput } from "./handler.interface";
import axios from "../types/axios";
import { JSDOM } from "jsdom";
import { DOMWindow } from "jsdom";
import readability from "./readability";
import google from "./google";
import { DOMWindow } from "jsdom";
export default async function handlePage(
url: string,
originalUrl: string,
engine?: string
): Promise<IHandlerOutput> {
if (engine && engineList.indexOf(engine) === -1) {
throw new Error("Invalid engine");
}
@ -21,7 +23,7 @@ export default async function handlePage(
const UrlParsed = new URL(originalUrl);
[...window.document.getElementsByTagName("a")].forEach((link) => {
link.href = `${UrlParsed.origin}/?url=${link.href}${
link.href = `${UrlParsed.origin}/get?url=${link.href}${
engine ? `&engine=${engine}` : ""
}`;
});

View File

@ -4,8 +4,8 @@ import { GetRequest } from "../types/requests";
import handlePage from "../handlers/main";
import { generateOriginUrl } from "../utils";
export default async function mainRoute(fastify: FastifyInstance) {
fastify.get("/", async (request: GetRequest, reply) => {
export default async function getRoute(fastify: FastifyInstance) {
fastify.get("/get", async (request: GetRequest, reply) => {
const remoteUrl = request.query.url;
const engine = request.query.engine;

View File

@ -1,8 +1,8 @@
import { FastifyInstance } from "fastify";
import { engineList } from "../handlers/main";
export default async function parseRoute(fastify: FastifyInstance) {
fastify.get("/start", async (_, reply) => {
export default async function indexRoute(fastify: FastifyInstance) {
fastify.get("/", async (_, reply) => {
return reply.view("/templates/start.ejs", { engineList });
});
}

14
templates/get.ejs Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="<%= parsed.lang %>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="robots" content="noindex, nofollow">
<title><%= parsed.title %></title>
</head>
<body>
<h1><%= parsed.title %></h1>
<main><%- parsed.content %></main>
</body>
</html>

View File

@ -1,14 +1,80 @@
<% const desc = "txtdot is a HTTP proxy that parses text, links and pictures from pages reducing internet traffic, removing ads and heavy scripts" %>
<!DOCTYPE html>
<html lang="<%= parsed.lang %>">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="robots" content="noindex, nofollow">
<title><%= parsed.title %></title>
<meta name="description" content="<%= desc %>">
<title>txt. main page</title>
<!-- CSS must be moved to a separate file later -->
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
font-size: 1.1rem;
}
main {
max-width: 50rem;
width: fit-content;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
}
.input-grid {
display: grid;
grid-template-columns: auto min-content;
width: fit-content;
}
.input-row {
display: flex;
flex-direction: row;
align-items: center;
}
#url {
width: 100%;
}
</style>
</head>
<body>
<h1><%= parsed.title %></h1>
<main><%- parsed.content %></main>
<main>
<header>
<h1>txt.</h1>
<p><%= desc %></p>
</header>
<form action="/get" method="get" class="input-grid">
<div class="input">
<input type="text" name="url" id="url" placeholder="URL">
</div>
<div class="input">
<input type="submit" id="submit" value="Parse">
</div>
<div class="input-row">
<div class="input">
<label for="engine">Engine</label>
<select name="engine">
<option value="" selected>Default</option>
<% engineList.forEach((engine) => { %>
<option value="<%= engine %>">
<%= engine %>
</option>
<% }) %>
</select>
</div>
<div class="input">
<label for="format">Format</label>
<select name="format">
<option value="html" selected>HTML</option>
<option value="text">Text</option>
</select>
</div>
</div>
</form>
</main>
</body>
</html>

View File

@ -1,80 +0,0 @@
<% const desc = "txtdot is a HTTP proxy that parses text, links and pictures from pages reducing internet traffic, removing ads and heavy scripts" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="<%= desc %>">
<title>txt. main page</title>
<!-- CSS must be moved to a separate file later -->
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
font-size: 1.1rem;
}
main {
max-width: 50rem;
width: fit-content;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
}
.input-grid {
display: grid;
grid-template-columns: auto min-content;
width: fit-content;
}
.input-row {
display: flex;
flex-direction: row;
align-items: center;
}
#url {
width: 100%;
}
</style>
</head>
<body>
<main>
<header>
<h1>txt.</h1>
<p><%= desc %></p>
</header>
<form action="/" method="get" class="input-grid">
<div class="input">
<input type="text" name="url" id="url" placeholder="URL">
</div>
<div class="input">
<input type="submit" id="submit" value="Parse">
</div>
<div class="input-row">
<div class="input">
<label for="engine">Engine</label>
<select name="engine">
<option value="" selected>Default</option>
<% engineList.forEach((engine) => { %>
<option value="<%= engine %>">
<%= engine %>
</option>
<% }) %>
</select>
</div>
<div class="input">
<label for="format">Format</label>
<select name="format">
<option value="html" selected>HTML</option>
<option value="text">Text</option>
</select>
</div>
</div>
</form>
</main>
</body>
</html>