Ghost/core/server/web/api/v0.1/app.js
Nazar Gargol 57271127f4 Added v2 api endpoints (#9874)
refs #9866

- Registered Content API under /ghost/api/v2/content/
- Registered Admin API under /ghost/api/v2/admin/
- Moved API v0.1 implementation to web/api/v0.1
- Created web/api/v2 for the new api endpoints
- Started with reducing the implementation for the new Content API (the Content api does not serve admin api endpoints, that's why it was reducible)
- Covered parent-app module with basic test checking correct applications/routes are being mounted
- Added a readme file, which contains a warning using v2, because it's under active development!
- This PR does only make the new endpoints available, we have not:
  - optimised the web folder (e.g. res.isAdmin)
  - started with different API controllers
  - reason: we want to do more preparation tasks before we copy the api controllers
2018-09-18 15:59:06 +02:00

61 lines
1.7 KiB
JavaScript

// # API routes
const debug = require('ghost-ignition').debug('api'),
boolParser = require('express-query-boolean'),
express = require('express'),
// routes
routes = require('./routes'),
// Include the middleware
// API specific
versionMatch = require('../../middleware/api/version-match'), // global
// Shared
bodyParser = require('body-parser'), // global, shared
cacheControl = require('../../middleware/cache-control'), // global, shared
maintenance = require('../../middleware/maintenance'), // global, shared
errorHandler = require('../../middleware/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;
};