/
server.coffee
executable file
·147 lines (130 loc) · 4.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
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
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env coffee
express = require('express')
morgan = require('morgan')
bodyParser = require('body-parser')
paths = require('./config.json').paths
child_process = require('child_process')
_ = require('lodash')
es = require('event-stream')
ansi_up = require('ansi_up')
basicAuth = require('basic-auth')
jade = require('jade')
config = require('./config.json')
app = express()
# top-level middleware
app.use(morgan('common'))
admin = express.Router()
app.use('/admin/', admin)
app.use('/preview', express.static(paths.preview))
app.use('/', express.static(paths.public))
# password-protected admin page
admin.use((req, res, next)->
user = basicAuth(req) or {}
if user.pass is process.env.ADMIN_PASSWORD
next()
else
res.set('WWW-Authenticate', 'Basic realm=Authorization Required')
res.sendStatus(401)
)
# display page with version data
admin.get('/', (req, res, next)->
unless req.originalUrl.match(/\/$|\/\?/)
return res.redirect(301, req.baseUrl + req.url)
options = {
pretty: true
cache: false#process.env.NODE_ENV isnt 'development'
}
adminTemplate = jade.compileFile('./src/templates/admin.jade', options)
context = _.extend({}, config.globals, {versions: []})
html = adminTemplate(context)
res.send(html)
)
admin.get('/versions', (req, res, next)->
env = {}
if process.env.OPENSHIFT_HOMEDIR
env.GIT_DIR = "#{process.env.OPENSHIFT_HOMEDIR}git/#{process.env.OPENSHIFT_APP_NAME}.git"
rs = String.fromCharCode(30)
us = String.fromCharCode(31)
fmt = ['%H','%an <%ae>','%ai','%s'].join(us)+rs # {"commit": "%H", "author": "%an <%ae>", "date": "%ad", "message": "%f"}'+rs
child = child_process.spawn('git', ['log', '--pretty=format:'+fmt, 'gh-pages'], {env: env})
child.stdout
.pipe(es.split(rs))
.pipe(es.mapSync((record)->
fields = record.split(us)
if fields.length > 1
return {
commit: fields[0].replace(/\W/g, '')
author: fields[1]
date: fields[2]
subject: fields[3]
}
))
.pipe(es.writeArray((err, versions)->
if err then return next(err)
res.jsonp(versions)
))
)
# support streaming responses
admin.use(bodyParser.urlencoded({ extended: false }))
admin.use((req, res, next)->
res.runCommand = (cmd, args=[], env={})->
res.type('html')
res.writeContinue()
for i in [1..20]
res.write(" \n")
res.write('<!DOCTYPE html><html><body><pre>')
res.write("<b>$ #{cmd} #{args.join(' ')}\n</b>")
childEnv = _.defaults(env, process.env, {
DEBUG: 'gulp-spreadsheets'#,gulp-drive'
DEBUG_COLORS: true
})
child = child_process.spawn(cmd, args, {env: childEnv})
es.merge([child.stdout, child.stderr])
.pipe(es.through((text)->
process.stderr.write(text)
html = ansi_up.ansi_to_html(ansi_up.escape_for_html(''+text))
@emit('data', html)
, (end)->
))
.pipe(res)
child.on('exit', (code, signal)->
if code isnt 0
res.write('<b>Error '+code+"</b>\n")
res.write('</pre><a href=".">Back to Admin Panel</a>')
res.end()
)
# support stopping the build if res.connection closes
unless req.body.nohup
res.on('close', ->
child.kill('SIGHUP')
)
next()
)
admin.post('/rebuild', (req, res, next)->
env = {
DEST_PATH: 'preview'
}
res.runCommand('gulp', ['build', '--color'], env)
)
admin.post('/commit', (req, res, next)->
env = {
DEST_PATH: 'preview'
GIT_AUTHOR_NAME: req.body.commit_author or 'Anonymous'
}
if req.body.rebuild_first
env.REBUILD_FIRST = 1
if req.body.publish_immediately
env.PUBLISH_IMMEDIATELY = 1
res.runCommand('./commit.sh', [req.body.commit_message], env)
)
admin.post('/publish', (req, res, next)->
res.runCommand('./publish.sh', [req.body.commit_id])
)
module.exports = app
# support running this file directly
if module is require.main
host = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0'
port = process.env.OPENSHIFT_NODEJS_PORT || 8000
server = app.listen(port, host, ->
console.log("HTTP server started on port", this.address().port)
)