Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/api-gateway/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FileConfigModule } from './modules/file_config/file_config.module';
import { AnalyticsModule } from './modules/analytics/analytics.module';
import { AnalyticsConfigModule } from './modules/analytics_config/analytics_config.module';
import { FileDeleteModule } from './modules/file_delete/file_delete.module';
import { FeatureFlagModule } from './modules/feature_flag/feature_flag.module';

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { FileDeleteModule } from './modules/file_delete/file_delete.module';
FileDeleteModule,
AnalyticsModule,
AnalyticsConfigModule,
FeatureFlagModule,
],
})
export class AppModule {}
63 changes: 63 additions & 0 deletions packages/api-gateway/src/models/feature_flag.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, IsOptional, IsBoolean } from 'class-validator';
import { FeatureFlagProto } from 'juno-proto';

export class CreateFeatureFlagModel {
@ApiProperty({
type: 'string',
description: 'Unique identifier for the feature flag',
})
@IsString()
id: string;

@ApiPropertyOptional({
type: 'boolean',
description: 'Whether the flag is enabled. Defaults to false.',
})
@IsOptional()
@IsBoolean()
enabled?: boolean;

@ApiPropertyOptional({
type: 'string',
description: 'Human-readable description of the flag',
})
@IsOptional()
@IsString()
description?: string;
}

export class UpdateFeatureFlagModel {
@ApiPropertyOptional({
type: 'boolean',
description: 'Whether the flag is enabled',
})
@IsOptional()
@IsBoolean()
enabled?: boolean;

@ApiPropertyOptional({
type: 'string',
description: 'Human-readable description of the flag',
})
@IsOptional()
@IsString()
description?: string;
}

export class FeatureFlagResponse {
@ApiProperty({ type: 'string', description: 'Feature flag ID' })
id: string;

@ApiProperty({ type: 'boolean', description: 'Whether the flag is enabled' })
enabled: boolean;

@ApiPropertyOptional({ type: 'string', description: 'Flag description' })
description?: string;

constructor(flag: FeatureFlagProto.FeatureFlag) {
this.id = flag.id;
this.enabled = flag.enabled;
this.description = flag.description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Inject,
OnModuleInit,
Param,
Body,
} from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import {
ApiBearerAuth,
ApiBadRequestResponse,
ApiCreatedResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { FeatureFlagProto } from 'juno-proto';
import { lastValueFrom } from 'rxjs';
import {
CreateFeatureFlagModel,
UpdateFeatureFlagModel,
FeatureFlagResponse,
} from 'src/models/feature_flag.dto';

const { FEATURE_FLAG_SERVICE_NAME } = FeatureFlagProto;

@ApiBearerAuth('API_Key')
@ApiTags('feature-flag')
@Controller('feature-flags')
export class FeatureFlagController implements OnModuleInit {
private featureFlagService: FeatureFlagProto.FeatureFlagServiceClient;

constructor(
@Inject(FEATURE_FLAG_SERVICE_NAME) private featureFlagClient: ClientGrpc,
) {}

onModuleInit() {
this.featureFlagService =
this.featureFlagClient.getService<FeatureFlagProto.FeatureFlagServiceClient>(
FEATURE_FLAG_SERVICE_NAME,
);
}

@Post()
@ApiOperation({ summary: 'Create a feature flag' })
@ApiCreatedResponse({
description: 'Feature flag created successfully',
type: FeatureFlagResponse,
})
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiBadRequestResponse({ description: 'Bad Request' })
async createFlag(
@Body() request: CreateFeatureFlagModel,
): Promise<FeatureFlagResponse> {
const response = await lastValueFrom(
this.featureFlagService.createFlag({
id: request.id,
enabled: request.enabled,
description: request.description,
}),
);
return new FeatureFlagResponse(response.flag);
}

@Get(':id')
@ApiOperation({ summary: 'Get a feature flag by ID' })
@ApiOkResponse({
description: 'Feature flag retrieved successfully',
type: FeatureFlagResponse,
})
@ApiNotFoundResponse({ description: 'Feature flag not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getFlag(@Param('id') id: string): Promise<FeatureFlagResponse> {
const response = await lastValueFrom(
this.featureFlagService.getFlag({ id }),
);
return new FeatureFlagResponse(response.flag);
}

@Put(':id')
@ApiOperation({ summary: 'Update a feature flag' })
@ApiOkResponse({
description: 'Feature flag updated successfully',
type: FeatureFlagResponse,
})
@ApiNotFoundResponse({ description: 'Feature flag not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiBadRequestResponse({ description: 'Bad Request' })
async setFlag(
@Param('id') id: string,
@Body() request: UpdateFeatureFlagModel,
): Promise<FeatureFlagResponse> {
const response = await lastValueFrom(
this.featureFlagService.setFlag({
id,
enabled: request.enabled,
description: request.description,
}),
);
return new FeatureFlagResponse(response.flag);
}

@Delete(':id')
@ApiOperation({ summary: 'Delete a feature flag' })
@ApiOkResponse({ description: 'Feature flag deleted successfully' })
@ApiNotFoundResponse({ description: 'Feature flag not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async deleteFlag(@Param('id') id: string): Promise<{ success: boolean }> {
const response = await lastValueFrom(
this.featureFlagService.deleteFlag({ id }),
);
return { success: response.success };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { join } from 'path';
import { FeatureFlagController } from './feature_flag.controller';
import {
ApiKeyProto,
ApiKeyProtoFile,
FeatureFlagProto,
FeatureFlagProtoFile,
JwtProto,
JwtProtoFile,
} from 'juno-proto';
import { ApiKeyMiddleware } from 'src/middleware/api_key.middleware';

const { JWT_SERVICE_NAME, JUNO_JWT_PACKAGE_NAME } = JwtProto;
const { API_KEY_SERVICE_NAME, JUNO_API_KEY_PACKAGE_NAME } = ApiKeyProto;
const { FEATURE_FLAG_SERVICE_NAME, JUNO_FEATURE_FLAG_PACKAGE_NAME } =
FeatureFlagProto;

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: join(__dirname, '../../../../../.env.local'),
}),
ClientsModule.register([
{
name: JWT_SERVICE_NAME,
transport: Transport.GRPC,
options: {
url: process.env.AUTH_SERVICE_ADDR,
package: JUNO_JWT_PACKAGE_NAME,
protoPath: JwtProtoFile,
},
},
{
name: API_KEY_SERVICE_NAME,
transport: Transport.GRPC,
options: {
url: process.env.AUTH_SERVICE_ADDR,
package: JUNO_API_KEY_PACKAGE_NAME,
protoPath: ApiKeyProtoFile,
},
},
{
name: FEATURE_FLAG_SERVICE_NAME,
transport: Transport.GRPC,
options: {
url: process.env.DB_SERVICE_ADDR,
package: JUNO_FEATURE_FLAG_PACKAGE_NAME,
protoPath: FeatureFlagProtoFile,
},
},
]),
],
controllers: [FeatureFlagController],
})
export class FeatureFlagModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(ApiKeyMiddleware).forRoutes('feature-flags*');
}
}
Loading
Loading