Skip to content

Commit

Permalink
feat: feature flags config
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk committed Oct 19, 2023
1 parent f2f48b8 commit 1a3e6bc
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 30 deletions.
3 changes: 1 addition & 2 deletions packages/api/src/address/address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import { LogService } from "../log/log.service";
import { ParseAddressPipe, ADDRESS_REGEX_PATTERN } from "../common/pipes/parseAddress.pipe";
import { TransferService } from "../transfer/transfer.service";
import { TransferDto } from "../transfer/transfer.dto";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "address";

@ApiTags("Address BFF")
Expand Down
5 changes: 2 additions & 3 deletions packages/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import { StatsModule } from "./stats/stats.module";
import { MetricsMiddleware } from "./middlewares/metrics.middleware";
import { metricProviders } from "./metrics";
import { DbMetricsService } from "./dbMetrics.service";
import { disableExternalAPI } from "./config/featureFlags";
import config from "./config";

// TMP: disable external API until release
const { disableExternalAPI } = config();

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, load: [config] }),
Expand All @@ -35,6 +33,7 @@ const { disableExternalAPI } = config();
}),
ApiModule,
ApiContractModule,
// TMP: disable external API until release
...(disableExternalAPI ? [] : [ApiBlockModule, ApiAccountModule, ApiTransactionModule, ApiLogModule]),
TokenModule,
AddressModule,
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/batch/batch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkRespons
import { BatchService } from "./batch.service";
import { BatchDto } from "./batch.dto";
import { BatchDetailsDto } from "./batchDetails.dto";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "batches";

@ApiTags("Batch BFF")
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/block/block.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkRespons
import { BlockService } from "./block.service";
import { BlockDto } from "./block.dto";
import { BlockDetailDto } from "./blockDetail.dto";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "blocks";

@ApiTags("Block BFF")
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/config/featureFlags.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as featureFlags from "./featureFlags.spec";

describe("featureFlags", () => {
it("sets default values", () => {
expect(featureFlags).toEqual({});
});
});
11 changes: 11 additions & 0 deletions packages/api/src/config/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { config } from "dotenv";
config();

const { DISABLE_API_SCHEMA_DOCS, DISABLE_BFF_API_SCHEMA_DOCS, DISABLE_EXTERNAL_API } = process.env;

export const swagger = {
enabled: DISABLE_API_SCHEMA_DOCS !== "true",
bffEnabled: DISABLE_BFF_API_SCHEMA_DOCS !== "true",
};

export const disableExternalAPI = DISABLE_EXTERNAL_API === "true";
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import config from "./config";
import config from "../config";

jest.mock("./featureFlags", () => ({
feature1Enabled: true,
feature2Enabled: false,
}));

describe("config", () => {
const env = process.env;
Expand Down Expand Up @@ -36,12 +41,11 @@ describe("config", () => {
retryDelay: 3000,
applicationName: "block-explorer-api",
},
swagger: {
enabled: true,
bffEnabled: true,
},
contractVerificationApiUrl: "http://127.0.0.1:3070",
disableExternalAPI: false,
featureFlags: {
feature1Enabled: true,
feature2Enabled: false,
},
});
});

Expand Down
10 changes: 2 additions & 8 deletions packages/api/src/config.ts → packages/api/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
import * as featureFlags from "./featureFlags";

export default () => {
const {
Expand All @@ -9,9 +10,6 @@ export default () => {
DATABASE_URL,
DATABASE_CONNECTION_POOL_SIZE,
DATABASE_CONNECTION_IDLE_TIMEOUT_MS,
DISABLE_API_SCHEMA_DOCS,
DISABLE_BFF_API_SCHEMA_DOCS,
DISABLE_EXTERNAL_API,
DATABASE_STATEMENT_TIMEOUT_MS,
CONTRACT_VERIFICATION_API_URL,
} = process.env;
Expand Down Expand Up @@ -74,11 +72,7 @@ export default () => {
collectDbConnectionPoolMetricsInterval: parseInt(COLLECT_DB_CONNECTION_POOL_METRICS_INTERVAL, 10) || 10000,
},
typeORM: getTypeOrmModuleOptions(),
swagger: {
enabled: DISABLE_API_SCHEMA_DOCS !== "true",
bffEnabled: DISABLE_BFF_API_SCHEMA_DOCS !== "true",
},
disableExternalAPI: DISABLE_EXTERNAL_API === "true",
contractVerificationApiUrl: CONTRACT_VERIFICATION_API_URL || "http://127.0.0.1:3070",
featureFlags,
};
};
2 changes: 1 addition & 1 deletion packages/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function bootstrap() {
const metricsApp = await NestFactory.create(AppMetricsModule);
metricsApp.enableShutdownHooks();

if (configService.get<boolean>("swagger.enabled")) {
if (configService.get<boolean>("featureFlags.swagger.enabled")) {
const swaggerConfig = new DocumentBuilder()
.setTitle("Block explorer API")
.setDescription("ZkSync Block Explorer API")
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/stats/stats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { BatchService } from "../batch/batch.service";
import { BlockService } from "../block/block.service";
import { TransactionService } from "../transaction/transaction.service";
import { StatsDto } from "./stats.dto";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "stats";

@ApiTags("Stats BFF")
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import { TransferService } from "../transfer/transfer.service";
import { TokenDto } from "./token.dto";
import { TransferDto } from "../transfer/transfer.dto";
import { ParseAddressPipe, ADDRESS_REGEX_PATTERN } from "../common/pipes/parseAddress.pipe";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "tokens";

@ApiTags("Token BFF")
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/transaction/transaction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import { LogDto } from "../log/log.dto";
import { LogService } from "../log/log.service";
import { TransactionService } from "./transaction.service";
import { ParseTransactionHashPipe, TX_HASH_REGEX_PATTERN } from "../common/pipes/parseTransactionHash.pipe";
import config from "../config";
import { swagger } from "../config/featureFlags";

const { swagger } = config();
const entityName = "transactions";

@ApiTags("Transaction BFF")
Expand Down

0 comments on commit 1a3e6bc

Please sign in to comment.