This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathqest.coffee
executable file
·137 lines (107 loc) · 3.87 KB
/
qest.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/env coffee
# Module dependencies.
optimist = require 'optimist'
express = require 'express'
path = require 'path'
fs = require 'fs'
hbs = require 'hbs'
redis = require 'redis'
mqtt = require "mqttjs"
EventEmitter = require('events').EventEmitter
RedisStore = require('connect-redis')(express)
ascoltatori = require('ascoltatori')
# Create Server
module.exports.app = app = express()
http = require('http').createServer(app)
# Configuration
app.redis = {}
module.exports.configure = configure = ->
app.configure 'development', ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
app.configure 'production', ->
app.use(express.errorHandler())
app.configure ->
app.set('views', __dirname + '/app/views')
app.set('view engine', 'hbs')
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.cookieParser())
app.use(express.session(secret: "wyRLuS5A79wLn3ItlGVF61Gt",
store: new RedisStore(client: app.redis.client), maxAge: 1000 * 60 * 60 * 24 * 14)) # two weeks
app.use(app.router)
app.use(express.static(__dirname + '/public'))
# setup websockets
io = app.io = require('socket.io').listen(http)
io.configure 'production', ->
io.enable('browser client minification'); # send minified client
io.enable('browser client etag'); # apply etag caching logic based on version number
io.enable('browser client gzip'); # gzip the file
io.set('log level', 0)
io.configure 'test', ->
io.set('log level', 0)
load("models")
load("controllers")
load("helpers")
load = (key) ->
app[key] = {}
loadPath = __dirname + "/app/#{key}/"
for component in fs.readdirSync(loadPath)
if component.match /(js|coffee)$/
component = path.basename(component, path.extname(component))
loadedModule = require(loadPath + component)(app)
component = loadedModule.name if loadedModule?.name? and loadedModule.name != ""
app[key][component] = loadedModule
# Start the module if it's needed
optionParser = optimist.
default('port', 3000).
default('mqtt', 1883).
default('redis-port', 6379).
default('redis-host', '127.0.0.1').
default('redis-db', 0).
usage("Usage: $0 [-p WEB-PORT] [-m MQTT-PORT] [-rp REDIS-PORT] [-rh REDIS-HOST]").
alias('port', 'p').
alias('mqtt', 'm').
alias('redis-port', 'rp').
alias('redis-host', 'rh').
alias('redis-db', 'rd').
describe('port', 'The port the web server will listen to').
describe('mqtt', 'The port the mqtt server will listen to').
describe('redis-port', 'The port of the redis server').
describe('redis-host', 'The host of the redis server').
boolean("help").
describe("help", "This help")
argv = optionParser.argv
module.exports.setupAscoltatore = setupAscoltatore = (opts = {}) ->
app.ascoltatore = new ascoltatori.RedisAscoltatore
redis: redis
port: opts.port
host: opts.host
db: opts.db
module.exports.setup = setup = (opts = {}) ->
args = [opts.port, opts.host]
app.redis.client = redis.createClient(args...)
app.redis.client.select(opts.db || 0)
setupAscoltatore(opts)
start = module.exports.start = (opts={}, cb=->) ->
opts.port ||= argv.port
opts.mqtt ||= argv.mqtt
opts.redisPort ||= argv['redis-port']
opts.redisHost ||= argv['redis-host']
opts.redisDB ||= argv['redis-db']
if argv.help
optionParser.showHelp()
return 1
setup(port: opts.redisPort, host: opts.redisHost, db: opts.redisDB)
configure()
countDone = 0
done = ->
cb() if countDone++ == 2
http.listen opts.port, ->
console.log("mqtt-rest web server listening on port %d in %s mode", opts.port, app.settings.env)
done()
mqtt.createServer(app.controllers.mqtt_api).listen opts.mqtt, ->
console.log("mqtt-rest mqtt server listening on port %d in %s mode", opts.mqtt, app.settings.env)
done()
app
if require.main.filename == __filename
start()