mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
f417c4c732
- Reduced our maintenance middleware code down to the bare minimum!
- We have an old maintenance middleware in place to handle when a site is forcibly put into maintenance mode, or the urlService hasn't finished booting
- This maintenance middleware was mounted on every sub app, instead of globally for reasons I no longer remember
- Recently, we introduced a new, static version of maintenence middleware to show during the boot process so we can get the server started earlier & not drop requests
- This version has its own HTML template and doesn't depend on any of Ghost's error rendering code
- To simplify and help with decoupling, this commit merges the two middleware, so that the new independent & static middleware renders its template for any one of the 3 possible maintenance modes
- It only needs to exist in the top level app 🙌
TODO: move the maintenance middleware to its own file/package so it's not part of the app.js as that is weird
60 lines
3.1 KiB
JavaScript
60 lines
3.1 KiB
JavaScript
const debug = require('@tryghost/debug')('members');
|
|
const {URL} = require('url');
|
|
const cors = require('cors');
|
|
const bodyParser = require('body-parser');
|
|
const express = require('../../../shared/express');
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
const membersService = require('../../services/members');
|
|
const middleware = membersService.middleware;
|
|
const shared = require('../shared');
|
|
const labs = require('../../../shared/labs');
|
|
|
|
module.exports = function setupMembersApp() {
|
|
debug('Members App setup start');
|
|
const membersApp = express('members');
|
|
|
|
// Members API shouldn't be cached
|
|
membersApp.use(shared.middleware.cacheControl('private'));
|
|
|
|
// Support CORS for requests from the frontend
|
|
const siteUrl = new URL(urlUtils.getSiteUrl());
|
|
membersApp.use(cors(siteUrl.origin));
|
|
|
|
// Currently global handling for signing in with ?token= magiclinks
|
|
membersApp.use(middleware.createSessionFromMagicLink);
|
|
|
|
// Routing
|
|
|
|
// Webhooks
|
|
membersApp.post('/webhooks/stripe', middleware.stripeWebhooks);
|
|
|
|
// Initializes members specific routes as well as assigns members specific data to the req/res objects
|
|
// We don't want to add global bodyParser middleware as that interfers with stripe webhook requests on - `/webhooks`.
|
|
membersApp.get('/api/member', middleware.getMemberData);
|
|
membersApp.put('/api/member', bodyParser.json({limit: '1mb'}), middleware.updateMemberData);
|
|
membersApp.post('/api/member/email', bodyParser.json({limit: '1mb'}), (req, res) => membersService.api.middleware.updateEmailAddress(req, res));
|
|
membersApp.get('/api/session', middleware.getIdentityToken);
|
|
membersApp.get('/api/offers/:id', middleware.getOfferData);
|
|
membersApp.delete('/api/session', middleware.deleteSession);
|
|
membersApp.get('/api/site', middleware.getMemberSiteData);
|
|
|
|
// NOTE: this is wrapped in a function to ensure we always go via the getter
|
|
membersApp.post('/api/send-magic-link', bodyParser.json(), shared.middleware.brute.membersAuth, (req, res, next) => membersService.api.middleware.sendMagicLink(req, res, next));
|
|
membersApp.post('/api/create-stripe-checkout-session', (req, res, next) => membersService.api.middleware.createCheckoutSession(req, res, next));
|
|
membersApp.post('/api/create-stripe-update-session', (req, res, next) => membersService.api.middleware.createCheckoutSetupSession(req, res, next));
|
|
membersApp.put('/api/subscriptions/:id', (req, res, next) => membersService.api.middleware.updateSubscription(req, res, next));
|
|
membersApp.post('/api/events', labs.enabledMiddleware('membersActivity'), middleware.loadMemberSession, (req, res, next) => membersService.api.middleware.createEvents(req, res, next));
|
|
|
|
// API error handling
|
|
membersApp.use('/api', shared.middleware.errorHandler.resourceNotFound);
|
|
membersApp.use('/api', shared.middleware.errorHandler.handleJSONResponseV2);
|
|
|
|
// Webhook error handling
|
|
membersApp.use('/webhooks', shared.middleware.errorHandler.resourceNotFound);
|
|
membersApp.use('/webhooks', shared.middleware.errorHandler.handleJSONResponseV2);
|
|
|
|
debug('Members App setup end');
|
|
|
|
return membersApp;
|
|
};
|