mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-31 15:18:38 -04:00
refactor(config): extract note config from app config
This commit separates the app config object from a new note config object. This was done to separate different concerns in different config files. Especially if the number of settings that are about notes increase, it is a good idea to keep them separate from the app config. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
baa6606729
commit
f4a580cf2a
30 changed files with 161 additions and 78 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
@ -7,15 +7,13 @@ import { registerAs } from '@nestjs/config';
|
|||
import * as Joi from 'joi';
|
||||
|
||||
import { Loglevel } from './loglevel.enum';
|
||||
import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils';
|
||||
import { buildErrorMessage, parseOptionalInt } from './utils';
|
||||
|
||||
export interface AppConfig {
|
||||
domain: string;
|
||||
rendererOrigin: string;
|
||||
port: number;
|
||||
loglevel: Loglevel;
|
||||
forbiddenNoteIds: string[];
|
||||
maxDocumentLength: number;
|
||||
}
|
||||
|
||||
const schema = Joi.object({
|
||||
|
@ -30,15 +28,6 @@ const schema = Joi.object({
|
|||
.default(Loglevel.WARN)
|
||||
.optional()
|
||||
.label('HD_LOGLEVEL'),
|
||||
forbiddenNoteIds: Joi.array()
|
||||
.items(Joi.string())
|
||||
.optional()
|
||||
.default([])
|
||||
.label('HD_FORBIDDEN_NOTE_IDS'),
|
||||
maxDocumentLength: Joi.number()
|
||||
.default(100000)
|
||||
.optional()
|
||||
.label('HD_MAX_DOCUMENT_LENGTH'),
|
||||
});
|
||||
|
||||
export default registerAs('appConfig', () => {
|
||||
|
@ -48,8 +37,6 @@ export default registerAs('appConfig', () => {
|
|||
rendererOrigin: process.env.HD_RENDERER_ORIGIN,
|
||||
port: parseOptionalInt(process.env.PORT),
|
||||
loglevel: process.env.HD_LOGLEVEL,
|
||||
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
|
||||
maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH),
|
||||
},
|
||||
{
|
||||
abortEarly: false,
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
import { AppConfig } from '../app.config';
|
||||
import { Loglevel } from '../loglevel.enum';
|
||||
|
||||
export default registerAs('appConfig', () => ({
|
||||
domain: 'md.example.com',
|
||||
rendererOrigin: 'md-renderer.example.com',
|
||||
port: 3000,
|
||||
loglevel: Loglevel.ERROR,
|
||||
maxDocumentLength: 100000,
|
||||
forbiddenNoteIds: ['forbiddenNoteId'],
|
||||
}));
|
||||
export default registerAs(
|
||||
'appConfig',
|
||||
(): AppConfig => ({
|
||||
domain: 'md.example.com',
|
||||
rendererOrigin: 'md-renderer.example.com',
|
||||
port: 3000,
|
||||
loglevel: Loglevel.ERROR,
|
||||
}),
|
||||
);
|
||||
|
|
16
src/config/mock/note.config.mock.ts
Normal file
16
src/config/mock/note.config.mock.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
import { NoteConfig } from '../note.config';
|
||||
|
||||
export default registerAs(
|
||||
'noteConfig',
|
||||
(): NoteConfig => ({
|
||||
maxDocumentLength: 100000,
|
||||
forbiddenNoteIds: ['forbiddenNoteId'],
|
||||
}),
|
||||
);
|
46
src/config/note.config.ts
Normal file
46
src/config/note.config.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import * as Joi from 'joi';
|
||||
|
||||
import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils';
|
||||
|
||||
export interface NoteConfig {
|
||||
forbiddenNoteIds: string[];
|
||||
maxDocumentLength: number;
|
||||
}
|
||||
|
||||
const schema = Joi.object({
|
||||
forbiddenNoteIds: Joi.array()
|
||||
.items(Joi.string())
|
||||
.optional()
|
||||
.default([])
|
||||
.label('HD_FORBIDDEN_NOTE_IDS'),
|
||||
maxDocumentLength: Joi.number()
|
||||
.default(100000)
|
||||
.optional()
|
||||
.label('HD_MAX_DOCUMENT_LENGTH'),
|
||||
});
|
||||
|
||||
export default registerAs('noteConfig', () => {
|
||||
const noteConfig = schema.validate(
|
||||
{
|
||||
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
|
||||
maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH),
|
||||
},
|
||||
{
|
||||
abortEarly: false,
|
||||
presence: 'required',
|
||||
},
|
||||
);
|
||||
if (noteConfig.error) {
|
||||
const errorMessages = noteConfig.error.details.map(
|
||||
(detail) => detail.message,
|
||||
);
|
||||
throw new Error(buildErrorMessage(errorMessages));
|
||||
}
|
||||
return noteConfig.value as NoteConfig;
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue