make env validation errors clear

* modify mail smtp variable names
This commit is contained in:
Philipinho
2024-06-27 17:55:17 +01:00
parent 20c1a92885
commit a582d4786d
4 changed files with 49 additions and 33 deletions

View File

@ -1,16 +1,19 @@
# your domain, e.g https://example.com
APP_URL=http://localhost:3000 APP_URL=http://localhost:3000
APP_SECRET=REPLACE_WITH_LONG_SECRET
PORT=3000 PORT=3000
# make sure to replace this.
APP_SECRET=REPLACE_WITH_LONG_SECRET
JWT_TOKEN_EXPIRES_IN=30d JWT_TOKEN_EXPIRES_IN=30d
DATABASE_URL="postgresql://postgres:password@localhost:5432/docmost?schema=public" DATABASE_URL="postgresql://postgres:password@localhost:5432/docmost?schema=public"
REDIS_URL=redis://127.0.0.1:6379 REDIS_URL=redis://127.0.0.1:6379
# local | s3 # options: local | s3
STORAGE_DRIVER=local STORAGE_DRIVER=local
# S3 Config # S3 driver config
AWS_S3_ACCESS_KEY_ID= AWS_S3_ACCESS_KEY_ID=
AWS_S3_SECRET_ACCESS_KEY= AWS_S3_SECRET_ACCESS_KEY=
AWS_S3_REGION= AWS_S3_REGION=
@ -18,15 +21,17 @@ AWS_S3_BUCKET=
AWS_S3_ENDPOINT= AWS_S3_ENDPOINT=
AWS_S3_URL= AWS_S3_URL=
# EMAIL drivers: smtp / postmark / log # options: smtp | postmark
MAIL_DRIVER=smtp MAIL_DRIVER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_FROM_ADDRESS=hello@example.com MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME=Docmost MAIL_FROM_NAME=Docmost
# for postmark driver # SMTP driver config
SMTP_HOST=127.0.0.1
SMTP_PORT=587
SMTP_USERNAME=
SMTP_PASSWORD=
# Postmark driver config
POSTMARK_TOKEN= POSTMARK_TOKEN=

View File

@ -71,28 +71,28 @@ export class EnvironmentService {
return this.configService.get<string>('MAIL_DRIVER', 'log'); return this.configService.get<string>('MAIL_DRIVER', 'log');
} }
getMailHost(): string {
return this.configService.get<string>('MAIL_HOST');
}
getMailPort(): number {
return parseInt(this.configService.get<string>('MAIL_PORT'));
}
getMailUsername(): string {
return this.configService.get<string>('MAIL_USERNAME');
}
getMailPassword(): string {
return this.configService.get<string>('MAIL_PASSWORD');
}
getMailFromAddress(): string { getMailFromAddress(): string {
return this.configService.get<string>('MAIL_FROM_ADDRESS'); return this.configService.get<string>('MAIL_FROM_ADDRESS');
} }
getMailFromName(): string { getMailFromName(): string {
return this.configService.get<string>('MAIL_FROM_NAME'); return this.configService.get<string>('MAIL_FROM_NAME', 'Docmost');
}
getSmtpHost(): string {
return this.configService.get<string>('SMTP_HOST');
}
getSmtpPort(): number {
return parseInt(this.configService.get<string>('SMTP_PORT'));
}
getSmtpUsername(): string {
return this.configService.get<string>('SMTP_USERNAME');
}
getSmtpPassword(): string {
return this.configService.get<string>('SMTP_PASSWORD');
} }
getPostmarkToken(): string { getPostmarkToken(): string {

View File

@ -1,12 +1,16 @@
import { IsNotEmpty, IsUrl, validateSync } from 'class-validator'; import { IsNotEmpty, IsNotIn, IsUrl, validateSync } from 'class-validator';
import { plainToInstance } from 'class-transformer'; import { plainToInstance } from 'class-transformer';
export class EnvironmentVariables { export class EnvironmentVariables {
@IsNotEmpty() @IsNotEmpty()
@IsUrl({ protocols: ['postgres', 'postgresql'], require_tld: false }) @IsUrl(
{ protocols: ['postgres', 'postgresql'], require_tld: false },
{ message: 'DATABASE_URL must be a valid postgres connection string' },
)
DATABASE_URL: string; DATABASE_URL: string;
@IsNotEmpty() @IsNotEmpty()
@IsNotIn(['REPLACE_WITH_LONG_SECRET'])
APP_SECRET: string; APP_SECRET: string;
} }
@ -14,14 +18,21 @@ export function validate(config: Record<string, any>) {
const validatedConfig = plainToInstance(EnvironmentVariables, config); const validatedConfig = plainToInstance(EnvironmentVariables, config);
const errors = validateSync(validatedConfig); const errors = validateSync(validatedConfig);
console.error(
'The EnvironmentVariables has failed the following validations:',
);
if (errors.length > 0) { if (errors.length > 0) {
errors.map((error) => { errors.map((error) => {
console.error(error.toString()); console.log(JSON.stringify(error.constraints));
}); });
console.log( console.log(
'Please fix the environment variables and try again. Shutting down...', 'Please fix the environment variables and try again. Shutting down...',
); );
process.exit(1); process.exit(1);
} }
return validatedConfig; return validatedConfig;
} }

View File

@ -29,12 +29,12 @@ export const mailDriverConfigProvider = {
return { return {
driver, driver,
config: { config: {
host: environmentService.getMailHost(), host: environmentService.getSmtpHost(),
port: environmentService.getMailPort(), port: environmentService.getSmtpPort(),
connectionTimeout: 30 * 1000, // 30 seconds connectionTimeout: 30 * 1000, // 30 seconds
auth: { auth: {
user: environmentService.getMailUsername(), user: environmentService.getSmtpUsername(),
pass: environmentService.getMailPassword(), pass: environmentService.getSmtpPassword(),
}, },
} as SMTPTransport.Options, } as SMTPTransport.Options,
}; };