forked from thomas4019/expressa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
289 lines (253 loc) · 9.85 KB
/
index.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const fs = require('fs')
const express = require('express')
const bodyParser = require('body-parser')
const debug = require('debug')('expressa')
const randomstring = require('randomstring')
const dbTypeNames = ['cached', 'file', 'memory', 'postgres', 'mongo']
const dbTypes = dbTypeNames.reduce((obj, name) => {
obj[name] = require('./db/' + name)
return obj
}, {})
dbTypes['mongodb'] = dbTypes['mongo'] // alias
const auth = require('./auth')
const util = require('./util')
const collectionsApi = require('./controllers/collections')
const usersApi = require('./controllers/users')
const installApi = require('./controllers/install')
const statusApi = require('./controllers/status')
const userPermissions = require('./middleware/users_permissions')
const loggingMiddleware = require('./middleware/logging')
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
async function bootstrapCollections (router) {
const collections = await router.db.collection.all()
await Promise.all(collections.map((collection) => {
if (!dbTypes[collection.storage]) {
console.error('missing ' + collection.storage + ' dbtype which is used by ' + collection._id)
console.error('try updating to the latest version of expressa')
}
return router.setupCollectionDb(collection)
}))
}
let initialized = false
async function initCollections (db, router) {
debug('init collections')
try {
const data = await db.settings.get(process.env.NODE_ENV)
Object.assign(router.settings, data)
} catch (err) {
const filePath = router.settings.file_storage_path || 'data'
if (!fs.existsSync(filePath + '/settings/' + process.env.NODE_ENV + '.json')) {
console.error('Settings not found. Please visit the expressa admin page for the installation process.')
console.error('This is likely at http://localhost:3000/admin but may be different if you changed ports, etc.')
} else {
console.error('error reading settings')
console.error(err.stack)
}
}
console.log('Starting expressa server using environment: ' + process.env.NODE_ENV) // eslint-disable-line no-console
db.collection = dbTypes[router.settings.collection_db_type || 'file'](router.settings, 'collection')
await bootstrapCollections(router)
debug('collections loaded.')
await Promise.all([
require('./listeners_collection_permissions')(router),
require('./listeners')(router),
require('./listeners_validation')(router),
require('./listeners_users')(router),
])
debug('added standard listeners')
}
function ph (requestHandler) {
return async function wrapper (req, res, next) {
const collectionName = req.params.collection
let collection = {}
try {
collection = collectionName ? await req.db.collection.get(collectionName) : {}
} catch (e) {
if (!e.message.includes('document not found')) {
console.error(e)
}
}
if (!collection.allowHTTPCaching) {
res.header('Cache-Control', 'no-cache, no-store, must-revalidate')
res.header('Expires', '-1')
res.header('Pragma', 'no-cache')
}
try {
const result = await requestHandler(req, res, next)
if (typeof result !== 'object') {
res.status(500).send('invalid result')
}
debug(`${200} ${req.method} ${req.url} body: ${JSON.stringify(req.body)} result: ${JSON.stringify(result)}`)
res.send(result)
} catch (err) {
err.status = err.status || 500
debug(`${err.status} ${req.method} ${req.url} ${err.stack}`)
if ((req.settings.print_400_errors) || err.status >= 500) {
console.error(err)
}
res.status(err.status).send({ error: err.result || err.message || err })
}
}
}
module.exports.api = function (settings) {
const router = express.Router()
router.custom = express.Router()
router.dbTypes = dbTypes
router.settings = settings || {}
router.db = {
settings: dbTypes[router.settings.settings_db_type || 'file'](router.settings, 'settings')
}
router.util = {
get pgpool() {
return util.getPgPool(router.settings.postgresql_uri)
},
ApiError: util.ApiError,
generateDocumentId: util.generateDocumentId,
doLogin: auth.doLogin
}
router.eventListeners = {}
router.setupCollectionDb = async function(collection) {
debug(`init collection db ${collection._id} ${collection.storage}`)
router.db[collection._id] = dbTypes[collection.storage](router.settings, collection._id, collection)
if (collection.cacheInMemory) {
router.db[collection._id] = dbTypes['cached'](router.db[collection._id])
debug('initializing ' + collection._id + ' as a memory cached collection')
}
return Promise.resolve(router.db[collection._id].init())
}
router.addSingleCollectionListener = function (event, listener) {
if (!router.eventListeners[event]) {
router.eventListeners[event] = []
}
router.eventListeners[event].push(listener)
router.eventListeners[event].sort((a, b) => a.priority - b.priority)
if (event === 'ready' && initialized) {
listener(router)
}
}
router.addCollectionListenerWithPriority = function (events, collections, priority, listener) {
listener.priority = priority
router.addCollectionListener(events, collections, listener)
}
router.addLateCollectionListener = function (events, collections, listener) {
listener.priority = 10
router.addCollectionListener(events, collections, listener)
}
router.addCollectionListener = function (events, collections, listener) {
events = util.castArray(events)
collections = util.castArray(collections)
listener.priority = listener.priority || 0
listener.collections = collections
if (!listener.name) {
throw new Error('Listeners must be named')
}
events.forEach(function (event) {
router.addSingleCollectionListener(event, listener)
})
}
router.addListener = function addListener (events, priority, listener) {
events = util.castArray(events)
if (typeof priority === 'function') {
listener = priority
priority = 0
}
if (!listener.name) {
throw new Error('Listeners must be named')
}
listener.priority = priority
events.forEach(function (event) {
router.addSingleCollectionListener(event, listener)
})
}
router.getSetting = function (name) {
return util.getPath(router.settings, name)
};
(async function setup() {
await initCollections(router.db, router)
const modules = ['collections', 'core', 'logging', 'permissions']
router.modules = {}
for (const module of modules) {
router.modules[module] = require(`./modules/${module}/${module}`)
if (router.modules[module].init) {
router.modules[module].init(router)
}
}
addRoutesAndMiddleware()
try {
await installApi.updateAdminPermissions(router)
} catch (e) {
console.error('Error while attempting to update admin permissions')
console.error(e)
}
initialized = true
await util.notify('ready', router)
})()
function addRoutesAndMiddleware() {
// Allow CORS
router.use(function addCorsHeaders(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-access-token')
next()
})
router.use(bodyParser.json({
type: '*/*', // The wildcard type ensures it works even without the application/json header
limit: router.settings.body_limit || '1mb',
}))
router.use(function attachVariablesToReq(req, res, next) {
req.settings = router.settings
req.eventListeners = router.eventListeners
req.db = router.db
req.modules = router.modules
req.setupCollectionDb = router.setupCollectionDb
req.getSetting = router.getSetting
req.requestId = randomstring.generate(12)
res.header('X-Request-ID', req.requestId)
next()
})
router.use(loggingMiddleware)
router.notify = util.notify
router.post('/install', ph(async (req) => installApi.install(req, router)))
router.get('/install/settings/schema', ph(async (req) => installApi.getSettingsSchema(req, router)))
router.post('/user/login', ph(usersApi.login))
router.use(auth.middleware) // Add user id to request
router.use(userPermissions.middleware) // Add user and permissions to request
router.get('/status', ph(statusApi.getStatus(router)))
router.use(router.custom) // Externally added middleware
router.post('/user/register', ph(usersApi.register))
router.get('/users?/me', ph(usersApi.getMe))
router.get('/:collection/schema', ph(collectionsApi.getSchema))
router.get('/:collection', ph(collectionsApi.get))
router.post('/:collection', ph(collectionsApi.insert))
router.get('/:collection/:id', ph(collectionsApi.getById))
router.put('/:collection/:id', ph(collectionsApi.replaceById))
router.post('/:collection/:id/update', ph(collectionsApi.updateById))
router.delete('/:collection/:id', ph(collectionsApi.deleteById))
// Error handler, log and send to user
// eslint-disable-next-line no-unused-vars
router.use(function safeErrorHandler(err, req, res, next) {
console.error('my err handler')
console.error(err.stack)
res.status(res.errCode || 500)
if (process.env.DEBUG || (req.hasPermission && req.hasPermission('view errors'))) {
res.send({
error: err
})
} else {
res.send({
error: 'something wrong happened'
})
}
})
}
return router
}
module.exports.admin = function (settings) {
const router = express.Router()
router.get('/settings.js', function (req, res) {
res.set('Content-Type', 'text/javascript')
res.send('window.settings = ' + JSON.stringify(settings || {}) + '')
})
router.use(express.static(__dirname + '/modules/admin/dist'))
return router
}