-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.coffee
More file actions
94 lines (74 loc) · 2.76 KB
/
app.coffee
File metadata and controls
94 lines (74 loc) · 2.76 KB
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
_ = require('underscore')
redis = require('redis')
express = require('express')
RedisStore = require('connect-redis')(express)
property = require('./lib/property')
config = require('./config')
middleware = require('./lib/middleware')()
hacks = require('./lib/hacks')
app = module.exports = express.createServer()
app.NODE_ENV = global.process.env.NODE_ENV || 'development'
public_dir = __dirname + '/public'
# Configuration
app.configure () ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.set 'view options'
app.use express.bodyParser()
app.use express.methodOverride()
app.use express.cookieParser()
app.use express.session
store : new RedisStore {
port : config.REDIS_PORT,
db : config.SESSION_DB
}
secret : config.SESSION_SECRET
cookie : { maxAge : config.SESSION_MAX_AGE }
app.use require('stylus').middleware
src : __dirname + '/public'
compress : app.NODE_ENV == 'production'
app.use express.static(__dirname + '/public')
app.use middleware.loadUser
app.use middleware.checkIfDeveloper
app.helpers
NODE_ENV : app.NODE_ENV
app.dynamicHelpers
req : (req) -> req
title : () -> property.create()
cssIncludes : () -> property.create([])
jsIncludes : () -> property.create([])
app.use app.router
app.redis = redis.createClient(config.REDIS_PORT)
app.redis.select config.DATA_DB
app.redis.on 'connect', hacks.onConnect(app.redis, config.DATA_DB)
app.redis.on 'error', hacks.onError
app.statRedis = redis.createClient(config.REDIS_PORT)
app.statRedis.select config.STAT_DB
app.statRedis.on 'connect', hacks.onConnect(app.statRedis, config.STAT_DB)
app.statRedis.on 'error', hacks.onError
app.configure 'development', () ->
app.use express.errorHandler { dumpExceptions: true, showStack: true }
app.configure 'production', () ->
app.use express.errorHandler()
# Setup the app params
kanaSet = ['katakana', 'hiragana', 'kana']
app.param 'kanaSet', (req, res, next, set) ->
if not _.contains(kanaSet, set)
return next(Error("Invalid kana set #{ set }"))
next()
# Controllers
require(__dirname + '/controllers/pages')(app)
require(__dirname + '/controllers/kana')(app)
require(__dirname + '/controllers/kanafilter')(app)
require(__dirname + '/controllers/stats')(app)
require(__dirname + '/controllers/guess')(app)
app.all '*', (req, res) ->
res.render '404', { status : 404, layout : false }
app.error (err, req, res, next) ->
if err instanceof NotFound
res.render '404', { status : 404, layout : false }
else
next err
if not module.parent
app.listen(global.process.env.PORT || config.SERVER_PORT)
console.log "Express server listening on port %d in %s mode", app.address().port, app.NODE_ENV