Skip to content

Commit

Permalink
feat: config to hide swagger for BFF endpoints (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk authored Oct 20, 2023
1 parent f01128b commit 0c0b64e
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PORT=3020
LOG_LEVEL=debug
LIMITED_PAGINATION_MAX_ITEMS=100000
DISABLE_API_SCHEMA_DOCS=false
DISABLE_BFF_API_SCHEMA_DOCS=false
DISABLE_EXTERNAL_API=false
DATABASE_STATEMENT_TIMEOUT_MS=90000
CONTRACT_VERIFICATION_API_URL=http://127.0.0.1:3070
12 changes: 11 additions & 1 deletion packages/api/src/address/address.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Controller, Get, Param, Query } from "@nestjs/common";
import { ApiTags, ApiParam, ApiOkResponse, ApiBadRequestResponse, ApiExtraModels, refs } from "@nestjs/swagger";
import {
ApiTags,
ApiParam,
ApiOkResponse,
ApiBadRequestResponse,
ApiExtraModels,
refs,
ApiExcludeController,
} from "@nestjs/swagger";
import { Pagination } from "nestjs-typeorm-paginate";
import { utils } from "ethers";
import { PagingOptionsWithMaxItemsLimitDto, ListFiltersDto } from "../common/dtos";
Expand All @@ -16,10 +24,12 @@ 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 { swagger } from "../config/featureFlags";

const entityName = "address";

@ApiTags("Address BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class AddressController {
constructor(
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
11 changes: 10 additions & 1 deletion packages/api/src/batch/batch.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Param, NotFoundException, Query } from "@nestjs/common";
import { ApiTags, ApiParam, ApiOkResponse, ApiBadRequestResponse, ApiNotFoundResponse } from "@nestjs/swagger";
import {
ApiTags,
ApiParam,
ApiOkResponse,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiExcludeController,
} from "@nestjs/swagger";
import { Pagination } from "nestjs-typeorm-paginate";
import { ParseLimitedIntPipe } from "../common/pipes/parseLimitedInt.pipe";
import { PagingOptionsDto, ListFiltersDto } from "../common/dtos";
Expand All @@ -8,10 +15,12 @@ import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkRespons
import { BatchService } from "./batch.service";
import { BatchDto } from "./batch.dto";
import { BatchDetailsDto } from "./batchDetails.dto";
import { swagger } from "../config/featureFlags";

const entityName = "batches";

@ApiTags("Batch BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class BatchController {
constructor(private readonly batchService: BatchService) {}
Expand Down
11 changes: 10 additions & 1 deletion packages/api/src/block/block.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Param, NotFoundException, Query } from "@nestjs/common";
import { ApiTags, ApiParam, ApiOkResponse, ApiBadRequestResponse, ApiNotFoundResponse } from "@nestjs/swagger";
import {
ApiTags,
ApiParam,
ApiOkResponse,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiExcludeController,
} from "@nestjs/swagger";
import { Pagination } from "nestjs-typeorm-paginate";
import { buildDateFilter } from "../common/utils";
import { ParseLimitedIntPipe } from "../common/pipes/parseLimitedInt.pipe";
Expand All @@ -8,10 +15,12 @@ import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkRespons
import { BlockService } from "./block.service";
import { BlockDto } from "./block.dto";
import { BlockDetailDto } from "./blockDetail.dto";
import { swagger } from "../config/featureFlags";

const entityName = "blocks";

@ApiTags("Block BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class BlockController {
constructor(private readonly blockService: BlockService) {}
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,11 +41,11 @@ describe("config", () => {
retryDelay: 3000,
applicationName: "block-explorer-api",
},
swagger: {
enabled: true,
},
contractVerificationApiUrl: "http://127.0.0.1:3070",
disableExternalAPI: false,
featureFlags: {
feature1Enabled: true,
feature2Enabled: false,
},
});
});

Expand Down
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,8 +10,6 @@ export default () => {
DATABASE_URL,
DATABASE_CONNECTION_POOL_SIZE,
DATABASE_CONNECTION_IDLE_TIMEOUT_MS,
DISABLE_API_SCHEMA_DOCS,
DISABLE_EXTERNAL_API,
DATABASE_STATEMENT_TIMEOUT_MS,
CONTRACT_VERIFICATION_API_URL,
} = process.env;
Expand Down Expand Up @@ -73,10 +72,7 @@ export default () => {
collectDbConnectionPoolMetricsInterval: parseInt(COLLECT_DB_CONNECTION_POOL_METRICS_INTERVAL, 10) || 10000,
},
typeORM: getTypeOrmModuleOptions(),
swagger: {
enabled: DISABLE_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
4 changes: 3 additions & 1 deletion packages/api/src/stats/stats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Controller, Get } from "@nestjs/common";
import { ApiTags, ApiOkResponse } from "@nestjs/swagger";
import { ApiTags, ApiOkResponse, ApiExcludeController } from "@nestjs/swagger";
import { Not, IsNull } from "typeorm";
import { BatchService } from "../batch/batch.service";
import { BlockService } from "../block/block.service";
import { TransactionService } from "../transaction/transaction.service";
import { StatsDto } from "./stats.dto";
import { swagger } from "../config/featureFlags";

const entityName = "stats";

@ApiTags("Stats BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class StatsController {
constructor(
Expand Down
11 changes: 10 additions & 1 deletion packages/api/src/token/token.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Param, NotFoundException, Query } from "@nestjs/common";
import { ApiTags, ApiParam, ApiOkResponse, ApiBadRequestResponse, ApiNotFoundResponse } from "@nestjs/swagger";
import {
ApiTags,
ApiParam,
ApiOkResponse,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiExcludeController,
} from "@nestjs/swagger";
import { Pagination } from "nestjs-typeorm-paginate";
import { PagingOptionsDto, PagingOptionsWithMaxItemsLimitDto } from "../common/dtos";
import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkResponse";
Expand All @@ -8,10 +15,12 @@ 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 { swagger } from "../config/featureFlags";

const entityName = "tokens";

@ApiTags("Token BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class TokenController {
constructor(private readonly tokenService: TokenService, private readonly transferService: TransferService) {}
Expand Down
11 changes: 10 additions & 1 deletion packages/api/src/transaction/transaction.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Param, NotFoundException, Query } from "@nestjs/common";
import { ApiTags, ApiParam, ApiBadRequestResponse, ApiNotFoundResponse, ApiOkResponse } from "@nestjs/swagger";
import {
ApiTags,
ApiParam,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiExcludeController,
} from "@nestjs/swagger";
import { Pagination } from "nestjs-typeorm-paginate";
import { ApiListPageOkResponse } from "../common/decorators/apiListPageOkResponse";
import { PagingOptionsWithMaxItemsLimitDto, ListFiltersDto } from "../common/dtos";
Expand All @@ -12,10 +19,12 @@ 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 { swagger } from "../config/featureFlags";

const entityName = "transactions";

@ApiTags("Transaction BFF")
@ApiExcludeController(!swagger.bffEnabled)
@Controller(entityName)
export class TransactionController {
constructor(
Expand Down
2 changes: 1 addition & 1 deletion packages/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ ENV PORT $PORT
USER node
WORKDIR /usr/src/app/packages/app/dist

CMD http-server -p $PORT
CMD http-server -p $PORT -c-1 --proxy="http://127.0.0.1:$PORT/index.html?"

0 comments on commit 0c0b64e

Please sign in to comment.