small updates

This commit is contained in:
Evert Prants 2022-08-18 10:12:02 +03:00
parent ded40a2514
commit 545a71e57d
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
12 changed files with 72 additions and 41 deletions

View File

@ -46,6 +46,6 @@ async function bootstrap() {
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('pug');
await app.listen(3000);
await app.listen(3000, '0.0.0.0');
}
bootstrap();

View File

@ -1,3 +1,5 @@
import { DataSourceOptions } from 'typeorm';
export interface SMTPConfiguration {
host: string;
port: number;
@ -29,4 +31,5 @@ export interface Configuration {
app: AppConfiguration;
email: EmailConfiguration;
jwt: JWTConfiguration;
database: DataSourceOptions;
}

View File

@ -17,6 +17,7 @@ export const configProviders: Provider<any>[] = [
useValue: {
app: {
base_url: 'http://localhost:3000',
// generate the following with crypto.randomBytes(256 / 8).toString('hex')
session_secret: 'change me!',
challenge_secret: 'change me!',
},
@ -37,23 +38,37 @@ export const configProviders: Provider<any>[] = [
issuer: 'localhost',
expiration: 3600,
},
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'icyauth',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: true,
migrations: ['migration/*.js'],
cli: {
migrationsDir: 'migration',
},
},
} as Configuration,
},
{
provide: 'CONFIGURATION',
useFactory: async (
path: string,
def: Configuration,
configPath: string,
defaultConfig: Configuration,
): Promise<Configuration> => {
try {
const file = await readFile(path, { encoding: 'utf-8' });
const file = await readFile(configPath, { encoding: 'utf-8' });
return {
...def,
...defaultConfig,
...JSON.parse(JSON.stringify(toml.parse(file))),
};
} catch (e: any) {
console.error('Failed to load configuration:', e.message);
return def;
return defaultConfig;
}
},
inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'],

View File

@ -1,14 +1,16 @@
import { Provider } from '@nestjs/common';
import { FactoryProvider } from '@nestjs/common';
import { ConfigurationService } from 'src/modules/config/config.service';
import { ConnectionOptions, createConnection } from 'typeorm';
import { DataSource, DataSourceOptions } from 'typeorm';
export const databaseProviders: Provider<any>[] = [
export const databaseProviders: FactoryProvider<Promise<DataSource>>[] = [
{
provide: 'DATABASE_CONNECTION',
provide: 'DATA_SOURCE',
useFactory: async (config: ConfigurationService) => {
return await createConnection({
...config.get<ConnectionOptions>('database'),
const dataSource = new DataSource({
...config.get<DataSourceOptions>('database'),
});
return dataSource.initialize();
},
inject: [ConfigurationService],
},

View File

@ -1,6 +1,6 @@
import { Provider } from '@nestjs/common';
import { join } from 'path';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { Document } from './document.entity';
@ -11,7 +11,7 @@ export const documentProviders: Provider<any>[] = [
},
{
provide: 'DOCUMENT_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Document),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) => dataSource.getRepository(Document),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,5 +1,5 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { OAuth2ClientAuthorization } from './oauth2-client-authorization.entity';
import { OAuth2ClientURL } from './oauth2-client-url.entity';
import { OAuth2Client } from './oauth2-client.entity';
@ -7,20 +7,20 @@ import { OAuth2Client } from './oauth2-client.entity';
export const clientProviders: Provider<any>[] = [
{
provide: 'CLIENT_REPOSITORY',
useFactory: (connection: Connection) =>
connection.getRepository(OAuth2Client),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(OAuth2Client),
inject: ['DATA_SOURCE'],
},
{
provide: 'CLIENT_URL_REPOSITORY',
useFactory: (connection: Connection) =>
connection.getRepository(OAuth2ClientURL),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(OAuth2ClientURL),
inject: ['DATA_SOURCE'],
},
{
provide: 'CLIENT_AUTHORIZATION_REPOSITORY',
useFactory: (connection: Connection) =>
connection.getRepository(OAuth2ClientAuthorization),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(OAuth2ClientAuthorization),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,12 +1,12 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { OAuth2Token } from './oauth2-token.entity';
export const tokenProviders: Provider<any>[] = [
{
provide: 'TOKEN_REPOSITORY',
useFactory: (connection: Connection) =>
connection.getRepository(OAuth2Token),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) =>
dataSource.getRepository(OAuth2Token),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { Privilege } from './privilege.entity';
export const privilegeProviders: Provider<any>[] = [
{
provide: 'PRIVILEGE_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Privilege),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) => dataSource.getRepository(Privilege),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { Upload } from './upload.entity';
export const uploadProviders: Provider<any>[] = [
{
provide: 'UPLOAD_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(Upload),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) => dataSource.getRepository(Upload),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { UserToken } from './user-token.entity';
export const userTokenProviders: Provider<any>[] = [
{
provide: 'USER_TOKEN_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(UserToken),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) => dataSource.getRepository(UserToken),
inject: ['DATA_SOURCE'],
},
];

View File

@ -1,11 +1,11 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'typeorm';
import { DataSource } from 'typeorm';
import { User } from './user.entity';
export const userProviders: Provider<any>[] = [
{
provide: 'USER_REPOSITORY',
useFactory: (connection: Connection) => connection.getRepository(User),
inject: ['DATABASE_CONNECTION'],
useFactory: (dataSource: DataSource) => dataSource.getRepository(User),
inject: ['DATA_SOURCE'],
},
];

View File

@ -8,6 +8,11 @@ export class FormUtilityService {
public passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d\w\W]{8,}$/;
public usernameRegex = /^[a-zA-Z0-9_\-.]{3,26}$/;
/**
* Merge array of objects into single object
* @param flash Array of objects
* @returns Merged object
*/
public mergeObjectArray(flash: Record<string, any>[]): Record<string, any> {
return flash.reduce<Record<string, any>>(
(obj, item) => ({ ...obj, ...item }),
@ -15,6 +20,12 @@ export class FormUtilityService {
);
}
/**
* Trim values of specified keys of an object
* @param entry Object
* @param fields Fields to strip spaces from
* @returns Object with trimmed values
*/
public trimmed<T>(entry: T, fields: string[]): T {
return fields.reduce<T>(
(object, key) => ({ ...object, [key]: object[key]?.trim() }),