434a0435fd
no issue - optimised only for web/ folder, because it has used very general namespaces - the debug namespace must be specific, otherwise i run `DEBUG=ghost:api:*` and i get web debug logs and api folder debug logs - we can come up with a new namespace system, but for now it must be explicit enough
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
// # API routes
|
|
const debug = require('ghost-ignition').debug('web:api:v0.1:app');
|
|
const boolParser = require('express-query-boolean');
|
|
const express = require('express');
|
|
|
|
// routes
|
|
const routes = require('./routes');
|
|
|
|
// Include the middleware
|
|
|
|
// API specific
|
|
const versionMatch = require('../../shared/middlewares/api/version-match'); // global
|
|
|
|
// Shared
|
|
const bodyParser = require('body-parser'); // global, shared
|
|
const cacheControl = require('../../shared/middlewares/cache-control'); // global, shared
|
|
const maintenance = require('../../shared/middlewares/maintenance'); // global, shared
|
|
const errorHandler = require('../../shared/middlewares/error-handler'); // global, shared
|
|
|
|
module.exports = function setupApiApp() {
|
|
debug('API v0.1 setup start');
|
|
const apiApp = express();
|
|
|
|
// @TODO finish refactoring this away.
|
|
apiApp.use(function setIsAdmin(req, res, next) {
|
|
// api === isAdmin
|
|
res.isAdmin = true;
|
|
next();
|
|
});
|
|
|
|
// API middleware
|
|
|
|
// Body parsing
|
|
apiApp.use(bodyParser.json({limit: '1mb'}));
|
|
apiApp.use(bodyParser.urlencoded({extended: true, limit: '1mb'}));
|
|
|
|
// Query parsing
|
|
apiApp.use(boolParser());
|
|
|
|
// send 503 json response in case of maintenance
|
|
apiApp.use(maintenance);
|
|
|
|
// Check version matches for API requests, depends on res.locals.safeVersion being set
|
|
// Therefore must come after themeHandler.ghostLocals, for now
|
|
apiApp.use(versionMatch);
|
|
|
|
// API shouldn't be cached
|
|
apiApp.use(cacheControl('private'));
|
|
|
|
// Routing
|
|
apiApp.use(routes());
|
|
|
|
// API error handling
|
|
apiApp.use(errorHandler.resourceNotFound);
|
|
apiApp.use(errorHandler.handleJSONResponse);
|
|
|
|
debug('API v0.1 setup end');
|
|
|
|
return apiApp;
|
|
};
|