expand config

This commit is contained in:
jurgis
2022-12-11 13:17:10 +02:00
parent 80ba785798
commit 0b438fb134
10 changed files with 72 additions and 36 deletions
@@ -6,7 +6,7 @@ import { SearchResponseDto } from '../Dto/search-response.dto';
import { SearchResponseItemDto } from '../Dto/search-response-item.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { LrtCategory } from '../Entity/lrt-category.entity';
import { FindOptionsUtils, Repository } from 'typeorm';
import { Repository } from 'typeorm';
@Injectable()
export class LrtApiSearchClient {
+1 -1
View File
@@ -9,7 +9,7 @@ export class LrtService {
constructor(
private readonly kodiApiResponseFactory: KodiApiResponseFactory,
private readonly lrtApiClient: LrtApiClient,
private readonly recentSearchesService: RecentSearchesService
private readonly recentSearchesService: RecentSearchesService,
) {}
getMainMenu(): ApiResponse {
@@ -8,9 +8,7 @@ import { readFile, writeFile } from 'fs/promises';
@Injectable()
export class RecentSearchesService {
private readonly configService: ConfigService;
constructor(
private readonly kodiApiResponseFactory: KodiApiResponseFactory,
) {
constructor(private readonly kodiApiResponseFactory: KodiApiResponseFactory) {
this.configService = configService;
}
async getRecentSearches(module: string, path: string): Promise<ApiResponse> {
@@ -48,6 +46,9 @@ export class RecentSearchesService {
}
private getFilePath(module: string): string {
return join(this.configService.getRecentSearchesFolder(), module + '.json');
return join(
this.configService.getPaths().getRecentSearchesFolder(),
module + '.json',
);
}
}
+1 -2
View File
@@ -2,7 +2,6 @@ import { NestMiddleware } from '@nestjs/common';
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 {
@@ -33,7 +32,7 @@ export class RequestLogMiddleware implements NestMiddleware {
private log(message: string) {
const strTime = new Date().toISOString();
const logFile = configService.getStaticRequestsLogPath();
const logFile = configService.getPaths().getStaticRequestsLogPath();
appendFile(logFile, strTime + ' ' + message + '\n', function (err) {
if (err) {
console.log('error logging ' + message);
+1 -1
View File
@@ -13,7 +13,7 @@ import { configService } from '../config/config.service';
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: configService.getStaticFolder(),
rootPath: configService.getPaths().getStaticFolder(),
}),
],
controllers: [StaticController],
+1 -1
View File
@@ -18,7 +18,7 @@ export class StaticService {
);
}
const path = join(configService.getStaticFolder(), relPath);
const path = join(configService.getPaths().getStaticFolder(), relPath);
const stats: Stats | false = await stat(path).catch(() => false);
+8 -23
View File
@@ -1,13 +1,14 @@
import * as env from 'dotenv';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { LrtCategory } from '../KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity';
import { join } from 'path';
import { TitleEntity } from '../KodiApi/AllFiles/Entity/title.entity';
import { FileEntity } from '../KodiApi/AllFiles/Entity/file.entity';
import { TypeOrmConfig } from './type-orm.config';
import { PathsConfig } from './paths.config';
env.config();
export class ConfigService {
private typeOrmConfig: TypeOrmConfig = new TypeOrmConfig(this);
private pathsConfig: PathsConfig = new PathsConfig(this);
public getEnv(key: string): any {
return process.env[key];
}
@@ -19,27 +20,11 @@ export class ConfigService {
}
public getTypeOrmConfig(): TypeOrmModuleOptions {
return {
type: 'sqlite',
database: this.getEnv('DB_PATH'),
entities: [LrtCategory, TitleEntity, FileEntity],
};
return this.typeOrmConfig;
}
public getStaticRequestsLogPath(): string {
return join(this.getRootPath(), this.getEnv('LOG_FILE_REQUESTS'));
}
public getStaticFolder(): string {
return join(this.getRootPath(), this.getEnv('STATIC_SERVE_FOLDER'));
}
public getRecentSearchesFolder(): string {
return join(this.getRootPath(), this.getEnv('RECENT_SEARCHES_FOLDER'));
}
private getRootPath(): string {
return join(__dirname, '../..');
public getPaths(): PathsConfig {
return this.pathsConfig;
}
}
+33
View File
@@ -0,0 +1,33 @@
import { ConfigService } from './config.service';
import { join } from 'path';
export class PathsConfig {
constructor(private readonly configService: ConfigService) {
this.configService = configService;
}
public getStaticRequestsLogPath(): string {
return join(
this.getRootPath(),
this.configService.getEnv('LOG_FILE_REQUESTS'),
);
}
public getStaticFolder(): string {
return join(
this.getRootPath(),
this.configService.getEnv('STATIC_SERVE_FOLDER'),
);
}
public getRecentSearchesFolder(): string {
return join(
this.getRootPath(),
this.configService.getEnv('RECENT_SEARCHES_FOLDER'),
);
}
private getRootPath(): string {
return join(__dirname, '../..');
}
}
+16
View File
@@ -0,0 +1,16 @@
import { LrtCategory } from '../KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity';
import { TitleEntity } from '../KodiApi/AllFiles/Entity/title.entity';
import { FileEntity } from '../KodiApi/AllFiles/Entity/file.entity';
import { ConfigService } from './config.service';
import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type';
export class TypeOrmConfig {
constructor(configService: ConfigService) {
this.type = 'sqlite';
this.database = configService.getEnv('DB_PATH');
this.entities = [LrtCategory, TitleEntity, FileEntity];
}
type: 'sqlite';
database: string;
entities: Array<EntityClassOrSchema>;
}