From 7261d72f2eb64a767c231ad001c86d0eb3f3a850 Mon Sep 17 00:00:00 2001 From: ahonn Date: Mon, 29 Apr 2024 15:46:33 +1000 Subject: [PATCH] feat: add APP_MODE env var to support api/background mode only --- src/app.ts | 52 +++++++++++++++++++++++++++++--------------------- src/env.ts | 30 +++++++++++++++++++++++++++++ src/options.ts | 2 +- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/app.ts b/src/app.ts index 3eb82234..f47f482a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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'); + } } } diff --git a/src/env.ts b/src/env.ts index 1f83e434..3e86f7c2 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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 @@ -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 */ diff --git a/src/options.ts b/src/options.ts index b015c2a7..a5639988 100644 --- a/src/options.ts +++ b/src/options.ts @@ -16,7 +16,7 @@ const envToLogger = { }, } : {}), - level: env.LOGGER_LEVEL ?? 'debug', + level: 'debug', }, production: { level: env.LOGGER_LEVEL ?? 'info',