-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
184 lines (136 loc) · 4.51 KB
/
app.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/env node
//
// mongo/express sample
// https://gist.github.com/1025038
// dynamic helpers
// http://stackoverflow.com/questions/6331776/accessing-express-js-req-or-session-from-jade-template
// http://stackoverflow.com/questions/11580796/migrating-express-js-2-to-3-specifically-app-dynamichelpers-to-app-locals-use
if(process.env.NODETIME_KEY){
var nodetime=require('nodetime').profile({
accountKey:process.env.NODETIME_KEY,
appName: process.env.NODETIME_APP
});
}
if (process.env.MEWRELIC_KEY){
var newrelic=require('newrelic')
}
if(process.env.VCAP_SERVICES){
//var vcap = JSON.parse(process.env.VCAP_SERVICES);
}
//
// load env
var express = require('express')
, fs = require('fs')
, passport = require('passport')
, env = process.env.NODE_ENV || 'development'
, config = require('./app/config')
, mongoose = require('mongoose')
, _ = require('underscore');
//
// open database
mongoose.connect(config.mongo.name,{server:{safe:true, auto_reconnect:true}},function(e){
//double check for database drop
console.log("boot[",new Date(),"] :",mongoose.connection.db.databaseName, config.mongo.name)
if(process.env.NODE_ENV!=='test'){
console.time("running db maintain")
require('./app/db.maintain').update(mongoose.connection.db,function(err,log){
if(err){
console.log("ERROR",err)
}
console.timeEnd("running db maintain")
});
}
if(config.dropdb && process.env.NODE_ENV==='test'){
mongoose.connection.db.dropDatabase(function(err,done){
});
}
});
//mongoose.set('debug', function (collectionName, method, query, doc) {
// Here query is what you are looking for.
// so whatever you want to do with the query
// would be done in here
// console.log('DEBUG ------------------',query)
//})
//
// configure karibou-wallet
require("karibou-wallet")({
allowMultipleSetOption:true,
apikey:config.payment.karibou.apikey,
allowMaxAmount:config.payment.allowMaxAmount,
debug:config.mail.develMode,
mongo:{
name:config.mongo.multiple,
multiple:(config.mongo.multiple)?true:false
}
});
//
// configure postfinance
var settings={};
settings.allowMultipleSetOption = false;
settings.sandbox = config.payment.postfinance.sandbox;
settings.enabled = config.payment.postfinance.enabled||false;
settings.debug = config.payment.postfinance.debug||false;
settings.pspid = config.payment.postfinance.pspid;
settings.apiUser=config.payment.postfinance.apiUser;
settings.apiPassword = config.payment.postfinance.apiPassword;
settings.shaSecret = config.payment.postfinance.shaSecret;
require("node-postfinance").configure(settings);
// load models
files = require("fs").readdirSync( './models' );
for(var i in files) {
if(/\.js$/.test(files[i])) require('./models/'+files[i]);
}
// extend config right after db is ready
mongoose.model('Config').getMain(function(err,c){
if(err){
console.log('Ooops error when reading stored config',err)
process.exit(1)
}
_.extend(config.shared,c)
})
var app = express()
// utils
require('./app/utils')(app);
// Events Bus
var bus=require('./app/bus');require('./app/bus.routes');
// mailer
require('./app/mail')(app,bus);
// payment api
var payment=require('./app/payment');
// express settings
require('./app/express')(app, config, passport)
// bootstrap passport config
require('./app/passport')(app, config, passport)
// Bootstrap routes
require('./app/routes')(app, config, passport)
console.log("generate random key:",require('crypto').randomBytes(32).toString('base64'))
//
// maintain db
//
//
//
// start the server
var host = (process.env.OPENSHIFT_INTERNAL_IP || process.env.OPENSHIFT_NODEJS_IP || config.express.host || 'localhost');
var count=0;
app.listen(app.get('port'),host).on('connection', function(socket) {
// console.log("---- new connection was made by a client: ",socket.remoteAddress,"c:", ++count);
socket.on('end',function(){
// console.log("---- close connection made by a client: ",socket.remoteAddress,"c:", --count);
})
})
//
// manage unmanaged exception
process.on('uncaughtException', function(err) {
if(process.env.NODE_ENV==='production'){
var msg=(err.stack)?err.stack:JSON.stringify(err,null,2);
bus.emit('sendmail',"evaleto@gmail.com",
"[karibou] uncaughtException "+err.toString(), {content:msg}, "simple",
function (err,status) {
console.log(err,status)
process.exit(1)
});
}
console.log("uncaughtException",err.stack);
});
// expose app
exports = module.exports = app