forked from Notalib/LYT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
executable file
·91 lines (79 loc) · 2.37 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
#!/usr/bin/env coffee
express = require 'express'
watchr = require 'watchr'
exec = require('child_process').exec
proxy = require('http-proxy').createProxyServer()
argv = require 'optimist'
.usage 'Starts a local dev server that proxies DODP calls'
.demand 'r'
.alias 'r', 'remote-host'
.describe 'r', 'Proxy /DodpMobile, /DodpFiles and /CatalogSearch to this url'
.default 'r', 'http://m.e17.dk/'
.alias 'q', 'quiet'
.describe 'q', 'Be quieter please'
.alias 's', 'silence'
.describe 's', 'Silence please'
.alias 'p', 'port'
.describe 'p', 'Start the server at this port'
.default 'p', 8080
.argv
proxy.off( 'error' ).on 'error', (e) ->
unless argv.quiet or argv.silence
if e.code is 'ECONNRESET'
console.log 'The client reset connect'
else
console.log 'proxy error:', e
app = express()
app.use require('body-parser').urlencoded
extended: false
app.use require('morgan')() if not (argv.quiet or argv.silence)
app
.use express.static( process.cwd() + '/build' )
.use (req, res, next) ->
if req.url.match( /^\/(Dodp(Mobile|Files)|CatalogSearch)/ )
proxy.proxyRequest req, res, target: argv['remote-host']
else if req.url.match /\.buildnumber$/
tries = 0
delay = 10
maxTries = 60*1000/delay
old_buildnumber = buildnumber
interval = setInterval(
->
tries += 1
if old_buildnumber isnt buildnumber or tries >= maxTries
res.write "" + buildnumber
res.end()
clearInterval interval
delay
)
else if req.url.match /test\/results/
console.log req.body
res.write "ok"
res.end()
else
next()
exec 'cake -dnt app', ->
if not argv.silence
console.log 'Fininshed build'
server = app.listen argv.port, ->
if not argv.silence
console.log 'Listening on port %d', server.address().port
changedTimeout = null
buildnumber = 1
fileChanged = (filePath) ->
clearTimeout( changedTimeout ) if changedTimeout
if not argv.silence
console.log 'Rebuild after change to ' + filePath
changedTimeout = setTimeout(
=>
exec 'cake -dnt app', ->
if not argv.silence
console.log 'Fininshed rebuild'
buildnumber++
100
)
watchr.watch
paths: [ 'html', 'src', 'scss' ],
listeners:
change: ( changeType, filePath, fileCurrentStat, filePreviousStat ) ->
fileChanged filePath