ditch nestjs config module, add static config serfice

This commit is contained in:
Jurgis Sakalauskas
2022-12-09 11:37:44 +02:00
parent b353e00bec
commit 7e5fa1fb2a
11 changed files with 86 additions and 102 deletions
+4 -71
View File
@@ -11,7 +11,6 @@
"dependencies": { "dependencies": {
"@nestjs/axios": "^1.0.0", "@nestjs/axios": "^1.0.0",
"@nestjs/common": "^9.0.0", "@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.0.0", "@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"@nestjs/serve-static": "^3.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": { "node_modules/@nestjs/core": {
"version": "9.2.1", "version": "9.2.1",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.2.1.tgz", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.2.1.tgz",
@@ -3918,14 +3885,6 @@
"node": ">=12" "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": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -6489,7 +6448,8 @@
"node_modules/lodash": { "node_modules/lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "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": { "node_modules/lodash.memoize": {
"version": "4.1.2", "version": "4.1.2",
@@ -10813,29 +10773,6 @@
"uuid": "9.0.0" "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": { "@nestjs/core": {
"version": "9.2.1", "version": "9.2.1",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.2.1.tgz", "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", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==" "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": { "ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -14519,7 +14451,8 @@
"lodash": { "lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
}, },
"lodash.memoize": { "lodash.memoize": {
"version": "4.1.2", "version": "4.1.2",
-1
View File
@@ -23,7 +23,6 @@
"dependencies": { "dependencies": {
"@nestjs/axios": "^1.0.0", "@nestjs/axios": "^1.0.0",
"@nestjs/common": "^9.0.0", "@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.0.0", "@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"@nestjs/serve-static": "^3.0.0", "@nestjs/serve-static": "^3.0.0",
+2 -1
View File
@@ -3,6 +3,7 @@ import { NextFunction } from 'express';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { appendFile } from 'fs'; import { appendFile } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { configService } from '../config/config.service';
export class RequestLogMiddleware implements NestMiddleware { export class RequestLogMiddleware implements NestMiddleware {
async use(req: Request, res: Response, next: NextFunction) { async use(req: Request, res: Response, next: NextFunction) {
@@ -32,7 +33,7 @@ export class RequestLogMiddleware implements NestMiddleware {
private log(message: string) { private log(message: string) {
const strTime = new Date().toISOString(); 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) { appendFile(logFile, strTime + ' ' + message + '\n', function (err) {
if (err) { if (err) {
console.log('error logging ' + message); console.log('error logging ' + message);
+2 -6
View File
@@ -5,19 +5,15 @@ import {
RequestMethod, RequestMethod,
} from '@nestjs/common'; } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static'; import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { StaticController } from './static.controller'; import { StaticController } from './static.controller';
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware'; import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
import { StaticService } from './static.service'; import { StaticService } from './static.service';
import { ConfigModule } from '@nestjs/config'; import { configService } from '../config/config.service';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
ServeStaticModule.forRoot({ ServeStaticModule.forRoot({
rootPath: join(__dirname, process.env.STATIC_SERVE_FOLDER), rootPath: configService.getStaticFolder(),
}), }),
], ],
controllers: [StaticController], controllers: [StaticController],
+2 -1
View File
@@ -7,6 +7,7 @@ import { readdir, stat } from 'fs/promises';
import { Stats } from 'fs'; import { Stats } from 'fs';
import { join } from 'path'; import { join } from 'path';
import FileDto from './Dto/file.dto'; import FileDto from './Dto/file.dto';
import { configService } from '../config/config.service';
@Injectable() @Injectable()
export class StaticService { 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); const stats: Stats | false = await stat(path).catch(() => false);
+14
View File
@@ -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();
}
}
+15
View File
@@ -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 {}
+4 -8
View File
@@ -4,23 +4,19 @@ import {
NestModule, NestModule,
RequestMethod, RequestMethod,
} from '@nestjs/common'; } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { KodiApiModule } from './KodiApi/kodi-api.module'; import { KodiApiModule } from './KodiApi/kodi-api.module';
import { StaticModule } from './Static/static.module'; import { StaticModule } from './Static/static.module';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { LrtCategory } from './KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity';
import { RequestLogMiddleware } from './RequestLog/request-log.middleware'; import { RequestLogMiddleware } from './RequestLog/request-log.middleware';
import { TryModule } from './TryOutModule/try.module';
import { configService } from './config/config.service';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot({ isGlobal: true }), TryModule,
KodiApiModule, KodiApiModule,
StaticModule, StaticModule,
TypeOrmModule.forRoot({ TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
type: 'sqlite',
database: process.env.DB_PATH,
entities: [LrtCategory],
}),
], ],
controllers: [], controllers: [],
providers: [], providers: [],
-13
View File
@@ -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();
+40
View File
@@ -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();
+3 -1
View File
@@ -1,8 +1,10 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { configService } from './config/config.service';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ? parseInt(process.env.PORT) : 3000); await app.listen(configService.getPort());
} }
bootstrap(); bootstrap();