-
Notifications
You must be signed in to change notification settings - Fork 32
feat(POC): add run time config management service #2355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b4cb84a
1962f38
105e4b3
e7e05c5
7d24f30
0e8137c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { ApiProperty } from "@nestjs/swagger"; | ||
| import { IsString, IsOptional, IsObject } from "class-validator"; | ||
|
|
||
| export class OutputRuntimeConfigDto { | ||
| @ApiProperty({ | ||
| type: String, | ||
| description: "Unique config identifier (e.g. 'frontend', 'backend', etc.)", | ||
| example: "frontend", | ||
| }) | ||
| @IsString() | ||
| _id: string; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to have more than "frontend" and "backend"? I would make this an enum. Or are you thinking there might be use cases where other microservices store most of their configs on the scicat backend?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this statement.
We don’t want to lock it to just “frontend” or “backend”.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest just |
||
|
|
||
| @ApiProperty({ | ||
| type: Object, | ||
| description: "Configuration content as a JSON object", | ||
| }) | ||
| @IsObject() | ||
| data: Record<string, unknown>; | ||
|
|
||
| @ApiProperty({ | ||
| type: String, | ||
| required: false, | ||
| description: "Optional description of this configuration entry", | ||
| }) | ||
| @IsOptional() | ||
| @IsString() | ||
| description?: string; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use of this? Just internal documentation?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the idea. |
||
|
|
||
| @ApiProperty({ | ||
| type: String, | ||
| description: "User or system that last updated the configuration", | ||
| example: "system", | ||
| }) | ||
| @IsString() | ||
| updatedBy: string; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { | ||
| Body, | ||
| Controller, | ||
| Get, | ||
| Param, | ||
| Put, | ||
| Req, | ||
| UseGuards, | ||
| } from "@nestjs/common"; | ||
| import { | ||
| ApiBearerAuth, | ||
| ApiBody, | ||
| ApiOkResponse, | ||
| ApiTags, | ||
| } from "@nestjs/swagger"; | ||
| import { Request } from "express"; | ||
| import { AllowAny } from "src/auth/decorators/allow-any.decorator"; | ||
| import { JWTUser } from "src/auth/interfaces/jwt-user.interface"; | ||
| import { OutputRuntimeConfigDto } from "./dto/runtime-config.dto"; | ||
| import { RuntimeConfigService } from "./runtime-config.service"; | ||
| import { PoliciesGuard } from "src/casl/guards/policies.guard"; | ||
| import { Action } from "src/casl/action.enum"; | ||
| import { AppAbility } from "src/casl/casl-ability.factory"; | ||
| import { CheckPolicies } from "src/casl/decorators/check-policies.decorator"; | ||
| import { RuntimeConfig } from "./schemas/runtime-config.schema"; | ||
| @ApiBearerAuth() | ||
| @ApiTags("runtime-config") | ||
| @Controller("runtime-config") | ||
| export class RuntimeConfigController { | ||
| constructor(private readonly runtimeConfigService: RuntimeConfigService) {} | ||
|
|
||
| @AllowAny() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs the swagger descriptors (ApiOperation, ApiParam, etc) |
||
| @ApiOkResponse({ type: OutputRuntimeConfigDto }) | ||
| @Get("data/:id") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not remember why we decided to move it away from |
||
| async getConfig( | ||
| @Param("id") id: string, | ||
| ): Promise<OutputRuntimeConfigDto | null> { | ||
| const config = await this.runtimeConfigService.getConfig(id); | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| @UseGuards(PoliciesGuard) | ||
| @CheckPolicies("runtimeconfig", (ability: AppAbility) => | ||
| ability.can(Action.Update, RuntimeConfig), | ||
| ) | ||
| @Put("data/:id") | ||
| @ApiBody({ | ||
| type: Object, | ||
| description: "Runtime config object", | ||
| }) | ||
| @ApiOkResponse({ type: OutputRuntimeConfigDto }) | ||
| async updateConfig( | ||
| @Req() request: Request, | ||
| @Param("id") id: string, | ||
| @Body() config: Record<string, unknown>, | ||
| ): Promise<OutputRuntimeConfigDto | null> { | ||
| const user: JWTUser = request.user as JWTUser; | ||
| return await this.runtimeConfigService.updateConfig( | ||
| id, | ||
| config, | ||
| user.username, | ||
| ); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming is perfect if we are going to use this subsystem to store only configurations. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { MongooseModule } from "@nestjs/mongoose"; | ||
| import { RuntimeConfigService } from "./runtime-config.service"; | ||
| import { RuntimeConfigController } from "./runtime-config.controller"; | ||
| import { | ||
| RuntimeConfig, | ||
| RuntimeConfigSchema, | ||
| } from "./schemas/runtime-config.schema"; | ||
| import { | ||
| GenericHistory, | ||
| GenericHistorySchema, | ||
| } from "src/common/schemas/generic-history.schema"; | ||
| import { ConfigModule, ConfigService } from "@nestjs/config"; | ||
| import { CaslModule } from "src/casl/casl.module"; | ||
| import { applyHistoryPluginOnce } from "src/common/mongoose/plugins/history.plugin.util"; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| CaslModule, | ||
| ConfigModule, | ||
| MongooseModule.forFeature([ | ||
| { | ||
| name: GenericHistory.name, | ||
| schema: GenericHistorySchema, | ||
| }, | ||
| ]), | ||
| MongooseModule.forFeatureAsync([ | ||
| { | ||
| name: RuntimeConfig.name, | ||
| imports: [ConfigModule], | ||
| inject: [ConfigService], | ||
| useFactory: (configService: ConfigService) => { | ||
| const schema = RuntimeConfigSchema; | ||
| applyHistoryPluginOnce(schema, configService); | ||
|
|
||
| return schema; | ||
| }, | ||
| }, | ||
| ]), | ||
| ], | ||
| controllers: [RuntimeConfigController], | ||
| providers: [RuntimeConfigService], | ||
| exports: [RuntimeConfigService], | ||
| }) | ||
| export class RuntimeConfigModule {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also the fields
createdBy,updatedAt,createdAt.