Use express routers

This commit is contained in:
Daniel García Aubert 2018-03-27 18:46:54 +02:00
parent 7660046720
commit 5b9f608667
12 changed files with 75 additions and 97 deletions

View File

@ -17,11 +17,9 @@ function AnalysesController(pgConnection, authApi, userLimitsApi) {
module.exports = AnalysesController; module.exports = AnalysesController;
AnalysesController.prototype.register = function (app) { AnalysesController.prototype.register = function (mapRouter) {
const { base_url_mapconfig: mapconfigBasePath } = app; mapRouter.get(
`/analyses/catalog`,
app.get(
`${mapconfigBasePath}/analyses/catalog`,
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),

View File

@ -27,11 +27,9 @@ module.exports = class AnalysisController {
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
} }
register (app) { register (mapRouter) {
const { base_url_mapconfig: mapConfigBasePath } = app; mapRouter.get(
`/:token/analysis/node/:nodeId`,
app.get(
`${mapConfigBasePath}/:token/analysis/node/:nodeId`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),

View File

@ -31,11 +31,9 @@ module.exports = class AttributesController {
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
} }
register (app) { register (mapRouter) {
const { base_url_mapconfig: mapConfigBasePath } = app; mapRouter.get(
`/:token/:layer/attributes/:fid`,
app.get(
`${mapConfigBasePath}/:token/:layer/attributes/:fid`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),

View File

@ -46,14 +46,12 @@ module.exports = class DataviewController {
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
} }
register (app) { register (mapRouter) {
const { base_url_mapconfig: mapConfigBasePath } = app;
// Undocumented/non-supported API endpoint methods. // Undocumented/non-supported API endpoint methods.
// Use at your own peril. // Use at your own peril.
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/dataview/:dataviewName`, `/:token/dataview/:dataviewName`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -74,8 +72,8 @@ module.exports = class DataviewController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/:layer/widget/:dataviewName`, `/:token/:layer/widget/:dataviewName`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -96,8 +94,8 @@ module.exports = class DataviewController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/dataview/:dataviewName/search`, `/:token/dataview/:dataviewName/search`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -118,8 +116,8 @@ module.exports = class DataviewController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/:layer/widget/:dataviewName/search`, `/:token/:layer/widget/:dataviewName/search`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),

View File

@ -109,6 +109,4 @@ LayergroupController.prototype.register = function(app) {
); );
analysisController.register(app); analysisController.register(app);
}; };

View File

@ -31,13 +31,11 @@ module.exports = class StaticController {
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
} }
register (app) { register (mapRouter) {
const { base_url_mapconfig: mapConfigBasePath } = app;
const forcedFormat = 'png'; const forcedFormat = 'png';
app.get( mapRouter.get(
`${mapConfigBasePath}/static/center/:token/:z/:lat/:lng/:width/:height.:format`, `/static/center/:token/:z/:lat/:lng/:width/:height.:format`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -59,8 +57,8 @@ module.exports = class StaticController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`, `/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),

View File

@ -41,11 +41,9 @@ module.exports = class TileController {
this.surrogateKeysCache = surrogateKeysCache; this.surrogateKeysCache = surrogateKeysCache;
} }
register (app) { register (mapRouter) {
const { base_url_mapconfig: mapConfigBasePath } = app; mapRouter.get(
`/:token/:z/:x/:y@:scale_factor?x.:format`,
app.get(
`${mapConfigBasePath}/:token/:z/:x/:y@:scale_factor?x.:format`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -70,8 +68,8 @@ module.exports = class TileController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/:z/:x/:y.:format`, `/:token/:z/:x/:y.:format`,
layergroupToken(), layergroupToken(),
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
@ -96,8 +94,8 @@ module.exports = class TileController {
sendResponse() sendResponse()
); );
app.get( mapRouter.get(
`${mapConfigBasePath}/:token/:layer/:z/:x/:y.(:format)`, `/:token/:layer/:z/:x/:y.(:format)`,
distinguishLayergroupFromStaticRoute(), distinguishLayergroupFromStaticRoute(),
layergroupToken(), layergroupToken(),
credentials(), credentials(),

View File

@ -62,32 +62,22 @@ function MapController (
module.exports = MapController; module.exports = MapController;
MapController.prototype.register = function(app) { MapController.prototype.register = function(mapRouter, templateRouter) {
const { base_url_mapconfig: mapConfigBasePath, base_url_templated: templateBasePath } = app; mapRouter.get(`/`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS));
mapRouter.post(`/`, this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS));
app.get( mapRouter.options(`/`, cors('Content-Type'));
`${mapConfigBasePath}`,
this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS)
);
app.post(
`${mapConfigBasePath}`,
this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.ANONYMOUS)
);
const useTemplate = true; const useTemplate = true;
app.get( templateRouter.get(
`${templateBasePath}/:template_id/jsonp`, `/:template_id/jsonp`,
this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate) this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate)
); );
app.post( templateRouter.post(
`${templateBasePath}/:template_id`, `/:template_id`,
this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate) this.composeCreateMapMiddleware(RATE_LIMIT_ENDPOINTS_GROUPS.NAMED, useTemplate)
); );
app.options(`${mapConfigBasePath}`, cors('Content-Type'));
}; };
MapController.prototype.composeCreateMapMiddleware = function (endpointGroup, useTemplate = false) { MapController.prototype.composeCreateMapMiddleware = function (endpointGroup, useTemplate = false) {

View File

@ -47,11 +47,10 @@ function NamedMapsController (
module.exports = NamedMapsController; module.exports = NamedMapsController;
NamedMapsController.prototype.register = function(app) { NamedMapsController.prototype.register = function(mapRouter, templateRouter) {
const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app;
app.get( templateRouter.get(
`${templateBasePath}/:template_id/:layer/:z/:x/:y.(:format)`, `/:template_id/:layer/:z/:x/:y.(:format)`,
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),
@ -74,8 +73,8 @@ NamedMapsController.prototype.register = function(app) {
vectorError() vectorError()
); );
app.get( mapRouter.get(
`${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`, `/static/named/:template_id/:width/:height.:format`,
credentials(), credentials(),
authorize(this.authApi), authorize(this.authApi),
dbConnSetup(this.pgConnection), dbConnSetup(this.pgConnection),

View File

@ -19,11 +19,9 @@ function NamedMapsAdminController(authApi, templateMaps, userLimitsApi) {
module.exports = NamedMapsAdminController; module.exports = NamedMapsAdminController;
NamedMapsAdminController.prototype.register = function (app) { NamedMapsAdminController.prototype.register = function (templateRouter) {
const { base_url_templated: templateBasePath } = app; templateRouter.post(
`/`,
app.post(
`${templateBasePath}/`,
credentials(), credentials(),
checkContentType({ action: 'POST', label: 'POST TEMPLATE' }), checkContentType({ action: 'POST', label: 'POST TEMPLATE' }),
authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'create', label: 'POST TEMPLATE' }),
@ -32,8 +30,8 @@ NamedMapsAdminController.prototype.register = function (app) {
sendResponse() sendResponse()
); );
app.put( templateRouter.put(
`${templateBasePath}/:template_id`, `/:template_id`,
credentials(), credentials(),
checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }), checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }),
authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'update', label: 'PUT TEMPLATE' }),
@ -42,8 +40,8 @@ NamedMapsAdminController.prototype.register = function (app) {
sendResponse() sendResponse()
); );
app.get( templateRouter.get(
`${templateBasePath}/:template_id`, `/:template_id`,
credentials(), credentials(),
authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'get', label: 'GET TEMPLATE' }),
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET),
@ -51,8 +49,8 @@ NamedMapsAdminController.prototype.register = function (app) {
sendResponse() sendResponse()
); );
app.delete( templateRouter.delete(
`${templateBasePath}/:template_id`, `/:template_id`,
credentials(), credentials(),
authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }), authorizedByAPIKey({ authApi: this.authApi, action: 'delete', label: 'DELETE TEMPLATE' }),
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE),
@ -60,8 +58,8 @@ NamedMapsAdminController.prototype.register = function (app) {
sendResponse() sendResponse()
); );
app.get( templateRouter.get(
`${templateBasePath}/`, `/`,
credentials(), credentials(),
authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }), authorizedByAPIKey({ authApi: this.authApi, action: 'list', label: 'GET TEMPLATE LIST' }),
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST), rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST),
@ -69,10 +67,7 @@ NamedMapsAdminController.prototype.register = function (app) {
sendResponse() sendResponse()
); );
app.options( templateRouter.options(`/:template_id`, cors('Content-Type'));
`${templateBasePath}/:template_id`,
cors('Content-Type')
);
}; };
function checkContentType ({ action, label }) { function checkContentType ({ action, label }) {

View File

@ -12,10 +12,10 @@ function ServerInfoController(versions) {
module.exports = ServerInfoController; module.exports = ServerInfoController;
ServerInfoController.prototype.register = function(app) { ServerInfoController.prototype.register = function(monitorRouter) {
app.get('/health', this.health.bind(this)); monitorRouter.get('/health', this.health.bind(this));
app.get('/', this.welcome.bind(this)); monitorRouter.get('/', this.welcome.bind(this));
app.get('/version', this.version.bind(this)); monitorRouter.get('/version', this.version.bind(this));
}; };
ServerInfoController.prototype.welcome = function(req, res) { ServerInfoController.prototype.welcome = function(req, res) {

View File

@ -116,8 +116,6 @@ module.exports = function(serverOptions) {
// initialize express server // initialize express server
var app = bootstrap(serverOptions); var app = bootstrap(serverOptions);
// Extend windshaft with all the elements of the options object
_.extend(app, serverOptions);
var mapStore = new windshaft.storage.MapStore({ var mapStore = new windshaft.storage.MapStore({
pool: redisPool, pool: redisPool,
@ -222,6 +220,12 @@ module.exports = function(serverOptions) {
* Routing * Routing
******************************************************************************************************************/ ******************************************************************************************************************/
const mapRouter = express.Router();
const templateRouter = express.Router();
const monitorRouter = express.Router();
const { base_url_mapconfig: mapConfigBasePath, base_url_templated: templateBasePath } = serverOptions;
new controller.Layergroup( new controller.Layergroup(
pgConnection, pgConnection,
mapStore, mapStore,
@ -233,7 +237,7 @@ module.exports = function(serverOptions) {
layergroupAffectedTablesCache, layergroupAffectedTablesCache,
analysisBackend, analysisBackend,
authApi authApi
).register(app); ).register(mapRouter);
new controller.Map( new controller.Map(
pgConnection, pgConnection,
@ -246,7 +250,7 @@ module.exports = function(serverOptions) {
mapConfigAdapter, mapConfigAdapter,
statsBackend, statsBackend,
authApi authApi
).register(app); ).register(mapRouter, templateRouter);
new controller.NamedMaps( new controller.NamedMaps(
namedMapProviderCache, namedMapProviderCache,
@ -258,13 +262,17 @@ module.exports = function(serverOptions) {
pgConnection, pgConnection,
authApi, authApi,
userLimitsApi userLimitsApi
).register(app); ).register(mapRouter, templateRouter);
new controller.NamedMapsAdmin(authApi, templateMaps, userLimitsApi).register(app); new controller.NamedMapsAdmin(authApi, templateMaps, userLimitsApi).register(templateRouter);
new controller.Analyses(pgConnection, authApi, userLimitsApi).register(app); new controller.Analyses(pgConnection, authApi, userLimitsApi).register(mapRouter);
new controller.ServerInfo(versions).register(app); new controller.ServerInfo(versions).register(monitorRouter);
app.use(mapConfigBasePath, mapRouter);
app.use(templateBasePath, templateRouter);
app.use('/', monitorRouter);
/******************************************************************************************************************* /*******************************************************************************************************************
* END Routing * END Routing