txtdot/src/app.ts

73 lines
1.7 KiB
TypeScript
Raw Normal View History

import path from "path";
2023-08-14 13:05:34 +03:00
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import fastifyView from "@fastify/view";
2023-08-16 10:36:04 +03:00
import fastifySwagger from "@fastify/swagger";
2023-08-16 10:49:55 +03:00
import fastifySwaggerUi from "@fastify/swagger-ui";
import ejs from "ejs";
2023-08-14 13:59:32 +03:00
2023-09-21 12:51:25 +03:00
import indexRoute from "./routes/browser/index";
2023-08-26 11:46:36 +03:00
import getRoute from "./routes/browser/get";
2023-09-21 12:51:25 +03:00
import proxyRoute from "./routes/browser/proxy";
2023-08-26 11:46:36 +03:00
import parseRoute from "./routes/api/parse";
import rawHtml from "./routes/api/raw-html";
2023-08-14 13:59:32 +03:00
import publicConfig from "./publicConfig";
2023-08-16 13:20:24 +03:00
import errorHandler from "./errors/handler";
2023-09-21 15:28:55 +03:00
import getConfig from "./config/main";
2023-08-13 20:38:33 +03:00
class App {
2023-08-14 13:03:19 +03:00
async init() {
2023-09-21 15:28:55 +03:00
const config = getConfig();
2023-08-14 13:03:19 +03:00
const fastify = Fastify({
logger: true,
2023-09-21 15:28:55 +03:00
trustProxy: config.reverse_proxy,
2023-08-14 13:03:19 +03:00
});
2023-08-13 20:38:33 +03:00
fastify.register(fastifyStatic, {
2023-08-15 17:34:39 +03:00
root: path.join(process.cwd(), "static"),
prefix: "/static/",
});
fastify.register(fastifyView, {
engine: {
ejs: ejs,
},
2023-08-13 20:38:33 +03:00
});
await fastify.register(fastifySwagger, {
swagger: {
info: {
title: "TXTDot API",
description: publicConfig.description,
version: publicConfig.version,
},
}
});
2023-08-16 10:49:55 +03:00
await fastify.register(fastifySwaggerUi, { routePrefix: "/doc" });
2023-08-16 10:36:04 +03:00
fastify.register(indexRoute);
fastify.register(getRoute);
2023-09-21 15:26:32 +03:00
2023-09-21 15:28:55 +03:00
if (config.proxy_res)
2023-09-21 15:26:32 +03:00
fastify.register(proxyRoute);
2023-08-14 16:44:23 +03:00
fastify.register(parseRoute);
fastify.register(rawHtml);
2023-08-13 21:39:23 +03:00
2023-08-16 13:20:24 +03:00
fastify.setErrorHandler(errorHandler);
2023-08-18 11:38:05 +03:00
fastify.listen(
2023-09-21 15:28:55 +03:00
{ host: config.host, port: config.port },
2023-08-18 11:38:05 +03:00
(err) => {
err && console.log(err);
}
);
2023-08-13 20:38:33 +03:00
}
}
const app = new App();
app.init();