Skip to content

Commit

Permalink
refactor: instantiate mosaic before registering fastify plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid committed Dec 20, 2023
1 parent fea910c commit ae7b8f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/cli/src/plugins/createMosaicInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MosaicConfig } from '@jpmorganchase/mosaic-types';
import MosaicCore from '@jpmorganchase/mosaic-core';

export async function createMosaicInstance(config: MosaicConfig) {
const mosaic = new MosaicCore(config);
await mosaic.start();
return mosaic;
}
5 changes: 2 additions & 3 deletions packages/cli/src/plugins/mosaicFastifyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'node:path';
export interface FastifyMosaicPluginOptions {
config: MosaicConfig;
scope?: string[];
mosaic: MosaicCore;
}

export interface FastifyMosaic {
Expand All @@ -16,9 +17,7 @@ export interface FastifyMosaic {
}

async function fastifyMosaic(fastify: FastifyInstance, options: FastifyMosaicPluginOptions) {
const { config, scope } = options;
const mosaic = new MosaicCore(config);
await mosaic.start();
const { config, scope, mosaic } = options;
const fs = Array.isArray(scope) ? mosaic.filesystem.scope(scope) : mosaic.filesystem;
fastify.decorate('mosaic', { config, core: mosaic, fs });

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import middie from '@fastify/middie';
import fastifyMosaic from './plugins/mosaicFastifyPlugin.js';
import fastifyMosaicAdmin from './plugins/mosaicAdminPlugin.js';
import fastifyMosaicWorkflows from './plugins/mosaicWorkflowsPlugin.js';
import { createMosaicInstance } from './plugins/createMosaicInstance.js';

const MOSAIC_ADMIN_PREFIX = '_mosaic_';

Expand All @@ -15,8 +16,9 @@ export const server = Fastify({
});

export default async function serve(config: MosaicConfig, port: number, scope?: string[]) {
const mosaic = await createMosaicInstance(config);
await server.register(middie);
await server.register(fastifyMosaic, { config, scope });
await server.register(fastifyMosaic, { config, scope, mosaic });
await server.register(fastifyMosaicWorkflows);
await server.register(fastifyMosaicAdmin, {
prefix: MOSAIC_ADMIN_PREFIX,
Expand Down

1 comment on commit ae7b8f0

@vercel
Copy link

@vercel vercel bot commented on ae7b8f0 Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mosaic – ./

mosaic-mosaic-dev-team.vercel.app
mosaic-git-main-mosaic-dev-team.vercel.app

Please sign in to comment.