forked from jeromegn/DocumentUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
103 lines (78 loc) · 2.72 KB
/
server.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
# App timezone is UTC. This needs to be set before ANY Date() methods are called
# or it will be ignored
process.env.TZ = "EST"
# NODE_ENV
process.env.NODE_ENV ||= "development"
process.on "uncaughtException", (error)->
console.log "Caught exception: #{error}"
console.log error.stack.split("\n")
process.exit(1)
# Running this file fires up the Web server.
if module.id == "."
Server = require("./server")
Server.on "loaded", ->
console.log "Server is READY!"
if process.env.NODE_ENV == "development"
growl = require("growl").notify
growl "Restarted", title: "DocumentUp"
Server.listen 8080
return
require("sugar")
Express = require("express")
RedisStore = require('connect-redis')(Express)
Eco = require("eco")
Stylus = require("stylus")
Nib = require("nib")
FS = require("fs")
logger = require("./config/logger")
compileStylus = (str, path) ->
Stylus(str).set('filename', path).set('compress', true).use(Nib())
server = Express.createServer()
server.register ".eco", Eco
server = Express.createServer()
server.configure ->
server.set "root", __dirname
server.set "jsonp callback", true
server.use Express.query()
server.use Express.bodyParser()
server.use Express.cookieParser()
server.use logger.middleware()
server.use Express.session(secret: "c96dbcc746d551ea0665da4a23536280", store: new RedisStore)
# Templates and views
server.set "views", "#{__dirname}/app/views"
server.set "view engine", "eco"
server.set "view options"
layout: "layouts/default.eco"
release: new Date().toJSON()
env: server.settings.env
server.configure "development", ->
if process.env.DEBUG
server.use Express.profiler()
server.error Express.errorHandler(dumpExceptions: true, showStack: true)
# use Stylus
server.use Stylus.middleware
src: "#{__dirname}/app"
dest: "#{__dirname}/public"
compile: compileStylus
server.use server.router
server.use Express.static "#{__dirname}/public"
server.configure "production", ->
server.error Express.errorHandler()
server.use Express.responseTime()
server.use server.router
server.use Express.static "#{__dirname}/public", maxAge: 1000 * 60 * 60 * 24 * 14
server.on "listening", ->
require("./app/resources/projects")
require("./app/resources/site")
server.configure ->
server.use (error, req, res, next)->
logger.error(error)
if code = error.code
res.render "errors/#{code}", layout: "layouts/error", status: 404
else
res.render "errors/500", layout: "layouts/error", status: 500
# 404 error
server.use (req, res, next)->
res.render "errors/404", layout: "layouts/error", status: 404
server.emit "loaded"
module.exports = server