This repository has been archived by the owner on Feb 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
133 lines (108 loc) · 3.83 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
var express = require("express"),
bodyParser = require('body-parser'),
fs = require('fs'),
pjson = require('./package.json'),
_ = require("lodash"),
child = require('child_process').fork,
koop = require('./lib');
module.exports = function( config ) {
var app = express(), route, controller, model;
// keep track of the registered services
app.services = [];
koop.config = config;
// handle POST requests
app.use(bodyParser());
// init the koop log based on config params
koop.log = new koop.Logger( config );
//request parameters can come from query url or POST body
app.use(function(req, res, next) {
req.query=_.extend(req.query || {}, req.body || {});
next();
});
// log every request
//app.use(function(req, res, next) {
//koop.log.debug("%s %s", req.method, req.url);
//next();
//});
// store the sha so we know what version of koop this is
app.status = {
version: pjson.version,
//sha: fs.readFileSync( __dirname + '/.git/refs/heads/master' ).toString(),
providers: {}
};
app.set('view engine', 'ejs');
// serve the index
app.get("/", function(req, res, next) {
res.render(__dirname + '/views/index');
});
// serve the index
app.get("/providers", function(req, res, next) {
res.json(app.services);
});
// register providers into the app
// sets up models, routes -> controllers/handlers
app.register = function(provider){
// only register if the provider has a name
if ( provider.name ) {
app.services.push( { type:'FeatureServer', name: provider.name.toLowerCase() });
// save the provider onto the app
model = new provider.model( koop );
// pass the model to the controller
controller = new provider.controller( model );
// if a provider has a status object store it
if ( provider.status ) {
app.status.providers[provider.name] = provider.status;
}
// binds a series of standard routes
if ( provider.name && provider.pattern ) {
app._bindDefaultRoutes(provider.name, provider.pattern, controller );
}
// add each route, the routes let us override defaults etc.
app._bindRoutes( provider.routes, controller );
}
};
var defaultRoutes = {
'featureserver': ['/FeatureServer/:layer/:method', '/FeatureServer/:layer', '/FeatureServer'],
'preview':['/preview'],
'drop':['/drop']
};
// assigns a series of default routes; assumes
app._bindDefaultRoutes = function( name, pattern, controller ){
var routes, handler;
for ( handler in defaultRoutes ){
if ( controller[ handler ] ){
defaultRoutes[ handler ].forEach(function(route){
app[ 'get' ]( '/'+ name + pattern + route, controller[ handler ]);
});
}
}
};
// bind each route in a list to controller handler
app._bindRoutes = function( routes, controller ){
for ( route in routes ){
var path = route.split(' ');
app[ path[0] ]( path[1], controller[ routes[ route ] ]);
}
};
// init koop centralized file access
// this allows us to turn the FS access off globally
koop.files = new koop.Files( koop );
koop.tiles = new koop.Tiles( koop );
koop.thumbnail = new koop.Thumbnail( koop );
// Need the exporter to have access to the cache so we pass it Koop
koop.exporter = new koop.Exporter( koop );
koop.Cache = new koop.DataCache( koop );
// Start the Cache DB with the conn string from config
if ( config && config.db ) {
if ( config.db.postgis ) {
koop.Cache.db = koop.PostGIS.connect( config.db.postgis.conn );
} else if ( config && config.db.sqlite ) {
koop.Cache.db = koop.SQLite.connect( config.db.sqlite );
}
koop.Cache.db.log = koop.log;
} else if (config && !config.db){
console.log('Exiting since no DB configuration found in config');
process.exit();
}
return app;
};