Skip to content

Instantly share code, notes, and snippets.

@seka
Created Jun 25, 2013
Embed
What would you like to do?
configure class pattern 1
### ------- 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