Created
June 25, 2013 01:12
-
-
Save seka/5855131 to your computer and use it in GitHub Desktop.
configure class pattern 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### ------- Module dependencies. ------ ### | |
# Webアプリの立ち上げに必要なモジュール | |
express = require 'express' | |
cluster = require 'cluster' | |
app = express() | |
### ------- Class. -------------------- ### | |
class AppConfig | |
setViewEngine : (app, express) -> | |
app.engine(@template_name, @template) | |
app.set('view engine', @template) | |
portInitialize : (app) -> | |
app.set(@port) | |
fileUpLoadInitialize : (app, express) -> | |
bodyParserOptions = { | |
uploadDir: "#{__dirname}/tmp" | |
isKeepExtensions: true | |
} | |
app.use(express.bodyParser(bodyParserOptions)) | |
settingFavicon : (app, express) -> | |
app.use(express.favicon()) | |
compressInitialize : (app, express) -> | |
app.use(express.compress({ | |
level: 4 | |
})) | |
requestPost : (app, express) -> | |
app.use(express.methodOverride()) | |
publicDirInitialize : (app, express) -> | |
app.use(express.static(@public_dir)) | |
constructor : (app, express) -> | |
@port = 3000 | |
@views = "#{__dirname}/views" | |
@template = require 'ejs-locals' | |
@template_name = 'ejs' | |
@public_dir = "#{__dirname}/public" | |
### ------- middleware call. ---------- ### | |
app.configure -> | |
test = new AppConfig() | |
test.setViewEngine(app, express) | |
test.portInitialize(app, express) | |
test.fileUpLoadInitialize(app, express) | |
test.settingFavicon(app, express) | |
test.compressInitialize(app, express) | |
test.publicDirInitialize(app, express) | |
console.log "express config set." | |
### ------- create httpServer. --------- ### | |
if (cluster.isMaster) | |
http = require 'http' | |
server = http.createServer(app) | |
# server listen | |
server.listen app.get('port'), -> | |
console.log "Master Server listening on #{app.get('port')}" | |
# database setup | |
database_root = "#{__dirname}/routes/database" | |
database = require(database_root)() | |
# rooting start | |
# databaseを設定した後にcontrollerのsetup | |
# controllerの設定をした後にsocket_moduleのsetup | |
timer_id = setTimeout( | |
-> | |
controller_root = "#{__dirname}/routes/controller" | |
console.log "#{require(controller_root)(app: app, database: database)}" | |
100 | |
) | |
### ------- Error. ------------------ ### | |
# nodeがERRによって落ちないようにする | |
process.on 'uncaughtException', (err) -> | |
console.log "err > #{err}" | |
console.error "uncaughtException > #{err.stack}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment