-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathapp.service.ts
More file actions
85 lines (74 loc) · 2.62 KB
/
Copy pathapp.service.ts
File metadata and controls
85 lines (74 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { HttpException, Injectable, Logger } from '@nestjs/common';
import { spawn } from 'child_process';
import { ConfigService } from '@nestjs/config';
import { UsersService } from './users/users.service';
import { AppModuleConfigProperties } from './app.module.config.properties';
import { OrmModuleConfigProperties } from './orm/orm.module.config.properties';
import { AppConfig } from './app.config.api';
import { UserDto } from './users/api/UserDto';
@Injectable()
export class AppService {
private readonly logger = new Logger(AppService.name);
constructor(
private readonly configService: ConfigService,
private readonly userService: UsersService
) {}
async launchCommand(command: string): Promise<string> {
this.logger.debug(`launch ${command} command`);
return new Promise((res, rej) => {
try {
const [exec, ...args] = command.split(' ');
const ps = spawn(exec, args);
ps.stdout.on('data', (data: Buffer) => {
this.logger.debug(`stdout: ${data}`);
res(data.toString('ascii'));
});
ps.stderr.on('data', (data: Buffer) => {
this.logger.debug(`stderr: ${data}`);
res(data.toString('ascii'));
});
ps.on('error', (err) => rej(err.message));
ps.on('close', (code) =>
this.logger.debug(`child process exited with code ${code}`)
);
} catch (err) {
rej(err.message);
}
});
}
getConfig(): AppConfig {
this.logger.debug('Called getConfig');
const dbSchema = this.configService.get<string>(
OrmModuleConfigProperties.ENV_DATABASE_SCHEMA
),
dbHost = this.configService.get<string>(
OrmModuleConfigProperties.ENV_DATABASE_HOST
),
dbPort = this.configService.get<string>(
OrmModuleConfigProperties.ENV_DATABASE_PORT
),
dbUser = this.configService.get<string>(
OrmModuleConfigProperties.ENV_DATABASE_USER
),
dbPwd = this.configService.get<string>(
OrmModuleConfigProperties.ENV_DATABASE_PASSWORD
);
return {
awsBucket: this.configService.get<string>(
AppModuleConfigProperties.ENV_AWS_BUCKET
),
sql: `postgres://${dbUser}:${dbPwd}@${dbHost}:${dbPort}/${dbSchema} `,
googlemaps: this.configService.get<string>(
AppModuleConfigProperties.ENV_GOOGLE_MAPS
)
};
}
async getUserInfo(email: string): Promise<UserDto> {
try {
this.logger.debug(`Find a user by email: ${email}`);
return new UserDto(await this.userService.findByEmail(email));
} catch (err) {
throw new HttpException(err.message, err.status);
}
}
}