diff --git a/.env.example b/.env.example index 07193eb..b413059 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -PORT=80 \ No newline at end of file +HOST=127.0.0.1 # set 0.0.0.0 if you don't use reverse proxy +PORT=8080 diff --git a/src/app.ts b/src/app.ts index 6f67873..f92fc18 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,3 @@ -import { IConfigService } from "./config/config.interface"; import { ConfigService } from "./config/config.service"; import path from "path"; @@ -19,7 +18,7 @@ import publicConfig from "./publicConfig"; import errorHandler from "./errors/handler"; class App { - config: IConfigService; + config: ConfigService; constructor() { this.config = new ConfigService(); @@ -59,7 +58,7 @@ class App { fastify.setErrorHandler(errorHandler); - fastify.listen({ port: Number(this.config.get("PORT")) }, (err) => { + fastify.listen({ host: this.config.host, port: this.config.port }, (err) => { err && console.log(err); }); } diff --git a/src/config/config.interface.ts b/src/config/config.interface.ts deleted file mode 100644 index 21c1d71..0000000 --- a/src/config/config.interface.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface IConfigService { - get(key: string): string; -} diff --git a/src/config/config.service.ts b/src/config/config.service.ts index cb775c9..f88c60c 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -1,30 +1,17 @@ -import { config, DotenvParseOutput } from "dotenv"; -import { IConfigService } from "./config.interface"; +import { config } from "dotenv"; -export class ConfigService implements IConfigService { - private config: DotenvParseOutput; +export class ConfigService { + public readonly host: string; + public readonly port: number; constructor() { - const { error, parsed } = config(); - - if (error) { - throw new Error(".env file not found"); - } + const parsed = config().parsed; if (!parsed) { throw new Error("Invalid .env file"); } - this.config = parsed; - } - - get(key: string): string { - const res = this.config[key]; - - if (!res) { - throw new Error(`Key ${key} not found`); - } - - return res; + this.host = process.env.HOST || 'localhost'; + this.port = Number(process.env.PORT) || 8080; } }