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;
AnalysesController.prototype.register = function (app) {
const { base_url_mapconfig: mapconfigBasePath } = app;
app.get(
`${mapconfigBasePath}/analyses/catalog`,
AnalysesController.prototype.register = function (mapRouter) {
mapRouter.get(
`/analyses/catalog`,
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -116,8 +116,6 @@ module.exports = function(serverOptions) {
// initialize express server
var app = bootstrap(serverOptions);
// Extend windshaft with all the elements of the options object
_.extend(app, serverOptions);
var mapStore = new windshaft.storage.MapStore({
pool: redisPool,
@ -222,6 +220,12 @@ module.exports = function(serverOptions) {
* 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(
pgConnection,
mapStore,
@ -233,7 +237,7 @@ module.exports = function(serverOptions) {
layergroupAffectedTablesCache,
analysisBackend,
authApi
).register(app);
).register(mapRouter);
new controller.Map(
pgConnection,
@ -246,7 +250,7 @@ module.exports = function(serverOptions) {
mapConfigAdapter,
statsBackend,
authApi
).register(app);
).register(mapRouter, templateRouter);
new controller.NamedMaps(
namedMapProviderCache,
@ -258,13 +262,17 @@ module.exports = function(serverOptions) {
pgConnection,
authApi,
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