2023-08-13 20:38:33 +03:00
|
|
|
import { ConfigService } from "./config/config.service";
|
2023-08-14 13:42:13 +03:00
|
|
|
|
2023-08-15 15:57:50 +03:00
|
|
|
import path from "path";
|
|
|
|
|
2023-08-14 13:05:34 +03:00
|
|
|
import Fastify from "fastify";
|
2023-08-15 15:57:50 +03:00
|
|
|
import fastifyStatic from "@fastify/static";
|
2023-08-14 15:39:40 +03:00
|
|
|
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";
|
2023-08-14 15:39:40 +03:00
|
|
|
import ejs from "ejs";
|
2023-08-14 13:59:32 +03:00
|
|
|
|
2023-08-15 15:03:28 +03:00
|
|
|
import getRoute from "./routes/get";
|
2023-08-14 18:22:03 +03:00
|
|
|
import parseRoute from "./routes/parse";
|
2023-08-15 15:03:28 +03:00
|
|
|
import indexRoute from "./routes/index";
|
2023-08-16 09:43:43 +03:00
|
|
|
import rawHtml from "./routes/raw-html";
|
2023-08-14 13:59:32 +03:00
|
|
|
|
2023-08-16 12:15:51 +03:00
|
|
|
import publicConfig from "./publicConfig";
|
2023-08-16 13:20:24 +03:00
|
|
|
import errorHandler from "./errors/handler";
|
2023-08-16 12:15:51 +03:00
|
|
|
|
2023-08-13 20:38:33 +03:00
|
|
|
class App {
|
2023-08-16 17:10:05 +03:00
|
|
|
config: ConfigService;
|
2023-08-14 13:42:13 +03:00
|
|
|
|
2023-08-13 20:38:33 +03:00
|
|
|
constructor() {
|
|
|
|
this.config = new ConfigService();
|
|
|
|
}
|
2023-08-14 13:42:13 +03:00
|
|
|
|
2023-08-14 13:03:19 +03:00
|
|
|
async init() {
|
|
|
|
const fastify = Fastify({
|
|
|
|
logger: true,
|
|
|
|
});
|
2023-08-13 20:38:33 +03:00
|
|
|
|
2023-08-15 15:57:50 +03:00
|
|
|
fastify.register(fastifyStatic, {
|
2023-08-15 17:34:39 +03:00
|
|
|
root: path.join(process.cwd(), "static"),
|
|
|
|
prefix: "/static/",
|
2023-08-15 15:57:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
fastify.register(fastifyView, {
|
2023-08-14 15:39:40 +03:00
|
|
|
engine: {
|
|
|
|
ejs: ejs,
|
2023-08-14 16:47:20 +03:00
|
|
|
},
|
2023-08-13 20:38:33 +03:00
|
|
|
});
|
|
|
|
|
2023-08-16 12:15:51 +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
|
|
|
|
2023-08-15 15:03:28 +03:00
|
|
|
fastify.register(indexRoute);
|
|
|
|
fastify.register(getRoute);
|
2023-08-14 16:44:23 +03:00
|
|
|
fastify.register(parseRoute);
|
2023-08-16 09:43:43 +03:00
|
|
|
fastify.register(rawHtml);
|
2023-08-13 21:39:23 +03:00
|
|
|
|
2023-08-16 13:20:24 +03:00
|
|
|
fastify.setErrorHandler(errorHandler);
|
|
|
|
|
2023-08-16 17:10:05 +03:00
|
|
|
fastify.listen({ host: this.config.host, port: this.config.port }, (err) => {
|
2023-08-14 16:44:23 +03:00
|
|
|
err && console.log(err);
|
2023-08-13 20:38:33 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = new App();
|
|
|
|
app.init();
|