From 1d2f8c60646ba2f7b00dfbe9e2072927c31a59dd Mon Sep 17 00:00:00 2001 From: junjiequan Date: Wed, 18 Sep 2024 16:31:28 +0200 Subject: [PATCH 1/4] feat: allow the swagger path to be configurable --- README.md | 1 + src/main.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 50e87e5d3..103b8c3fd 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,7 @@ Valid environment variables for the .env file. See [.env.example](/.env.example) | `ES_FIELDS_LIMIT` | number | | The total number of fields in an index. | 1000 | | `ES_REFRESH` | string | | If set to `wait_for`, Elasticsearch will wait till data is inserted into the specified index before returning a response. | false | | `LOGGERS_CONFIG_FILE` | string | | The file name for loggers configuration, located in the project root directory. | "loggers.json" | +| `SWAGGER_PATH` | string | Yes | swaggerPath is the path where the swagger UI will be available| "explorer"| ## Migrating from the old SciCat Backend diff --git a/src/main.ts b/src/main.ts index a8c8aaec8..a499e5ac1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,8 +36,9 @@ async function bootstrap() { docExpansion: "none", }, }; + const swaggerPath = process.env.SWAGGER_PATH || "explorer"; - SwaggerModule.setup("explorer", app, document, swaggerOptions); + SwaggerModule.setup(swaggerPath, app, document, swaggerOptions); app.useGlobalPipes( /** From 59aea01e16f483052f1578f15f03587f1ef9d72f Mon Sep 17 00:00:00 2001 From: junjiequan Date: Thu, 19 Sep 2024 10:13:17 +0200 Subject: [PATCH 2/4] Update the version in package.json from 4.0.0 to 4.5.0. In configuration.ts, add a new property "versions.api" with a default value of "v3". Also, add a new property "swaggerPath" with a default value of "explorer". --- package.json | 2 +- src/config/configuration.ts | 4 ++++ src/main.ts | 16 +++++++++------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 5e0a998b7..c2d233fcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scicat-backend-next", - "version": "4.0.0", + "version": "4.5.0", "description": "scicat-backend-next", "author": "", "private": true, diff --git a/src/config/configuration.ts b/src/config/configuration.ts index e73d42a72..a8c6527e6 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -58,6 +58,10 @@ const configuration = () => { }); const config = { + versions: { + api: process.env.API_VERSION || "v3", + }, + swaggerPath: process.env.SWAGGER_PATH || "explorer", loggerConfigs: jsonConfigMap.loggers || [defaultLogger], adminGroups: adminGroups.split(",").map((v) => v.trim()) ?? [], deleteGroups: deleteGroups.split(",").map((v) => v.trim()) ?? [], diff --git a/src/main.ts b/src/main.ts index a499e5ac1..06a6f82cb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,11 @@ async function bootstrap() { const app = await NestFactory.create(AppModule, { bufferLogs: true, }); + const configService: ConfigService, false> = app.get( + ConfigService, + ); + const apiVersion = configService.get("versions.api") ?? "v3"; + const swaggerPath = `${configService.get("swaggerPath")}`; const scicatLogger = app.get(ScicatLogger); @@ -23,20 +28,21 @@ async function bootstrap() { app.useGlobalFilters(new AllExceptionsFilter(scicatLogger)); app.enableCors(); - app.setGlobalPrefix("api/v3"); + + app.setGlobalPrefix(`api/${apiVersion}`); const config = new DocumentBuilder() .setTitle("SciCat backend API") .setDescription("This is the API for the SciCat Backend") - .setVersion("" + process.env.npm_package_version) + .setVersion(`api/${apiVersion}`) .addBearerAuth() .build(); + const document = SwaggerModule.createDocument(app, config); const swaggerOptions: SwaggerCustomOptions = { swaggerOptions: { docExpansion: "none", }, }; - const swaggerPath = process.env.SWAGGER_PATH || "explorer"; SwaggerModule.setup(swaggerPath, app, document, swaggerOptions); @@ -71,10 +77,6 @@ async function bootstrap() { app.use(json({ limit: "16mb" })); - const configService: ConfigService, false> = app.get( - ConfigService, - ); - const expressSessionSecret = configService.get( "expressSessionSecret", ); From 75ed50318ea50627adefcd8013ac2754d08da162 Mon Sep 17 00:00:00 2001 From: junjiequan Date: Thu, 19 Sep 2024 10:16:05 +0200 Subject: [PATCH 3/4] make api version not configurable. --- src/config/configuration.ts | 4 ++-- src/main.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/configuration.ts b/src/config/configuration.ts index a8c6527e6..97af54dfb 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -58,8 +58,8 @@ const configuration = () => { }); const config = { - versions: { - api: process.env.API_VERSION || "v3", + VERSIONS: { + API: "v3", }, swaggerPath: process.env.SWAGGER_PATH || "explorer", loggerConfigs: jsonConfigMap.loggers || [defaultLogger], diff --git a/src/main.ts b/src/main.ts index 06a6f82cb..96b2f84e6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,7 +18,7 @@ async function bootstrap() { const configService: ConfigService, false> = app.get( ConfigService, ); - const apiVersion = configService.get("versions.api") ?? "v3"; + const apiVersion = configService.get("VERSIONS.API"); const swaggerPath = `${configService.get("swaggerPath")}`; const scicatLogger = app.get(ScicatLogger); From 55329e75881a2fcea97721a99c5133ec8f16ddf2 Mon Sep 17 00:00:00 2001 From: junjiequan Date: Thu, 19 Sep 2024 15:10:21 +0200 Subject: [PATCH 4/4] converting api version vars naming to lower case --- src/config/configuration.ts | 4 ++-- src/main.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 97af54dfb..96e9e9bbd 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -58,8 +58,8 @@ const configuration = () => { }); const config = { - VERSIONS: { - API: "v3", + versions: { + api: "v3", }, swaggerPath: process.env.SWAGGER_PATH || "explorer", loggerConfigs: jsonConfigMap.loggers || [defaultLogger], diff --git a/src/main.ts b/src/main.ts index 96b2f84e6..980dc66a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,7 +18,7 @@ async function bootstrap() { const configService: ConfigService, false> = app.get( ConfigService, ); - const apiVersion = configService.get("VERSIONS.API"); + const apiVersion = configService.get("versions.api"); const swaggerPath = `${configService.get("swaggerPath")}`; const scicatLogger = app.get(ScicatLogger);