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
+2 -1
View File
@@ -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);
+2 -6
View File
@@ -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],
+2 -1
View File
@@ -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);
+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,
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: [],
-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 { 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();