-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.ts
62 lines (39 loc) · 1.39 KB
/
util.ts
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
/// <reference path="config.d.ts" />
/// <reference path="typings/index.d.ts" />
import * as mongoose from 'mongoose';
import * as fs from 'fs';
import * as redis from 'redis';
//import {config} from "./config";
import * as commander from 'commander';
export let program = commander.version("1.0")
.option('-e, --env [env]','environnement')
.parse(process.argv);
export let auth = function(env?:string){
let config = getConfiguration(env);
let databaseURL = config.database.url;
let mongoOptions : mongoose.ConnectionOptions = {};
if(config.database!= null && config.database.user != null && config.database.pass != null){
mongoOptions.user = config.database.user;
mongoOptions.pass = config.database.pass;
if(config.database.dbAuth != null)
mongoOptions.db = { authSource: config.database.dbAuth};
}
// mongoose.set('debug', true);
mongoose.connect(databaseURL, mongoOptions, (err) =>{
// console.log(err, mongoose.connection.readyState);
});
}
export let getConfiguration = function(env?: string){
if(env == null){
env = (<any>program).env;
}
let file = "./config";
if(env != null && env != "prod"){
file = `./config_${env}`;
}
return require(file).config;
}
if(global['redisClient'] == null){
global['redisClient'] = redis.createClient();
}
export let RedisClient : redis.RedisClient = global['redisClient'];