diff --git a/package-lock.json b/package-lock.json index d5a9259..66e9911 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "dependencies": { "@nestjs/axios": "^1.0.0", "@nestjs/common": "^9.0.0", - "@nestjs/config": "^2.2.0", "@nestjs/core": "^9.0.0", "@nestjs/platform-express": "^9.0.0", "@nestjs/serve-static": "^3.0.0", @@ -1596,38 +1595,6 @@ } } }, - "node_modules/@nestjs/config": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.2.0.tgz", - "integrity": "sha512-78Eg6oMbCy3D/YvqeiGBTOWei1Jwi3f2pSIZcZ1QxY67kYsJzTRTkwRT8Iv30DbK0sGKc1mcloDLD5UXgZAZtg==", - "dependencies": { - "dotenv": "16.0.1", - "dotenv-expand": "8.0.3", - "lodash": "4.17.21", - "uuid": "8.3.2" - }, - "peerDependencies": { - "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0", - "reflect-metadata": "^0.1.13", - "rxjs": "^6.0.0 || ^7.2.0" - } - }, - "node_modules/@nestjs/config/node_modules/dotenv": { - "version": "16.0.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", - "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==", - "engines": { - "node": ">=12" - } - }, - "node_modules/@nestjs/config/node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/@nestjs/core": { "version": "9.2.1", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.2.1.tgz", @@ -3918,14 +3885,6 @@ "node": ">=12" } }, - "node_modules/dotenv-expand": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-8.0.3.tgz", - "integrity": "sha512-SErOMvge0ZUyWd5B0NXMQlDkN+8r+HhVUsxgOO7IoPDOdDRD2JjExpN6y3KnFR66jsJMwSn1pqIivhU5rcJiNg==", - "engines": { - "node": ">=12" - } - }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -6489,7 +6448,8 @@ "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true }, "node_modules/lodash.memoize": { "version": "4.1.2", @@ -10813,29 +10773,6 @@ "uuid": "9.0.0" } }, - "@nestjs/config": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.2.0.tgz", - "integrity": "sha512-78Eg6oMbCy3D/YvqeiGBTOWei1Jwi3f2pSIZcZ1QxY67kYsJzTRTkwRT8Iv30DbK0sGKc1mcloDLD5UXgZAZtg==", - "requires": { - "dotenv": "16.0.1", - "dotenv-expand": "8.0.3", - "lodash": "4.17.21", - "uuid": "8.3.2" - }, - "dependencies": { - "dotenv": { - "version": "16.0.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", - "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==" - }, - "uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" - } - } - }, "@nestjs/core": { "version": "9.2.1", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.2.1.tgz", @@ -12584,11 +12521,6 @@ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==" }, - "dotenv-expand": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-8.0.3.tgz", - "integrity": "sha512-SErOMvge0ZUyWd5B0NXMQlDkN+8r+HhVUsxgOO7IoPDOdDRD2JjExpN6y3KnFR66jsJMwSn1pqIivhU5rcJiNg==" - }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -14519,7 +14451,8 @@ "lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true }, "lodash.memoize": { "version": "4.1.2", diff --git a/package.json b/package.json index ca6123f..4ea9077 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "dependencies": { "@nestjs/axios": "^1.0.0", "@nestjs/common": "^9.0.0", - "@nestjs/config": "^2.2.0", "@nestjs/core": "^9.0.0", "@nestjs/platform-express": "^9.0.0", "@nestjs/serve-static": "^3.0.0", diff --git a/src/RequestLog/request-log.middleware.ts b/src/RequestLog/request-log.middleware.ts index 67c666a..b1f2f50 100644 --- a/src/RequestLog/request-log.middleware.ts +++ b/src/RequestLog/request-log.middleware.ts @@ -3,6 +3,7 @@ import { NextFunction } from 'express'; import { Request, Response } from 'express'; import { appendFile } from 'fs'; import { join } from 'path'; +import { configService } from '../config/config.service'; export class RequestLogMiddleware implements NestMiddleware { async use(req: Request, res: Response, next: NextFunction) { @@ -32,7 +33,7 @@ export class RequestLogMiddleware implements NestMiddleware { private log(message: string) { const strTime = new Date().toISOString(); - const logFile = join(__dirname, process.env.LOG_FILE_REQUESTS); + const logFile = configService.getStaticRequestsLogPath(); appendFile(logFile, strTime + ' ' + message + '\n', function (err) { if (err) { console.log('error logging ' + message); diff --git a/src/Static/static.module.ts b/src/Static/static.module.ts index 5363210..2e5c868 100644 --- a/src/Static/static.module.ts +++ b/src/Static/static.module.ts @@ -5,19 +5,15 @@ import { RequestMethod, } from '@nestjs/common'; import { ServeStaticModule } from '@nestjs/serve-static'; -import { join } from 'path'; import { StaticController } from './static.controller'; import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware'; import { StaticService } from './static.service'; -import { ConfigModule } from '@nestjs/config'; +import { configService } from '../config/config.service'; @Module({ imports: [ - ConfigModule.forRoot({ - isGlobal: true, - }), ServeStaticModule.forRoot({ - rootPath: join(__dirname, process.env.STATIC_SERVE_FOLDER), + rootPath: configService.getStaticFolder(), }), ], controllers: [StaticController], diff --git a/src/Static/static.service.ts b/src/Static/static.service.ts index 1da4c7f..2562be6 100644 --- a/src/Static/static.service.ts +++ b/src/Static/static.service.ts @@ -7,6 +7,7 @@ import { readdir, stat } from 'fs/promises'; import { Stats } from 'fs'; import { join } from 'path'; import FileDto from './Dto/file.dto'; +import { configService } from '../config/config.service'; @Injectable() export class StaticService { @@ -17,7 +18,7 @@ export class StaticService { ); } - const path = join(__dirname, process.env.STATIC_SERVE_FOLDER, relPath); + const path = join(configService.getStaticFolder(), relPath); const stats: Stats | false = await stat(path).catch(() => false); diff --git a/src/TryOutModule/try.cotroller.ts b/src/TryOutModule/try.cotroller.ts new file mode 100644 index 0000000..53815bd --- /dev/null +++ b/src/TryOutModule/try.cotroller.ts @@ -0,0 +1,14 @@ +import { Controller, Get } from '@nestjs/common'; +import { EntityManager } from 'typeorm'; +import { configService } from '../config/config.service'; + +@Controller('try') +export class TryController { + constructor(private orm: EntityManager) {} + @Get('') + async main() { + //return await this.orm.query('select * from aliases'); + //return { hello: 'world' }; + return configService.getStaticFolder(); + } +} diff --git a/src/TryOutModule/try.module.ts b/src/TryOutModule/try.module.ts new file mode 100644 index 0000000..ed4946a --- /dev/null +++ b/src/TryOutModule/try.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; + +import { TypeOrmModule } from '@nestjs/typeorm'; +import { TryController } from './try.cotroller'; +import { configService } from '../config/config.service'; + +/** + * Playground module for playing around + */ +@Module({ + imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig())], + controllers: [TryController], + providers: [], +}) +export class TryModule {} diff --git a/src/app.module.ts b/src/app.module.ts index 883b6e5..8625f0e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -4,23 +4,19 @@ import { NestModule, RequestMethod, } from '@nestjs/common'; -import { ConfigModule } from '@nestjs/config'; import { KodiApiModule } from './KodiApi/kodi-api.module'; import { StaticModule } from './Static/static.module'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { LrtCategory } from './KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity'; import { RequestLogMiddleware } from './RequestLog/request-log.middleware'; +import { TryModule } from './TryOutModule/try.module'; +import { configService } from './config/config.service'; @Module({ imports: [ - ConfigModule.forRoot({ isGlobal: true }), + TryModule, KodiApiModule, StaticModule, - TypeOrmModule.forRoot({ - type: 'sqlite', - database: process.env.DB_PATH, - entities: [LrtCategory], - }), + TypeOrmModule.forRoot(configService.getTypeOrmConfig()), ], controllers: [], providers: [], diff --git a/src/config/config.provider.ts b/src/config/config.provider.ts deleted file mode 100644 index 332419a..0000000 --- a/src/config/config.provider.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as Dotenv from 'dotenv'; -const dotenv: any = Dotenv.config().parsed; - -class Config { - public readonly port = dotenv.PORT; - getPort() { - return process.env.PORT; - } - //todo - check dotenv again if not loaded before emmiting err. - //works globally, loaded once -} - -export default new Config(); diff --git a/src/config/config.service.ts b/src/config/config.service.ts new file mode 100644 index 0000000..a5131bb --- /dev/null +++ b/src/config/config.service.ts @@ -0,0 +1,40 @@ +import * as env from 'dotenv'; +import { TypeOrmModuleOptions } from '@nestjs/typeorm'; +import { LrtCategory } from '../KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity'; +import { join } from 'path'; + +env.config(); + +class ConfigService { + public getEnv(key: string): any { + return process.env[key]; + } + + public getPort(): number { + const port = this.getEnv('PORT'); + + return port ? parseInt(port) : 3000; + } + + public getTypeOrmConfig(): TypeOrmModuleOptions { + return { + type: 'sqlite', + database: this.getEnv('DB_PATH'), + entities: [LrtCategory], + }; + } + + public getStaticRequestsLogPath(): string { + return join(this.getRootPath(), this.getEnv('LOG_FILE_REQUESTS')); + } + + public getStaticFolder(): string { + return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER')); + } + + private getRootPath(): string { + return join(__dirname, '../..'); + } +} + +export const configService = new ConfigService(); diff --git a/src/main.ts b/src/main.ts index d89f8bf..0e7a8c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,10 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { configService } from './config/config.service'; async function bootstrap() { const app = await NestFactory.create(AppModule); - await app.listen(process.env.PORT ? parseInt(process.env.PORT) : 3000); + await app.listen(configService.getPort()); } + bootstrap();