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 7261d72
Show file tree
Hide file tree
Showing 3 changed files with 61 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
30 changes: 30 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ 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'),

/**
* The URL/USERNAME/PASSWORD of the Bitcoin JSON-RPC server.
* The JSON-RPC server is used to query the Bitcoin blockchain.
*/
BITCOIN_JSON_RPC_URL: z.string(),
BITCOIN_JSON_RPC_USERNAME: z.string(),
BITCOIN_JSON_RPC_PASSWORD: z.string(),
/**
* The URL of the Electrs API.
* Electrs is a Rust implementation of Electrum Server.
* It is used to query the Bitcoin blockchain (balance, transactions, etc).
*/
BITCOIN_ELECTRS_API_URL: z.string(),

/**
* Bitcoin SPV service URL
* https://github.com/ckb-cell/ckb-bitcoin-spv-service
Expand Down Expand Up @@ -113,6 +135,14 @@ const envSchema = z
*/
PAYMASTER_BTC_CONTAINER_FEE_SATS: z.coerce.number().default(7000),

/**
* Enable BTCTimeLock cell unlock cron task
* set to false to disable the BTCTimeLock cell unlock cron task
*/
UNLOCKER_CRON_TASK_ENABLE: z
.enum(['true', 'false'])
.default('true')
.transform((value) => value === 'true'),
/**
* BTCTimeLock cell unlock batch size
*/
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 7261d72

Please sign in to comment.