mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-03-26 07:44:10 +08:00
refactor app.coffee
This commit is contained in:
124
app.coffee
124
app.coffee
@@ -6,48 +6,49 @@ global.app = module.exports = new EventEmitter()
|
||||
|
||||
app.libs =
|
||||
_: require 'underscore'
|
||||
async: require 'async'
|
||||
bunyan: require 'bunyan'
|
||||
bodyParser: require 'body-parser'
|
||||
child_process: require 'child_process'
|
||||
cookieParser: require 'cookie-parser'
|
||||
expressBunyanLogger: require 'express-bunyan-logger'
|
||||
csrf: require 'csrf'
|
||||
crypto: require 'crypto'
|
||||
express: require 'express'
|
||||
fs: require 'fs'
|
||||
harp: require 'harp'
|
||||
jade: require 'jade'
|
||||
markdown: require('markdown').markdown
|
||||
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'
|
||||
jade: require 'jade'
|
||||
async: require 'async'
|
||||
crypto: require 'crypto'
|
||||
moment: require 'moment-timezone'
|
||||
request: require 'request'
|
||||
expressSession: require 'express-session'
|
||||
express: require 'express'
|
||||
child_process: require 'child_process'
|
||||
|
||||
csrf: require 'csrf'
|
||||
mongoose: require 'mongoose'
|
||||
mongooseUniqueValidator: require 'mongoose-unique-validator'
|
||||
jsonStableStringify: require 'json-stable-stringify'
|
||||
|
||||
Insight: require 'insight'
|
||||
SSHConnection: require 'ssh2'
|
||||
Negotiator: require 'negotiator'
|
||||
ObjectID: (require 'mongoose').Types.ObjectId
|
||||
EventEmitter: EventEmitter
|
||||
|
||||
ObjectId: (require 'mongoose').Schema.Types.ObjectId
|
||||
Mixed: (require 'mongoose').Schema.Types.Mixed
|
||||
|
||||
{bunyan, cookieParser, crypto, bodyParser, depd, express, fs, harp, mongoose} = app.libs
|
||||
{morgan, Insight, nodemailer, path, redis, _} = app.libs
|
||||
cookieParser = require 'cookie-parser'
|
||||
BunyanMongo = require 'bunyan-mongo'
|
||||
bodyParser = require 'body-parser'
|
||||
nodemailer = require 'nodemailer'
|
||||
Insight = require 'insight'
|
||||
morgan = require 'morgan'
|
||||
Mabolo = require 'mabolo'
|
||||
bunyan = require 'bunyan'
|
||||
redis = require 'redis'
|
||||
harp = require 'harp'
|
||||
|
||||
{_, fs, path, express} = app.libs
|
||||
|
||||
app.package = require './package'
|
||||
app.utils = require './core/utils'
|
||||
config = require './config'
|
||||
utils = require './core/utils'
|
||||
|
||||
app.insight = new Insight
|
||||
fs.chmodSync path.join(__dirname, 'config.coffee'), 0o750
|
||||
|
||||
if fs.existsSync config.web.listen
|
||||
fs.unlinkSync config.web.listen
|
||||
|
||||
insight = new Insight
|
||||
# 这个代码用于向 RootPanel 开发者提交匿名的统计信息
|
||||
# This code used to send anonymous usage metrics to RootPanel developers
|
||||
# 您不必修改这里 You do not have to modify it
|
||||
@@ -55,53 +56,45 @@ app.insight = new Insight
|
||||
packageName: app.package.name
|
||||
packageVersion: app.package.version
|
||||
|
||||
app.insight.track 'app.coffee'
|
||||
insight.track 'app.coffee'
|
||||
|
||||
app.bunyanMongo = new app.utils.bunyanMongo()
|
||||
redis = redis.createClient 6379, '127.0.0.1',
|
||||
auth_pass: config.redis.password
|
||||
|
||||
app.logger = bunyan.createLogger
|
||||
mailer = nodemailer.createTransport config.email.account
|
||||
|
||||
mabolo = new Mabolo utils.mongodbUri _.extend config.mongodb,
|
||||
name: config.mongodb.test
|
||||
|
||||
bunyanMongo = new BunyanMongo()
|
||||
|
||||
mabolo.on 'connected', bunyanMongo.setDB.bind bunyanMongo
|
||||
|
||||
logger = bunyan.createLogger
|
||||
name: app.package.name
|
||||
streams: [
|
||||
type: 'raw'
|
||||
level: 'info'
|
||||
stream: app.bunyanMongo
|
||||
stream: bunyanMongo
|
||||
,
|
||||
level: process.env.LOG_LEVEL ? 'debug'
|
||||
stream: process.stdout
|
||||
]
|
||||
|
||||
do ->
|
||||
config_path = path.join __dirname, 'config.coffee'
|
||||
_.extend app,
|
||||
utils: utils
|
||||
redis: redis
|
||||
config: config
|
||||
mabolo: mabolo
|
||||
mailer: mailer
|
||||
logger: logger
|
||||
models: mabolo.models
|
||||
insight: insight
|
||||
express: express()
|
||||
|
||||
unless fs.existsSync config_path
|
||||
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.models = {}
|
||||
|
||||
app.config = config
|
||||
app.db = require './core/db'
|
||||
app.cache = require './core/cache'
|
||||
app.i18n = require './core/i18n'
|
||||
app.cache = require './core/cache'
|
||||
app.pluggable = require './core/pluggable'
|
||||
|
||||
require './core/model/Account'
|
||||
@@ -133,7 +126,7 @@ 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 'views', path.join __dirname, 'core/view'
|
||||
app.express.set 'view engine', 'jade'
|
||||
|
||||
app.express.use '/component', require './core/router/component'
|
||||
@@ -149,13 +142,12 @@ app.billing.initPlans()
|
||||
app.pluggable.initPlugins()
|
||||
app.interfaces.Node.initNodes()
|
||||
|
||||
app.express.get '/', (req, res) ->
|
||||
unless res.headerSent
|
||||
res.redirect '/panel/'
|
||||
|
||||
app.express.use '/bower_components', express.static './bower_components'
|
||||
app.express.use harp.mount './core/static'
|
||||
|
||||
app.express.get '/', (req, res) ->
|
||||
res.redirect '/panel/'
|
||||
|
||||
exports.start = _.once ->
|
||||
app.express.listen config.web.listen, ->
|
||||
if fs.existsSync config.web.listen
|
||||
|
||||
@@ -1,28 +1,13 @@
|
||||
{config} = app
|
||||
{mongoose} = app.libs
|
||||
|
||||
{user, password, host, name} = config.mongodb
|
||||
|
||||
if user and password
|
||||
mongodb_uri = "mongodb://#{user}:#{password}@#{host}/#{name}"
|
||||
else
|
||||
mongodb_uri = "mongodb://#{host}/#{name}"
|
||||
|
||||
mongoose.connect mongodb_uri
|
||||
mongoose.connect app.utils.mongodbUri config.mongodb
|
||||
|
||||
mongoose.connection.on 'error', (err) ->
|
||||
console.error err if err
|
||||
|
||||
mongoose.connection.on 'connected', ->
|
||||
db = mongoose.connection.db
|
||||
|
||||
db.createCollection 'logs',
|
||||
capped: true
|
||||
size: 32 * 1024 * 1024
|
||||
, (err, cLogs) ->
|
||||
app.bunyanMongo.collection = cLogs
|
||||
app.bunyanMongo.dequeueCachedRecords()
|
||||
|
||||
cOption = db.collection 'options'
|
||||
|
||||
cOption.findOne
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
expressBunyanLogger = require 'express-bunyan-logger'
|
||||
expressSession = require 'express-session'
|
||||
redisStore = require 'connect-redis'
|
||||
|
||||
{config} = app
|
||||
{_, expressSession, redisStore, path, fs, moment, expressBunyanLogger} = app.libs
|
||||
{_, path, fs, moment, crypto} = app.libs
|
||||
{Account} = app.models
|
||||
|
||||
exports.errorHandling = (req, res, next) ->
|
||||
@@ -10,26 +14,32 @@ exports.errorHandling = (req, res, next) ->
|
||||
param ?= {}
|
||||
|
||||
if req.method in ['GET', 'HEAD', 'OPTIONS']
|
||||
res.status(status).send name
|
||||
res.status(status).send name.toString()
|
||||
else
|
||||
res.status(status).json _.extend param,
|
||||
error: name
|
||||
error: name.toString()
|
||||
|
||||
next()
|
||||
|
||||
exports.logger = ->
|
||||
return expressBunyanLogger
|
||||
logger: app.logger
|
||||
genReqId: (req) -> req.sessionID
|
||||
parseUA: false
|
||||
logger: app.logger
|
||||
excludes: [
|
||||
'req', 'res', 'body', 'short-body', 'http-version',
|
||||
'incoming', 'req-headers', 'res-headers'
|
||||
]
|
||||
genReqId: (req) -> req.sessionID
|
||||
|
||||
exports.session = ->
|
||||
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
|
||||
|
||||
RedisStore = redisStore expressSession
|
||||
secret = fs.readFileSync(path.join __dirname, '../session.key').toString()
|
||||
secret = fs.readFileSync(session_key_path).toString()
|
||||
|
||||
return expressSession
|
||||
store: new RedisStore
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
describe.skip 'app', ->
|
||||
it 'should can startup', ->
|
||||
@timeout 20000
|
||||
require('../../app').start()
|
||||
|
||||
it 'should connected to mongodb', (done) ->
|
||||
async.forever (callback) ->
|
||||
if app.db.readyState == 1
|
||||
callback true
|
||||
else
|
||||
setImmediate callback
|
||||
, ->
|
||||
done()
|
||||
|
||||
it 'app.libs should be loaded', ->
|
||||
{_, express, fs, mongoose} = app.libs
|
||||
_.should.be.ok
|
||||
express.should.be.ok
|
||||
fs.should.be.ok
|
||||
mongoose.should.be.ok
|
||||
|
||||
it 'app.logger should be available', ->
|
||||
app.logger.info.should.be.a 'function'
|
||||
app.logger.error.should.be.a 'function'
|
||||
|
||||
it 'config.coffee should exists', ->
|
||||
fs.existsSync("#{__dirname}/../../config.coffee").should.be.ok
|
||||
|
||||
it 'session.key should exists', ->
|
||||
fs.existsSync("#{__dirname}/../../session.key").should.be.ok
|
||||
|
||||
it 'models should be available', ->
|
||||
{Account, Ticket} = app.models
|
||||
Account.find.should.be.a 'function'
|
||||
Ticket.find.should.be.a 'function'
|
||||
@@ -61,3 +61,11 @@ exports.formatBillingTrigger = (name, plugin_name) ->
|
||||
return name
|
||||
else
|
||||
return "#{plugin_name}.#{part1}"
|
||||
|
||||
exports.mongodbUri = (config) ->
|
||||
{user, password, host, name} = config
|
||||
|
||||
if user and password
|
||||
return "mongodb://#{user}:#{password}@#{host}/#{name}"
|
||||
else
|
||||
return "mongodb://#{host}/#{name}"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rootpanel",
|
||||
"version": "0.8.1",
|
||||
"version": "0.9.0",
|
||||
"description": "A pluggable PaaS service development framework",
|
||||
"homepage": "http://rootpanel.io",
|
||||
"license": "AGPL-3.0",
|
||||
@@ -31,6 +31,7 @@
|
||||
"body-parser": "^1.9.3",
|
||||
"bower": "^1.3.12",
|
||||
"bunyan": "^1.2.3",
|
||||
"bunyan-mongo": "^0.1.0",
|
||||
"coffee-script": "^1.8.0",
|
||||
"connect-redis": "^2.1.0",
|
||||
"cookie-parser": "^1.3.3",
|
||||
@@ -45,6 +46,7 @@
|
||||
"insight": "^0.4.3",
|
||||
"jade": "^1.7.0",
|
||||
"json-stable-stringify": "^1.0.0",
|
||||
"mabolo": "^0.1.3",
|
||||
"markdown": "^0.5.0",
|
||||
"moment-timezone": "^0.2.5",
|
||||
"mongodb": "^2.0.7",
|
||||
@@ -62,7 +64,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^1.10.0",
|
||||
"coffee-coverage": "^0.4.2",
|
||||
"coffee-coverage": "0.4.2",
|
||||
"mocha": "^2.0.1",
|
||||
"mocha-reporter-cov-summary": "^0.1.0",
|
||||
"supertest": "^0.15.0"
|
||||
|
||||
@@ -63,6 +63,7 @@ module.exports =
|
||||
password: 'password'
|
||||
host: 'localhost'
|
||||
name: 'RootPanel'
|
||||
test: 'RootPanel-test'
|
||||
|
||||
redis:
|
||||
host: '127.0.0.1'
|
||||
|
||||
@@ -72,6 +72,7 @@ module.exports =
|
||||
password: 'password'
|
||||
host: 'localhost'
|
||||
name: 'RootPanel'
|
||||
test: 'RootPanel-test'
|
||||
|
||||
redis:
|
||||
host: '127.0.0.1'
|
||||
|
||||
@@ -61,6 +61,7 @@ module.exports =
|
||||
password: 'password'
|
||||
host: 'localhost'
|
||||
name: 'RootPanel'
|
||||
test: 'RootPanel-test'
|
||||
|
||||
redis:
|
||||
host: '127.0.0.1'
|
||||
|
||||
@@ -6,10 +6,8 @@ global.config = require '../config'
|
||||
global._ = require 'underscore'
|
||||
global.fs = require 'fs'
|
||||
global.async = require 'async'
|
||||
global.deepmerge = require 'deepmerge'
|
||||
global.chai = require 'chai'
|
||||
global.supertest = require 'supertest'
|
||||
global.ObjectId = (require 'mongoose').Types.ObjectId
|
||||
|
||||
if process.env.COV_TEST == 'true'
|
||||
require('coffee-coverage').register
|
||||
@@ -25,13 +23,6 @@ if process.env.COV_TEST == 'true'
|
||||
|
||||
global.expect = chai.expect
|
||||
|
||||
global.created_objects =
|
||||
accounts: []
|
||||
couponcodes: []
|
||||
tickets: []
|
||||
|
||||
global.namespace = {}
|
||||
|
||||
chai.should()
|
||||
chai.config.includeStack = true
|
||||
|
||||
@@ -54,3 +45,6 @@ global.unlessTravis = ->
|
||||
return describe
|
||||
else
|
||||
return describe.skip
|
||||
|
||||
require './snippet'
|
||||
require '../app'
|
||||
|
||||
35
test/snippet.coffee
Normal file
35
test/snippet.coffee
Normal file
@@ -0,0 +1,35 @@
|
||||
utils = require '../core/utils'
|
||||
|
||||
createAgent = (callback) ->
|
||||
agent = supertest.agent app.express
|
||||
|
||||
agent.get '/account/session_info'
|
||||
.end (err, res) ->
|
||||
callback err,
|
||||
agent: agent
|
||||
csrf_token: res.body.csrf_token
|
||||
|
||||
createLoggedAgent = (callback) ->
|
||||
createAgent (err, {agent, csrf_token}) ->
|
||||
username = 'test' + utils.randomString(10).toLowerCase()
|
||||
email = utils.randomString(10) + '@gmail.com'
|
||||
password = utils.randomString 10
|
||||
|
||||
agent.post '/account/register'
|
||||
.send
|
||||
csrf_token: csrf_token
|
||||
username: username
|
||||
email: email
|
||||
password: password
|
||||
.end (err, res) ->
|
||||
callback err,
|
||||
agent: agent
|
||||
username: username
|
||||
email: email
|
||||
password: password
|
||||
csrf_token: csrf_token
|
||||
account_id: res.body.account_id
|
||||
|
||||
_.extend global,
|
||||
createAgent: createAgent
|
||||
createLoggedAgent: createLoggedAgent
|
||||
Reference in New Issue
Block a user