This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
77 lines (58 loc) · 2.45 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
bodyParser = require 'body-parser' # Parses the request body
compress = require 'compression' # Compress served pages
express = require 'express' # Express + Socket.io
engines = require 'consolidate' # Template Engines
errorHandler = require 'errorhandler' # Error-Handling
favIcon = require 'serve-favicon' # Favicon
methodOverride = require 'method-override' # Override Methods
serverStatic = require 'serve-static' # Static File Response
socketIO = require 'socket.io' # WebSockets
userAgent = require 'express-useragent' # Get Request Device
exports.startServer = (config, callback) ->
port = process.env.PORT or config.server.port
env = process.env.NODE_ENV or 'development'
app = express()
server = app.listen port, ->
console.log "Express server listening on port %d in %s mode", port, env
io = socketIO.listen(server)
app.set 'port', port
app.set 'views', config.server.views.path
app.locals.basedir = config.server.views.path
app.engine config.server.views.extension, engines[config.server.views.compileWith]
app.set 'view engine', config.server.views.extension
app.use favIcon(__dirname + '/public/images/desktop/favicon.ico')
app.use bodyParser.urlencoded({extended: true})
app.use methodOverride()
app.use compress()
app.use express.static(config.watch.compiledDir)
app.use userAgent.express()
if env == 'development'
app.use errorHandler()
viewVars =
cachebust: if env isnt "production" then "?b=#{(new Date()).getTime()}" else ''
optimize: config.isOptimize ? false
reload: config.liveReload.enabled
# Rest routes
require( './server/routes/rest' ).setup( app )
# Normale routes
app.use setup = ( req, res, next ) ->
viewVars.device = null
viewVars.page = null
viewVars.viewPath = null
if req.useragent.isMobile
viewVars.device = 'mobile'
else if req.useragent.isBot
viewVars.device = 'bot'
else #if req.useragent.isDesktop
viewVars.device = 'desktop'
next()
require( './server/routes/default' ).setup( app, viewVars)
app.get '*', ( req, res, next ) ->
if viewVars.page and viewVars.device
viewVars.viewPath = viewVars.device + '/' + viewVars.page
res.render viewVars.viewPath, viewVars
else
res.render 'codes/notFound', viewVars
# Sockets
require( './server/sockets/acl' ).setup( app )
callback server, io