mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
expand config
This commit is contained in:
+5
-3
@@ -1,3 +1,5 @@
|
||||
PORT=80
|
||||
STATIC_SERVE_FOLDER="../path/to/static/files"
|
||||
LOG_FILE_REQUESTS="../path/to/log/all/requests.log"
|
||||
PORT=3000
|
||||
DB_PATH=./data/db/db.db
|
||||
LOG_FILE_REQUESTS=./data/static/static.log
|
||||
STATIC_SERVE_FOLDER=./data/static
|
||||
RECENT_SEARCHES_FOLDER=./data/recent
|
||||
@@ -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 {
|
||||
|
||||
@@ -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',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -13,7 +13,7 @@ import { configService } from '../config/config.service';
|
||||
@Module({
|
||||
imports: [
|
||||
ServeStaticModule.forRoot({
|
||||
rootPath: configService.getStaticFolder(),
|
||||
rootPath: configService.getPaths().getStaticFolder(),
|
||||
}),
|
||||
],
|
||||
controllers: [StaticController],
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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, '../..');
|
||||
}
|
||||
}
|
||||
@@ -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>;
|
||||
}
|
||||
Reference in New Issue
Block a user