forked from yi/node-ticket-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
80 lines (61 loc) · 2.06 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
68
69
70
71
72
73
74
75
76
77
78
79
###
# nodejs-express-mongoose-demo
# Copyright(c) 2013 Madhusudhan Srinivasa <madhums8@gmail.com>
# MIT Licensed
###
## Module dependencies.
express = require('express')
compression = require 'compression'
fs = require('fs')
p = require "commander"
path = require "path"
_ = require "underscore"
debuglog = require("debug")("ticketman:server")
pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json")))
# config cli
p.version(pkg.version)
.option('-c, --config [VALUE]', 'path to config file')
.option('-p, --port [VALUE]', 'port to run this web service')
.option('-e, --environment [VALUE]', 'application environment mode')
.parse(process.argv)
# Main application entry file.
# Please note that the order of loading is important.
# Load configurations
# if test env, load example file
env = p.environment || process.env.NODE_ENV || 'development'
configs = require('./config/config')
config = configs[env]
config.version = pkg.version
# fix root path error after distillation
config.root = path.resolve __dirname, "../"
debuglog "[server] config.root:#{config.root}"
# load and mixin external configurations
if p.config
try
pathToExternalConfig = path.resolve(config.root, p.config)
debuglog "pathToExternalConfig:#{pathToExternalConfig}"
externalConfig = JSON.parse(fs.readFileSync(pathToExternalConfig))
debuglog "externalConfig:%j", externalConfig
_.extend config, externalConfig
catch err
debuglog "ERROR [server] fail to mixin externalConfig. #{err}"
mongoose = require('mongoose')
# Bootstrap db connection
mongoose.connect(config.db)
mongoose.set('debug', true) if env is 'development'
# Bootstrap models
require "./models/ticket"
require "./models/worker"
require "./utils/clear_db"
app = express()
app.use(compression())
# 启动 express web 框架
require('./config/express')(app, config)
# 启动路由器
require('./config/routes')(app)
# Start the app by listening on <port>
port = p.port || process.env.PORT || 3456
app.listen(port)
debuglog "Ticketman app started on port #{port}"
# expose app
exports = module.exports = app