All files / utils/services/config config.service.ts

30.43% Statements 7/23
11.76% Branches 2/17
0% Functions 0/5
23.81% Lines 5/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 668x         8x 8x 8x     8x                                                                                                              
import { readFileSync } from 'fs';
import { AmqpConfigInterface, AppConfigInterface } from './config.interface';
import { GraphQLObjectType } from 'graphql';
import { ControllerContainerService } from '../../services/controller-service/controller.service';
import { SchemaService } from '../../services/schema/schema.service';
import { Service } from '../../../utils/container/index';
import { Container } from '../../container/Container';
import { ConnectionHookService } from '../../services/connection-hook/connection-hook.service';
 
@Service()
export class ConfigService {
    cert: Buffer;
    AMQP_CONFIG: AmqpConfigInterface = {
        host: process.env.AMQP_HOST || '182.10.0.5',
        port: process.env.AMQP_PORT || 5672
    };
    APP_CONFIG: AppConfigInterface = {
        graphiql: process.env.GRAPHIQL === 'true' ? true : false,
        cert: this.cert,
        schema: null,
        uploadFolder: '',
        // tslint:disable-next-line:max-line-length
        port: process.env.API_PORT || process.env.PORT || 9000,
        cyper: <any>{
            iv: 'Jkyt1H3FA8JK9L3A',
            privateKey: '8zTVzr3p53VC12jHV54rIYu2545x47lY',
            algorithm: 'aes256',
            key: ''
        },
        connectionHooks: this.connectionHookService,
        ethereumApi: process.env.ETHEREUM_API || 'http://localhost:7545' || 'http://pub-node1.etherscan.io:8545'
    };
    constructor(
        private connectionHookService: ConnectionHookService
    ) {
        try {
            this.cert = readFileSync(process.env.API_CERT || './cert.key');
            this.APP_CONFIG.cert = this.cert;
        } catch (e) {
            console.log('This server will be runned without authentication!');
            this.cert = null;
            this.APP_CONFIG.cert = null;
        }
    }
 
    getApp() {
        return this.APP_CONFIG;
    }
 
    getAmqp() {
        return this.AMQP_CONFIG;
    }
 
    setAppConfig(config: AppConfigInterface) {
        this.APP_CONFIG = config;
    }
 
    static forRoot(config: { APP_CONFIG?: AppConfigInterface, AMQP_CONFIG?: AmqpConfigInterface }) {
        const configService = Container.get(ConfigService);
        configService.APP_CONFIG = config.APP_CONFIG;
        configService.AMQP_CONFIG = config.AMQP_CONFIG;
        return ConfigService;
    }
}