-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathindex.js
160 lines (143 loc) · 4.87 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
const Events = require('events');
const Util = require('util');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const compression = require('compression');
const Cache = require('@koopjs/cache-memory');
const Logger = require('@koopjs/logger');
const pkg = require('../package.json');
const ProviderRegistration = require('./provider-registration');
const geoservices = require('@koopjs/output-geoservices');
function Koop (options) {
this.version = pkg.version;
// TODO: remove usage of "config" module
this.config = options || require('config');
this.server = initServer(this.config);
// default to in-memory cache; another cache registration overrides this
this.cache = new Cache();
this.log = this.config.logger || new Logger(this.config);
this.providers = [];
this.pluginRoutes = [];
this.outputs = [];
this.register(geoservices, { logger: this.log });
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 (options) {
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')
// for demos and preview maps in providers
.set('view engine', 'ejs')
.use(express.static(path.join(__dirname, '/public')));
// Use CORS unless explicitly disabled in the config
if (!options.disableCors) {
app.use(cors());
}
// Use compression unless explicitly disable in the config
if (!options.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} outputClass - the output plugin to be registered
*/
Koop.prototype._registerOutput = function (outputClass, options) {
this.outputs.push({ outputClass, options });
this.log.info('registered output:', outputClass.name, outputClass.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;