-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
166 lines (151 loc) · 5.08 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
/* @flow */
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const compression = require('compression')
const pkg = require('./package.json')
const Cache = require('@koopjs/cache-memory')
const Logger = require('@koopjs/logger')
const datasetsProvider = require('./lib/datasets')
const ProviderRegistration = require('./lib/provider-registration')
const middleware = require('./lib/middleware')
const Events = require('events')
const Util = require('util')
const path = require('path')
const geoservices = require('@koopjs/output-geoservices')
const LocalFS = require('koop-localfs')
function Koop (config) {
this.version = pkg.version
this.config = config || require('config')
this.server = initServer(this.config)
// default to LocalDB cache
// cache registration overrides this
this.cache = new Cache()
this.log = new Logger(this.config)
this.providers = []
this.pluginRoutes = []
this.outputs = []
this.register(geoservices)
this.register(LocalFS)
this.register(datasetsProvider)
this.server
.on('mount', () => {
this.log.info(`Koop ${this.version} mounted at ${this.server.mountpath}`)
})
.get('/status', (req, res) => res.json(this.status))
}
Util.inherits(Koop, Events)
/**
* express middleware setup
*/
function initServer (config) {
const app = express()
// parse application/json
.use(bodyParser.json({ limit: '10000kb' }))
// parse application/x-www-form-urlencoded
.use(bodyParser.urlencoded({ extended: false }))
.disable('x-powered-by')
// TODO this should just live inside featureserver
.use(middleware.copyBodyToQuery)
.use(middleware.paramTrim)
.use(middleware.paramParse)
.use(middleware.paramCoerce)
// for demos and preview maps in providers
.set('view engine', 'ejs')
.use(express.static(path.join(__dirname, '/public')))
// Use CORS unless explicitly disable in the config
if (!config.disableCors) app.use(cors())
// Use compression unless explicitly disable in the config
if (!config.disableCompression) app.use(compression())
return app
}
Koop.prototype.register = function (plugin, options) {
if (typeof plugin === 'undefined') throw new Error('Plugin undefined.')
switch (plugin.type) {
case 'provider':
return this._registerProvider(plugin, options)
case 'cache':
return this._registerCache(plugin, options)
case 'plugin':
return this._registerPlugin(plugin, options)
case 'filesystem':
return this._registerFilesystem(plugin, options)
case 'output':
return this._registerOutput(plugin, options)
case 'auth':
return this._registerAuth(plugin, options)
default:
this.log.warn('Unrecognized plugin type: "' + plugin.type + '". Defaulting to provider.')
return this._registerProvider(plugin, options)
}
}
/**
* Store an Authentication plugin on the koop instance for use during provider registration.
* @param {object} auth
*/
Koop.prototype._registerAuth = function (auth) {
this._authModule = auth
}
/**
* registers a provider
* exposes the provider's routes, controller, and model
*
* @param {object} provider - the provider to be registered
*/
Koop.prototype._registerProvider = function (provider, options = {}) {
const providerRegistration = ProviderRegistration.create({ koop: this, provider, options })
this.providers.push(providerRegistration)
this.log.info('registered provider:', providerRegistration.namespace, providerRegistration.version)
}
/**
* registers a provider
* exposes the provider's routes, controller, and model
*
* @param {object} output - the output plugin to be registered
*/
Koop.prototype._registerOutput = function (Output) {
this.outputs.push(Output)
this.log.info('registered output:', Output.name, Output.version)
}
/**
* registers a cache
* overwrites any existing koop.Cache.db
*
* @param {object} cache - a koop database adapter
*/
Koop.prototype._registerCache = function (Cache, options) {
this.cache = new Cache(options)
this.log.info('registered cache:', Cache.name, Cache.version)
}
/**
* registers a filesystem
* overwrites the default filesystem
*
* @param {object} filesystem - a koop filesystem adapter
*/
Koop.prototype._registerFilesystem = function (Filesystem) {
this.fs = new Filesystem()
this.log.info('registered filesystem:', Filesystem.pluginName || Filesystem.plugin_name || Filesystem.name, Filesystem.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.prototype._registerPlugin = function (Plugin) {
const name = Plugin.pluginName || Plugin.plugin_name || Plugin.name
if (!name) throw new Error('Plugin is missing name')
let dependencies
if (Array.isArray(Plugin.dependencies) && Plugin.dependencies.length) {
dependencies = Plugin.dependencies.reduce((deps, dep) => {
deps[dep] = this[dep]
return deps
}, {})
}
this[name] = new Plugin(dependencies)
this.log.info('registered plugin:', name, Plugin.version)
}
module.exports = Koop