Skip to content

Commit

Permalink
Add fastify support to ticketing
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Jan 3, 2025
1 parent 34b2bfe commit a44494c
Show file tree
Hide file tree
Showing 16 changed files with 667 additions and 322 deletions.
14 changes: 9 additions & 5 deletions examples/kitchensink-express/src/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import { log } from '@unchainedshop/logger';
import '@unchainedshop/plugins/pricing/discount-half-price-manual.js';
import '@unchainedshop/plugins/pricing/discount-100-off.js';

import setupTicketing, { ticketingModules } from '@unchainedshop/ticketing';
import { TicketingAPI } from '@unchainedshop/ticketing';
import ticketingServices from '@unchainedshop/ticketing/lib/services.js';
import setupTicketing, {
TicketingAPI,
ticketingServices,
ticketingModules,
} from '@unchainedshop/ticketing';
import connectTicketingToExpress from '@unchainedshop/ticketing/lib/connect-express.js';

import seed from './seed.js';

Expand All @@ -33,7 +36,7 @@ app.use((req, res, next) => {
const httpServer = http.createServer(app);
const engine = await startPlatform({
modules: { ...defaultModules, ...ticketingModules },
services: { ...ticketingServices },
services: ticketingServices,
plugins: [
useResponseCache({
ttl: 0,
Expand Down Expand Up @@ -74,11 +77,12 @@ connect(app, engine);
connectDefaultPluginsToExpress(app, engine);

// Unchained Ticketing Extension
setupTicketing(app, engine.unchainedAPI as TicketingAPI, {
setupTicketing(engine.unchainedAPI as TicketingAPI, {
renderOrderPDF: console.log,
createAppleWalletPass: console.log,
createGoogleWalletPass: console.log,
});
connectTicketingToExpress(app);

const fileUrl = new URL(import.meta.resolve('../static/index.html'));
app.use('/', async (req, res) => {
Expand Down
12 changes: 7 additions & 5 deletions examples/kitchensink/src/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '@unchainedshop/plugins/pricing/discount-half-price-manual.js';
import '@unchainedshop/plugins/pricing/discount-100-off.js';

import setupTicketing, { ticketingModules, TicketingAPI } from '@unchainedshop/ticketing';
import connectTicketingToFastify from '@unchainedshop/ticketing/lib/connect-fastify.js';

import ticketingServices from '@unchainedshop/ticketing/lib/services.js';

Expand Down Expand Up @@ -92,11 +93,12 @@ connect(app, engine);
connectDefaultPluginsToFastify(app, engine);

// Unchained Ticketing Extension
// setupTicketing(app, engine.unchainedAPI as TicketingAPI, {
// renderOrderPDF: console.log,
// createAppleWalletPass: console.log,
// createGoogleWalletPass: console.log,
// });
setupTicketing(engine.unchainedAPI as TicketingAPI, {
renderOrderPDF: console.log,
createAppleWalletPass: console.log,
createGoogleWalletPass: console.log,
});
connectTicketingToFastify(app);

const fileUrl = new URL(import.meta.resolve('../static/index.html'));
app.route({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core-index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mongodb, MigrationRepository, ModuleInput } from '@unchainedshop/mongodb';
import initServices, { Services } from './services/index.js';
import initServices, { CustomServices, Services } from './services/index.js';
import initModules, { Modules, ModuleOptions } from './modules.js';

export * from './services/index.js';
Expand All @@ -19,7 +19,7 @@ export interface UnchainedCoreOptions {
configure: (params: ModuleInput<any>) => any;
}
>;
services?: Record<string, (this: Modules, ...args) => any>;
services?: CustomServices;
options?: ModuleOptions;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ function bindMethodsToModules(modules: Modules) {
}

export interface ServiceInterface {
(this: Modules, ...args: any[]): any;
(this: Modules, ...args: any[]): Promise<any> | any;
}

export type Bound<T extends ServiceInterface> = OmitThisParameter<T>;

export default function initServices(
modules: Modules,
customServices: Record<string, ServiceInterface> = {},
) {
export type CustomServices = Record<string, ServiceInterface | Record<string, ServiceInterface>>;

export default function initServices(modules: Modules, customServices: CustomServices = {}) {
const services = {
bookmarks: {
migrateBookmarks: migrateBookmarksService as Bound<typeof migrateBookmarksService>,
Expand Down
35 changes: 35 additions & 0 deletions packages/ticketing/src/connect-express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import express from 'express';
import appleWalletHandler from './mobile-tickets/apple-handler-express.js';
import googleWalletHandler from './mobile-tickets/google-handler-express.js';
import printTicketsHandler from './pdf-tickets/print-handler-express.js';

export default (app: express.Express) => {
const {
APPLE_WALLET_WEBSERVICE_PATH = '/rest/apple-wallet',
GOOGLE_WALLET_WEBSERVICE_PATH = '/rest/google-wallet',
UNCHAINED_PDF_PRINT_HANDLER_PATH = '/rest/print_tickets',
} = process.env;

app.use(
UNCHAINED_PDF_PRINT_HANDLER_PATH,
express.json({
type: 'application/json',
}),
printTicketsHandler,
);
app.use(
APPLE_WALLET_WEBSERVICE_PATH,
express.json({
type: 'application/json',
}),
appleWalletHandler,
);

app.use(
GOOGLE_WALLET_WEBSERVICE_PATH,
express.json({
type: 'application/json',
}),
googleWalletHandler,
);
};
30 changes: 30 additions & 0 deletions packages/ticketing/src/connect-fastify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { FastifyInstance } from 'fastify';
import appleWalletHandler from './mobile-tickets/apple-handler-fastify.js';
import googleWalletHandler from './mobile-tickets/google-handler-fastify.js';
import printTicketsHandler from './pdf-tickets/print-handler-fastify.js';

export default (app: FastifyInstance) => {
const {
APPLE_WALLET_WEBSERVICE_PATH = '/rest/apple-wallet',
GOOGLE_WALLET_WEBSERVICE_PATH = '/rest/google-wallet',
UNCHAINED_PDF_PRINT_HANDLER_PATH = '/rest/print_tickets',
} = process.env;

app.route({
url: `${APPLE_WALLET_WEBSERVICE_PATH}*`,
method: ['GET', 'POST', 'DELETE'],
handler: appleWalletHandler,
});

app.route({
url: `${GOOGLE_WALLET_WEBSERVICE_PATH}/*`,
method: 'POST',
handler: googleWalletHandler,
});

app.route({
url: `${UNCHAINED_PDF_PRINT_HANDLER_PATH}/*`,
method: 'POST',
handler: printTicketsHandler,
});
};
52 changes: 13 additions & 39 deletions packages/ticketing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,33 @@
import express from 'express';
import { subscribe } from '@unchainedshop/events';
import { RawPayloadType } from '@unchainedshop/events';
import { WorkerEventTypes, Work } from '@unchainedshop/core-worker';
import { RendererTypes, registerRenderer } from './template-registry.js';
import loadAppleWalletHandler from './mobile-tickets/apple-webservice.js';
import loadGoogleWalletHandler from './mobile-tickets/google-webservice.js';
import loadPDFHandler from './pdf-tickets/print-webservice.js';
import passes from './module.js';
import ticketing from './services.js';
import ticketingModules, { TicketingModule } from './module.js';

import { TicketingAPI } from './types.js';
import setupMagicKey from './magic-key.js';
import { TicketingServices } from './services.js';
import ticketingServices, { TicketingServices } from './services.js';

export type { TicketingAPI, RendererTypes };

export const ticketingModules = {
passes,
};
export { ticketingServices, ticketingModules, TicketingModule, TicketingServices };

export const ticketingServices: TicketingServices = {
ticketing,
};

export function setupPDFTickets(
app: express.Express,
{
renderOrderPDF,
}: {
renderOrderPDF: any;
},
) {
export function setupPDFTickets({ renderOrderPDF }: { renderOrderPDF: any }) {
registerRenderer(RendererTypes.ORDER_PDF, renderOrderPDF);
loadPDFHandler(app);
}

export function setupMobileTickets(
app: express.Express,
{
createGoogleWalletPass,
createAppleWalletPass,
}: {
createGoogleWalletPass: any;
createAppleWalletPass: any;
},
) {
export function setupMobileTickets({
createGoogleWalletPass,
createAppleWalletPass,
}: {
createGoogleWalletPass: any;
createAppleWalletPass: any;
}) {
registerRenderer(RendererTypes.GOOGLE_WALLET, createGoogleWalletPass);
registerRenderer(RendererTypes.APPLE_WALLET, createAppleWalletPass);

loadAppleWalletHandler(app);
loadGoogleWalletHandler(app);
}

export default function setupTicketing(
app: express.Express,
unchainedAPI: TicketingAPI,
{
renderOrderPDF,
Expand All @@ -65,10 +39,10 @@ export default function setupTicketing(
createGoogleWalletPass: any;
},
) {
setupPDFTickets(app, {
setupPDFTickets({
renderOrderPDF,
});
setupMobileTickets(app, {
setupMobileTickets({
createAppleWalletPass,
createGoogleWalletPass,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { createLogger } from '@unchainedshop/logger';
import express, { Request, Response } from 'express';
import type { Request, Response } from 'express';
import { TicketingAPI } from '../types.js';
import { getFileAdapter } from '@unchainedshop/core-files';

const logger = createLogger('unchained:apple-wallet-webservice');

const { APPLE_WALLET_WEBSERVICE_PATH = '/rest/apple-wallet' } = process.env;

const isAuthenticationTokenCorrect = (req, authenticationToken) => {
const expectedAuthorizationValue = `ApplePass ${authenticationToken}`;
return req.headers.authorization === expectedAuthorizationValue;
};

export const appleWalletHandler = async (
req: Request & { unchainedContext: TicketingAPI },
res: Response,
) => {
const appleWalletHandler = async (req: Request & { unchainedContext: TicketingAPI }, res: Response) => {
const resolvedContext = req.unchainedContext;
const { modules } = resolvedContext;
logger.info(`${req.path} (${JSON.stringify(req.query)})`);
Expand Down Expand Up @@ -239,12 +234,4 @@ export const appleWalletHandler = async (
res.end();
};

export default function loadAppleWalletHandler(app) {
app.use(
APPLE_WALLET_WEBSERVICE_PATH,
express.json({
type: 'application/json',
}),
appleWalletHandler,
);
}
export default appleWalletHandler;
Loading

0 comments on commit a44494c

Please sign in to comment.