Ghost/core/server/web/members/app.js
Hannah Wolfe 53d14fd8e3 Added Router etc to shared/express + use everywhere
- Added a wrapper around express.Router to our shared/express util
- Also export static and _express
- Use this shared util everywhre, meaning express is only used directly in this one file
- ATM this file is mostly an experiment / debug helper, it might be removed again later
- The aim is to have a minimal framework wrapping express that allows us to:
     - reduce our usage of express() in favour of Router()
     - unify some of our duplicated logic
     - fix some structural issues e.g. Sentry
     - make it easier to understand the codebase
2020-05-01 19:32:57 +01:00

49 lines
2.2 KiB
JavaScript

const debug = require('ghost-ignition').debug('web:members:app');
const {URL} = require('url');
const cors = require('cors');
const express = require('../../../shared/express');
const urlUtils = require('../../lib/url-utils');
const membersService = require('../../services/members');
const middleware = membersService.middleware;
const shared = require('../shared');
module.exports = function setupMembersApp() {
debug('Members App setup start');
const membersApp = express('members');
// Entire app is behind labs flag
membersApp.use(shared.middlewares.labs.members);
// 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
membersApp.get('/api/member', middleware.getMemberData);
membersApp.get('/api/session', middleware.getIdentityToken);
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', (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));
// API error handling
membersApp.use(shared.middlewares.errorHandler.resourceNotFound);
membersApp.use(shared.middlewares.errorHandler.handleJSONResponseV2);
debug('Members App setup end');
return membersApp;
};