Skip to content

Commit

Permalink
Merge pull request #1502 from SciCatProject/master
Browse files Browse the repository at this point in the history
11-13-2024 release
  • Loading branch information
Junjiequan authored Nov 14, 2024
2 parents 95ddf4a + e3c5ca3 commit 68a2d40
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/openapi/python-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"generatorName": "python",
"packageName": "scicat-sdk-py",
"projectName": "scicat-sdk-py"
"packageName": "scicat_sdk_py",
"projectName": "scicat_sdk_py"
}
4 changes: 2 additions & 2 deletions .github/openapi/python-pydantic-v1-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"generatorName": "python-pydantic-v1",
"packageName": "scicat-sdk-pydantic",
"projectName": "scicat-sdk-pydantic"
"packageName": "scicat_sdk_pydantic",
"projectName": "scicat_sdk_pydantic"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Valid environment variables for the .env file. See [.env.example](/.env.example)
| `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"|
| `MAX_FILE_UPLOAD_SIZE` | string | Yes | Maximum allowed file upload size | "16mb"|

## Migrating from the old SciCat Backend

Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/admin/admin.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from "@nestjs/common";
import { AdminService } from "./admin.service";
import { AdminController } from "./admin.controller";
import { ConfigModule } from "@nestjs/config";

@Module({
controllers: [AdminController],
imports: [],
imports: [ConfigModule],
providers: [AdminService],
exports: [AdminService],
})
Expand Down
46 changes: 33 additions & 13 deletions src/admin/admin.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { getModelToken } from "@nestjs/mongoose";
import { Test, TestingModule } from "@nestjs/testing";
import { ConfigService } from "@nestjs/config";
import { AdminService } from "./admin.service";
import config from "../config/frontend.config.json";
import theme from "../config/frontend.theme.json";

const mockConfig: Record<string, unknown> = config;

describe("PoliciesService", () => {
describe("AdminService", () => {
let service: AdminService;
const mockConfigService = {
get: jest.fn((propertyPath: string) => {
const config = {
maxFileUploadSizeInMb: "12mb",
} as Record<string, unknown>;

return config[propertyPath];
}),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AdminService,
{
provide: getModelToken("Policy"),
useValue: {
new: jest.fn().mockResolvedValue(mockConfig),
constructor: jest.fn().mockResolvedValue(mockConfig),
find: jest.fn(),
create: jest.fn(),
exec: jest.fn(),
},
provide: ConfigService,
useValue: mockConfigService,
},
AdminService,
],
}).compile();

Expand All @@ -31,4 +33,22 @@ describe("PoliciesService", () => {
it("should be defined", () => {
expect(service).toBeDefined();
});

describe("getConfig", () => {
it("should return modified config", async () => {
const result = await service.getConfig();

expect(result).toEqual({
...config,
maxFileUploadSizeInMb: "12mb",
});
});
});

describe("getTheme", () => {
it("should return theme config", async () => {
const result = await service.getTheme();
expect(result).toEqual(theme);
});
});
});
19 changes: 18 additions & 1 deletion src/admin/admin.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { Injectable } from "@nestjs/common";
import config from "../config/frontend.config.json";
import theme from "../config/frontend.theme.json";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class AdminService {
constructor(private configService: ConfigService) {}

async getConfig(): Promise<Record<string, unknown> | null> {
return config;
const modifiedConfig = this.applyBackendConfigAdjustments();

return modifiedConfig;
}

async getTheme(): Promise<Record<string, unknown> | null> {
return theme;
}

// NOTE: Adjusts backend config values for frontend use (e.g., file upload limits).
// Add future backend-dependent adjustments here as needed.
private applyBackendConfigAdjustments(): Record<string, unknown> {
const postEncodedMaxFileUploadSize =
this.configService.get<string>("maxFileUploadSizeInMb") || "16mb";

return {
...config,
maxFileUploadSizeInMb: postEncodedMaxFileUploadSize,
};
}
}
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const configuration = () => {
});

const config = {
maxFileUploadSizeInMb: process.env.MAX_FILE_UPLOAD_SIZE || "16mb", // 16MB by default
versions: {
api: "3",
},
Expand Down
14 changes: 11 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import session from "express-session";
import { json } from "body-parser";
import { NestFactory } from "@nestjs/core";
import {
DocumentBuilder,
Expand All @@ -10,9 +9,10 @@ import { AppModule } from "./app.module";
import { Logger, ValidationPipe, VersioningType } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { AllExceptionsFilter, ScicatLogger } from "./loggers/logger.service";
import { NestExpressApplication } from "@nestjs/platform-express";

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
bufferLogs: true,
});
const configService: ConfigService<Record<string, unknown>, false> = app.get(
Expand Down Expand Up @@ -84,7 +84,15 @@ async function bootstrap() {
}),
);

app.use(json({ limit: "16mb" }));
const fileUploadLimitInMb = configService.get<number>(
"maxFileUploadSizeInMb",
);

app.useBodyParser("json", { limit: fileUploadLimitInMb });
app.useBodyParser("urlencoded", {
limit: fileUploadLimitInMb,
extended: true,
});

const expressSessionSecret = configService.get<string>(
"expressSessionSecret",
Expand Down

0 comments on commit 68a2d40

Please sign in to comment.