Skip to content

Commit

Permalink
feat: add APP_MODE env var to support api/background mode only
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Apr 30, 2024
1 parent 288c920 commit 7c91240
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
52 changes: 30 additions & 22 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,44 @@ import cron from './plugins/cron';

async function routes(fastify: FastifyInstance) {
fastify.log.info(`Process env: ${JSON.stringify(getSafeEnvs(), null, 2)}`);
const env = container.resolve('env');

await fastify.register(cors);
await fastify.register(sentry);
fastify.register(sensible);
fastify.register(compress);
fastify.register(swagger);
fastify.register(jwt);
fastify.register(ipBlock);
fastify.register(cache);
fastify.register(rateLimit);
fastify.register(healthcheck);

const env = container.resolve('env');
await container.resolve('bitcoin').checkNetwork(env.NETWORK as NetworkType);

fastify.register(internalRoutes, { prefix: '/internal' });
fastify.register(tokenRoutes, { prefix: '/token' });
fastify.register(bitcoinRoutes, { prefix: '/bitcoin/v1' });
fastify.register(rgbppRoutes, { prefix: '/rgbpp/v1' });
fastify.log.info(`Application mode: ${env.APP_MODE}`);
if (['full', 'api'].includes(env.APP_MODE)) {
await fastify.register(cors);
fastify.register(swagger);
fastify.register(jwt);
fastify.register(ipBlock);
fastify.register(cache);
fastify.register(rateLimit);
fastify.register(healthcheck);

fastify.register(internalRoutes, { prefix: '/internal' });
fastify.register(tokenRoutes, { prefix: '/token' });
fastify.register(bitcoinRoutes, { prefix: '/bitcoin/v1' });
fastify.register(rgbppRoutes, { prefix: '/rgbpp/v1' });

fastify.log.info('Routes are registered');
}

// register cron routes only on Vercel
if (provider === 'vercel' || env.NODE_ENV === 'test') {
fastify.log.info('Cron routes is registered');
fastify.register(cronRoutes, { prefix: '/cron' });
} else {
fastify.log.info('Cron plugin is registered');
await fastify.register(cron);
fastify.addHook('onReady', () => {
fastify.cron.startAllJobs();
});
if (['full', 'background'].includes(env.APP_MODE)) {
// register cron routes only on Vercel
if (provider === 'vercel' || env.NODE_ENV === 'test') {
fastify.register(cronRoutes, { prefix: '/cron' });
fastify.log.info('Cron routes is registered');
} else {
await fastify.register(cron);
fastify.addHook('onReady', () => {
fastify.cron.startAllJobs();
});
fastify.log.info('Cron plugin is registered');
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ const envSchema = z
.transform((value) => value.split(','))
.pipe(z.string().array()),

/**
* Application Mode
* - full: Full application mode, includes API and background tasks.
* - api_only: API only mode, excludes background tasks.
* - background_only: Background tasks only mode, excludes API.
*/
APP_MODE: z.enum(['full', 'api', 'background']).default('full'),

/**
* Bitcoin SPV service URL
* https://github.com/ckb-cell/ckb-bitcoin-spv-service
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const envToLogger = {
},
}
: {}),
level: env.LOGGER_LEVEL ?? 'debug',
level: 'debug',
},
production: {
level: env.LOGGER_LEVEL ?? 'info',
Expand Down

0 comments on commit 7c91240

Please sign in to comment.