From f56b79c5633681cff1766ecfefbd140a834630d3 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 21 Sep 2023 16:28:55 +0400 Subject: [PATCH] getConfig() --- src/app.ts | 15 ++++++--------- src/config/main.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 src/config/main.ts diff --git a/src/app.ts b/src/app.ts index 6843d3c..fff4239 100644 --- a/src/app.ts +++ b/src/app.ts @@ -17,18 +17,15 @@ import rawHtml from "./routes/api/raw-html"; import publicConfig from "./publicConfig"; import errorHandler from "./errors/handler"; +import getConfig from "./config/main"; class App { - config: ConfigService; - - constructor() { - this.config = new ConfigService(); - } - async init() { + const config = getConfig(); + const fastify = Fastify({ logger: true, - trustProxy: this.config.reverse_proxy, + trustProxy: config.reverse_proxy, }); fastify.register(fastifyStatic, { @@ -56,7 +53,7 @@ class App { fastify.register(indexRoute); fastify.register(getRoute); - if (this.config.proxy_res) + if (config.proxy_res) fastify.register(proxyRoute); fastify.register(parseRoute); @@ -65,7 +62,7 @@ class App { fastify.setErrorHandler(errorHandler); fastify.listen( - { host: this.config.host, port: this.config.port }, + { host: config.host, port: config.port }, (err) => { err && console.log(err); } diff --git a/src/config/main.ts b/src/config/main.ts new file mode 100644 index 0000000..37dcad3 --- /dev/null +++ b/src/config/main.ts @@ -0,0 +1,12 @@ +import { ConfigService } from "./config.service"; + +let configSvc: ConfigService | undefined; + +export default function getConfig(): ConfigService { + if (configSvc) { + return configSvc; + } + + configSvc = new ConfigService(); + return configSvc; +}