-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
executable file
·90 lines (70 loc) · 2.01 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
# Node dependencies
fs = require 'fs'
http = require 'http'
# Library dependencies
async = require 'async'
express = require 'express'
ioLib = require 'socket.io'
swig = require 'swig'
# Project dependencies
Util = require './server/util.coffee'
Packager = require './server/packager.coffee'
Sockets = require './server/sockets.coffee'
PORT = 3000
# Dev swig opts (namely cache: false)
swig.init
allowErrors: false,
autoescape: true,
cache: false,
encoding: 'utf8',
filters: {},
root: '.',
tags: {},
extensions: {},
tzOffset: 0
serverError = (res, msg) ->
console.log "ERROR: #{msg}"
res.status(500).send(msg)
startServer = ->
app = socketIOServer = null
async.auto {
initServer: (next) =>
app = express()
socketIOServer = http.createServer(app)
next()
socketIO: ['initServer', (next) =>
io = ioLib.listen(socketIOServer)
io.set('log level', 1) # 1 = warn
io.sockets.on 'connection', Sockets.connect
next()
]
routes: ['initServer', (next) =>
app.get '/js/*', (req, res) ->
path = "client/#{ req.url[1..] }"
res.sendfile path
app.get '/css/*', (req, res) ->
path = "client/#{ req.url[0...-4] }.less"
await fs.readFile path, 'utf-8', defer err, data
await Util.compileCSS data, defer err, css
res.header 'Content-Type', 'text/css'
res.send css
app.get '/*', (req, res) ->
await async.parallel [
Packager.packageTemplates
Packager.packageCoffeeFiles
], defer err, results
return serverError err if err
data =
views: results[0]
clientJS: results[1]
content = swig.compileFile('index.html').render(data)
res.send content
socketIOServer.listen PORT
next()
]
ready: ['routes', 'socketIO', (next) ->
console.log "Server started on port #{ PORT }"
next()
]
}
startServer()