-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.coffee
68 lines (55 loc) · 2.04 KB
/
server.coffee
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
# monitoring with nodetime
if process.env.NODE_ENV == 'production'
require('nodetime').profile(
accountKey: "ENTER-A-VALID-KEY-HERE"
appName: 'mean.coffee'
)
# dependencies
config = require './server/config/config'
logger = require './server/config/logger'
express = require 'express'
mongoose = require 'mongoose'
passport = require 'passport'
glob = require 'glob'
path = require 'path'
root_path = __dirname
# catch all uncaught exceptions
process.on 'uncaughtException', (err) ->
logger.error 'Something very bad happened: ', err.message
logger.error err.stack
process.exit 1 # because now, you are in unpredictable state!
# watch and log any leak (a lot of false positive though)
memwatch = require 'memwatch'
memwatch.on 'leak', (d) -> logger.error "LEAK: #{JSON.stringify(d)}"
# bootstap db connection
db = mongoose.connect config.DBURL
logger.info "mongo connected to", config.DBURL
# exit on db connection error
mongoose.connection.on 'error', (err) ->
logger.error "mongodb error: #{err}"
process.exit 1
# retry 10 times on db connection lost
attempt = 1
mongoose.connection.on 'disconnected', () ->
if attempt < 10
logger.error "mongodb disconnected, trying to reconnect.."
logger.info "mongodb reconnect, attempt num #{attempt}"
attempt += 1
db = mongoose.connect config.DBURL, opts
else
logger.error "mongodb disconnect, giving up!"
# bootstrap passport config
require('./server/config/passport')(passport)
# express configuration
app = require("./server/config/express")(passport, db, logger, root_path)
require('./server/config/globals')(app)
# bootstrap routes
require('./server/routes')(app)
# bootstrap routes based on *.route.coffee naming
# files = glob.sync 'server/**/*.route.{js,coffee}'
# for file in files when file.lastIndexOf("server/specs/", 0) isnt 0
# logger.info "Found a route file, #{file} - requiring it"
# require(path.join __dirname, file)(app)
# start the app
app.listen app.get('port'), ->
logger.info "app server listening on port #{@address().port} in #{config.ENV} mode"