forked from HackPlan/RootPanel
-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.coffee
executable file
·148 lines (113 loc) · 4.16 KB
/
app.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
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
#!/usr/bin/env coffee
global.app = exports
app.libs =
_: require 'underscore'
async: require 'async'
bodyParser: require 'body-parser'
child_process: require 'child_process'
cookieParser: require 'cookie-parser'
copy: require 'copy-to'
csrf: require 'csrf'
crypto: require 'crypto'
depd: require 'depd'
express: require 'express'
fs: require 'fs'
tmp: require 'tmp'
harp: require 'harp'
jade: require 'jade'
markdown: require('markdown').markdown
middlewareInjector: require 'middleware-injector'
moment: require 'moment-timezone'
mongoose: require 'mongoose'
morgan: require 'morgan'
nodemailer: require 'nodemailer'
os: require 'os'
path: require 'path'
redis: require 'redis'
redisStore: require 'connect-redis'
request: require 'request'
expressSession: require 'express-session'
mongooseUniqueValidator: require 'mongoose-unique-validator'
ObjectID: (require 'mongoose').Types.ObjectId
ObjectId: (require 'mongoose').Schema.Types.ObjectId
Mixed: (require 'mongoose').Schema.Types.Mixed
{cookieParser, copy, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
{morgan, nodemailer, path, redis, _} = exports.libs
app.logger = do ->
unless process.env.NODE_ENV == 'test'
return console
return {
log: ->
error: console.error
}
app.package = require './package'
app.deprecate = depd 'rootpanel'
do ->
config_path = path.join __dirname, 'config.coffee'
unless fs.existsSync config_path
app.deprecate 'config.coffee not found, copy sample config to ./config.coffee'
fs.writeFileSync config_path, fs.readFileSync path.join __dirname, "./sample/core.config.coffee"
fs.chmodSync config_path, 0o750
config = require './config'
do ->
if fs.existsSync config.web.listen
fs.unlinkSync config.web.listen
session_key_path = path.join __dirname, 'session.key'
unless fs.existsSync session_key_path
fs.writeFileSync session_key_path, crypto.randomBytes(48).toString('hex')
fs.chmodSync session_key_path, 0o750
app.redis = redis.createClient 6379, '127.0.0.1',
auth_pass: config.redis.password
app.mailer = nodemailer.createTransport config.email.account
app.express = express()
app.config = config
app.db = require './core/db'
app.utils = require './core/utils'
app.cache = require './core/cache'
app.i18n = require './core/i18n'
app.pluggable = require './core/pluggable'
app.models = {}
require './core/model/Account'
require './core/model/Financials'
require './core/model/CouponCode'
require './core/model/Notification'
require './core/model/SecurityLog'
require './core/model/Ticket'
app.templates = require './core/templates'
app.billing = require './core/billing'
app.middleware = require './core/middleware'
app.notification = require './core/notification'
unless process.env.NODE_ENV == 'test'
app.express.use morgan 'dev'
app.express.use bodyParser.json()
app.express.use cookieParser()
app.express.use middlewareInjector
app.express.use app.middleware.errorHandling
app.express.use app.middleware.session()
app.express.use app.middleware.csrf()
app.express.use app.middleware.authenticate
app.express.use app.middleware.accountHelpers
app.express.set 'views', path.join(__dirname, 'core/view')
app.express.set 'view engine', 'jade'
app.express.get '/locale/:language?', app.i18n.downloadLocales
app.express.use '/account', require './core/router/account'
app.express.use '/billing', require './core/router/billing'
app.express.use '/ticket', require './core/router/ticket'
app.express.use '/coupon', require './core/router/coupon'
app.express.use '/admin', require './core/router/admin'
app.express.use '/panel', require './core/router/panel'
app.pluggable.initializePlugins()
app.express.get '/', (req, res) ->
unless res.headerSent
res.redirect '/panel/'
app.express.use harp.mount './core/static'
exports.start = _.once ->
app.express.listen config.web.listen, ->
app.started = true
if fs.existsSync config.web.listen
fs.chmodSync config.web.listen, 0o770
app.pluggable.selectHook(null, 'app.started').forEach (hook) ->
hook.action()
app.logger.log "RootPanel start at #{config.web.listen}"
unless module.parent
exports.start()