txtdot/src/app.ts

73 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-13 20:38:33 +03:00
import { ConfigService } from "./config/config.service";
2023-08-14 13:42:13 +03:00
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-08-26 11:46:36 +03:00
import getRoute from "./routes/browser/get";
import parseRoute from "./routes/api/parse";
import indexRoute from "./routes/browser/index";
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-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-21 14:30:06 +03:00
trustProxy: this.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-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(
{ host: this.config.host, port: this.config.port },
(err) => {
err && console.log(err);
}
);
2023-08-13 20:38:33 +03:00
}
}
const app = new App();
app.init();