Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (180 loc) · 5.68 KB
/
Copy pathindex.js
File metadata and controls
205 lines (180 loc) · 5.68 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var http = require('http')
var url = require('url')
var fs = require('fs')
var express = require('express')
var validator = require('express-validator')
var cookieSession = require('cookie-session')
var bodyParser = require('body-parser')
var serveFavicon = require('serve-favicon')
var serveStatic = require('serve-static')
var csrf = require('csurf')
var Promise = require('bluebird')
var compression = require('compression')
var logger = require('../../util/logger')
var pathutil = require('../../util/pathutil')
var dbapi = require('../../db/api')
var datautil = require('../../util/datautil')
var auth = require('./middleware/auth')
var deviceIconMiddleware = require('./middleware/device-icons')
var browserIconMiddleware = require('./middleware/browser-icons')
var appstoreIconMiddleware = require('./middleware/appstore-icons')
var markdownServe = require('markdown-serve')
module.exports = function(options) {
var log = logger.createLogger('app')
, app = express()
, server = http.createServer(app)
app.use('/static/wiki', markdownServe.middleware({
rootDirectory: pathutil.root('node_modules/stf-wiki')
, view: 'docs'
}))
app.set('view engine', 'jade')
app.set('views', pathutil.resource('app/views'))
app.set('strict routing', true)
app.set('case sensitive routing', true)
app.set('trust proxy', true)
if (fs.existsSync(pathutil.resource('build'))) {
log.info('Using pre-built resources')
app.use(compression())
app.use('/static/app/build/entry',
serveStatic(pathutil.resource('build/entry')))
app.use('/static/app/build', serveStatic(pathutil.resource('build'), {
maxAge: '10d'
}))
}
else {
// Keep webpack-related requires here, as our prebuilt package won't
// have them at all.
log.info('Using webpack')
var webpackServerConfig = require('./../../../webpack.config').webpackServer
app.use('/static/app/build',
require('./middleware/webpack')(webpackServerConfig))
}
app.use('/static/bower_components',
serveStatic(pathutil.resource('bower_components')))
app.use('/static/app/data', serveStatic(pathutil.resource('data')))
app.use('/static/app/status', serveStatic(pathutil.resource('common/status')))
app.use('/static/app/browsers', browserIconMiddleware())
app.use('/static/app/appstores', appstoreIconMiddleware())
app.use('/static/app/devices', deviceIconMiddleware())
app.use('/static/app', serveStatic(pathutil.resource('app')))
app.use('/static/logo',
serveStatic(pathutil.resource('common/logo')))
app.use(serveFavicon(pathutil.resource(
'common/logo/exports/STF-128.png')))
app.use(cookieSession({
name: options.ssid
, keys: [options.secret]
}))
app.use(auth({
secret: options.secret
, authUrl: options.authUrl
}))
// This needs to be before the csrf() middleware or we'll get nasty
// errors in the logs. The dummy endpoint is a hack used to enable
// autocomplete on some text fields.
app.all('/app/api/v1/dummy', function(req, res) {
res.send('OK')
})
app.use(bodyParser.json())
app.use(csrf())
app.use(validator())
app.use(function(req, res, next) {
res.cookie('XSRF-TOKEN', req.csrfToken())
next()
})
app.get('/', function(req, res) {
res.render('index')
})
app.get('/app/api/v1/state.js', function(req, res) {
var state = {
config: {
websocketUrl: (function() {
var wsUrl = url.parse(options.websocketUrl, true)
wsUrl.query.uip = req.ip
return url.format(wsUrl)
})()
}
, user: req.user
}
if (options.userProfileUrl) {
state.config.userProfileUrl = (function () {
return options.userProfileUrl
})()
}
res.type('application/javascript')
res.send('var GLOBAL_APPSTATE = ' + JSON.stringify(state))
})
app.get('/app/api/v1/user', function(req, res) {
res.json({
success: true
, user: req.user
})
})
app.get('/app/api/v1/group', function(req, res) {
dbapi.loadGroup(req.user.email)
.then(function(cursor) {
return Promise.promisify(cursor.toArray, cursor)()
.then(function(list) {
list.forEach(function(device) {
datautil.normalize(device, req.user)
})
res.json({
success: true
, devices: list
})
})
})
.catch(function(err) {
log.error('Failed to load group: ', err.stack)
res.json(500, {
success: false
})
})
})
app.get('/app/api/v1/devices', function(req, res) {
dbapi.loadDevices()
.then(function(cursor) {
return Promise.promisify(cursor.toArray, cursor)()
.then(function(list) {
list.forEach(function(device) {
datautil.normalize(device, req.user)
})
res.json({
success: true
, devices: list
})
})
})
.catch(function(err) {
log.error('Failed to load device list: ', err.stack)
res.json(500, {
success: false
})
})
})
app.get('/app/api/v1/devices/:serial', function(req, res) {
dbapi.loadDevice(req.params.serial)
.then(function(device) {
if (device) {
datautil.normalize(device, req.user)
res.json({
success: true
, device: device
})
}
else {
res.json(404, {
success: false
})
}
})
.catch(function(err) {
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
res.json(500, {
success: false
})
})
})
server.listen(options.port)
log.info('Listening on port %d', options.port)
}