forked from koopjs/koop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (250 loc) · 8.56 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
290
291
292
293
294
var express = require('express')
var bodyParser = require('body-parser')
var pkg = require('./package')
var _ = require('lodash')
var multipart = require('connect-multiparty')()
var lib = require('./lib')
module.exports = function (config) {
var koop = express()
// inherit everything from lib
// TODO: only expose constructors and methods that are used by other modules
// TODO: deprecate then remove exposed lib modules from koop
for (var method in lib) {
if (lib.hasOwnProperty(method)) koop[method] = lib[method]
}
koop.version = pkg.version
koop.config = config || {}
koop.log = new koop.Logger(koop.config)
koop.files = new koop.Files({
config: koop.config,
log: koop.log
})
// default to LocalDB cache
// cache registration overrides this
// TODO: instance should be lowercased
koop.Cache = new koop.DataCache()
koop.Cache.db = koop.LocalDB
// object for keeping track of registered services
// TODO: why not called providers?
// if services includes caches and plugins we should put them in here too
koop.services = {}
// TODO: consolidate status, services, `/providers` routes
koop.status = {
version: koop.version,
providers: {}
}
// expose default routes for later additions & edits by plugins
koop.defaultRoutes = {
'featureserver': ['/FeatureServer/:layer/:method', '/FeatureServer/:layer', '/FeatureServer'],
'preview': ['/preview'],
'drop': ['/drop']
}
if (!koop.config.db || !koop.config.db.conn) {
koop.log.warn('No cache configured, defaulting to local in-memory cache. No data will be cached across server sessions.')
}
/**
* express middleware setup
*/
// handle POST requests
// parse application/x-www-form-urlencoded
koop.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
koop.use(bodyParser.json())
koop.use(function (req, res, next) {
// request parameters can come from query url or POST body
req.query = _.extend(req.query || {}, req.body || {})
// remove the x powered by header from all responses
res.removeHeader('X-Powered-By')
next()
})
// for demos and preview maps in providers
koop.set('view engine', 'ejs')
koop.use(express.static(__dirname + '/public'))
/**
* public methods
*/
/**
* general method for registering providers, caches, and plugins
*
* @param {object} plugin - module to be registered
*/
koop.register = function (plugin) {
if (typeof plugin === 'undefined') throw new Error('Plugin undefined.')
if (!plugin.name) throw new Error('Cannot register plugin: missing name.')
if (plugin.type) {
if (plugin.type === 'provider') return koop.registerProvider(plugin)
if (plugin.type === 'cache') return koop.registerCache(plugin)
if (plugin.type === 'plugin') return koop.registerPlugin(plugin)
koop.log.warn('Unrecognized plugin type: "' + plugin.type + '". Defaulting to provider.')
return koop.registerProvider(plugin)
}
koop.log.warn('Plugin "%s" missing type property. Defaulting to provider.', plugin.name)
koop.registerProvider(plugin)
}
/**
* registers a provider
* exposes the provider's routes, controller, and model
*
* @param {object} provider - the provider to be registered
*/
koop.registerProvider = function (provider) {
koop.services[provider.name] = provider
var model = provider.model(koop)
var controller = provider.controller(model, koop.BaseController)
provider.version = provider.version || '(version missing)'
// if a provider has a status object store it
// TODO: deprecate & serve more meaningful status reports dynamically.
if (provider.status) {
koop.status.providers[provider.name] = provider.status
provider.version = provider.status.version
}
// binds a series of standard routes
if (provider.name && provider.pattern) {
koop._bindDefaultRoutes(provider.name, provider.pattern, controller)
}
// add each route, the routes let us override defaults etc.
koop._bindRoutes(provider.routes, controller)
koop.log.info('registered provider:', provider.name, provider.version)
}
/**
* registers a cache
* overwrites any existing koop.Cache.db
*
* @param {object} cache - a koop database adapter
*/
koop.registerCache = function (cache) {
if (!koop.config.db || !koop.config.db.conn) throw new Error('Cannot register cache: missing config.db.conn.')
koop.Cache.db = cache.connect(koop.config.db.conn, { log: koop.log })
koop.log.info('registered cache:', cache.name, cache.version)
}
/**
* registers a plugin
* Plugins can be any function that you want to have global access to
* within koop provider models
*
* @param {object} any koop plugin
*/
koop.registerPlugin = function (plugin) {
koop[plugin.name] = plugin
koop.log.info('registered plugin:', plugin.name, plugin.version)
}
/**
* creates default routes based on pattern from provider
*
* @param {string} name - provider name
* @param {string} pattern - route pattern from provider
* @param {object} controller - provider controller
*/
koop._bindDefaultRoutes = function (name, pattern, controller) {
for (var handler in koop.defaultRoutes) {
if (controller[handler]) {
koop.defaultRoutes[handler].forEach(function (route) {
koop.get('/' + name + pattern + route, controller[handler])
// add multipart middleware for POSTs to featureservices
koop.post('/' + name + pattern + route, multipart, controller[handler])
})
}
}
}
/**
* binds each route from provider routes object to corresponding controller handler
*
* @param {object} routes - provider routes
* @param {object} controller - provider controller
*/
koop._bindRoutes = function (routes, controller) {
for (var route in routes) {
var path = route.split(' ')
koop[path[0]](path[1], controller[routes[route]])
}
}
/**
* route definitions
*/
/**
* serves koop.services object
*
* @param {object} req - incoming request
* @param {object} res - outgoing response
*/
koop.get('/providers', function (req, res) {
res.json(koop.services)
})
/**
* serves provider information by name from koop.services
*
* @param {object} req - incoming request
* @param {object} res - outgoing response
*/
koop.get('/providers/:provider', function (req, res) {
res.json(koop.services[req.params.provider])
})
/**
* gets all the datasets in the cache for a provider
*
* @param {object} req - incoming request
* @param {object} res - outgoing response
*/
koop.get('/providers/:provider/datasets', function (req, res) {
var sqlQuery = "select * from koopinfo where id ilike '%" + req.params.provider + "%'"
koop.Cache.db._query(sqlQuery, function (err, result) {
if (err) return res.status(500).send(err)
res.json(result.rows)
})
})
/**
* export worker setup
*/
if (koop.config.export_workers) {
var workerConfig = koop.config.export_workers
workerConfig.log = koop.log
koop.Exporter.export_q = koop.ExportQueue.create(workerConfig)
}
/**
* returns information about export workers
*
* @param {object} req - incoming request
* @param {object} res - outgoing response
*/
koop.get('/export-workers', function (req, res) {
if (!koop.config.export_workers) return res.status(400).json({error: 'Export workers are not enabled. Check your configuration.'})
var response = {}
var count = 0
var jobTypes = ['inactiveCount', 'activeCount', 'completeCount', 'failedCount', 'delayedCount']
var error
function collectQStats (q, json, type, callback) {
q[type](function (err, count) {
if (err) return callback(err)
json[type.replace('Count', '')] = count
callback(null, json)
})
}
// for (var type in jobTypes){
function getJobCounts (type) {
collectQStats(koop.Exporter.export_q, response, type, function (err, json) {
count++
if (err) {
error = err
}
// save the response
response = json
// get more if there are more types
if (jobTypes[count]) {
getJobCounts(jobTypes[count])
} else {
// return the response
if (error) {
res.status(500).send(err)
} else {
res.json(response)
}
}
})
}
getJobCounts(jobTypes[count])
})
koop.on('mount', function (parent) {
koop.log.info('Koop %s mounted at %s', koop.version, koop.mountpath)
})
return koop
}