-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.coffee
72 lines (62 loc) · 1.81 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
require('joose')
require('joosex-namespace-depended')
require('hash')
Fs = require('fs')
Sys = require('sys')
Config = JSON.parse(Fs.readFileSync('config.json', 'utf8'))
Http = require('http')
Express = require('express')
IO = require('socket.io')
Jade = require('jade')
Helpers = require('./helpers')
Host = if process.env.NODE_ENV == 'production' then 'kindlebility.darkhax.com' else 'localhost:9090'
# Write out a pidfile when we start
PidFile = "node.#{process.pid}.pid"
Fs.writeFileSync(PidFile, process.pid.toString())
# Cleanup pidfile on exit
process.on 'exit', ->
Fs.unlinkSync(PidFile)
# Exit cleanly, firing events
process.on 'SIGINT', ->
process.exit(0)
# Catch errors with Hoptoad
Hoptoad = require('hoptoad-notifier').Hoptoad
Hoptoad.key = Config.hoptoad
process.on 'uncaughtException', (error) ->
# But only in production mode
if process.env.NODE_ENV == 'production'
Hoptoad.notify error, ->
# Let's exit, since we're not entirely sure what state the app might be in
process.exit(1)
else
Sys.puts(error)
app = Express.createServer()
app.configure ->
app.use(Express.static(__dirname + '/public'))
app.set('view engine', 'jade')
app.get '/', (req, res) ->
res.render('index', {
layout: false,
locals: {
host: Host
}
})
app.get /\/go|bookmarklet\.js/, (req, res) ->
res.contentType('text/javascript')
if Helpers.verifyParams(req)
res.render('bookmarklet.ejs', {
layout: false,
locals: {
to: req.param('to'),
host: Host
}
})
else
res.send("alert('Invalid request. Missing query params. Try making your bookmarklet again.');")
app.listen(9090)
socket = IO.listen(app)
socket.on 'connection', (client) ->
client.on 'message', (data) ->
json = JSON.parse(data)
json['client'] = client
Helpers.processSocketIO(json)