mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
ditch nestjs config module, add static config serfice
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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: [],
|
||||
|
||||
@@ -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();
|
||||
@@ -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
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user