Skip to content

Commit

Permalink
Revert "BC-6297 Remove redis module and setup of sessions (#4702)"
Browse files Browse the repository at this point in the history
This reverts commit 79f88a4.
  • Loading branch information
virgilchiriac committed Jan 19, 2024
1 parent 119a2a3 commit 1571d13
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/server/src/infra/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './redis.module';
export * from './interface/redis.constants';
32 changes: 32 additions & 0 deletions apps/server/src/infra/redis/redis.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { Module } from '@nestjs/common';
import { LegacyLogger, LoggerModule } from '@src/core/logger';
import { createClient, RedisClientType } from 'redis';
import { REDIS_CLIENT } from './interface/redis.constants';

@Module({
imports: [LoggerModule],
providers: [
{
provide: REDIS_CLIENT,
useFactory: (logger: LegacyLogger) => {
logger.setContext(RedisModule.name);

if (Configuration.has('REDIS_URI')) {
const redisUrl: string = Configuration.get('REDIS_URI') as string;
const client: RedisClientType = createClient({ url: redisUrl });

client.on('error', (error) => logger.error(error));
client.on('connect', (msg) => logger.log(msg));

return client;
}

return undefined;
},
inject: [LegacyLogger],
},
],
exports: [REDIS_CLIENT],
})
export class RedisModule {}
75 changes: 71 additions & 4 deletions apps/server/src/modules/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Configuration } from '@hpi-schul-cloud/commons';
import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@infra/database';
import { MailModule } from '@infra/mail';
import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@infra/rabbitmq';
import { RedisModule, REDIS_CLIENT } from '@infra/redis';
import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { AccountApiModule } from '@modules/account/account-api.module';
Expand Down Expand Up @@ -29,12 +30,15 @@ import { ImportUserModule } from '@modules/user-import';
import { UserLoginMigrationApiModule } from '@modules/user-login-migration/user-login-migration-api.module';
import { UserApiModule } from '@modules/user/user-api.module';
import { VideoConferenceApiModule } from '@modules/video-conference/video-conference-api.module';
import { DynamicModule, Module, NotFoundException } from '@nestjs/common';
import { DynamicModule, Inject, MiddlewareConsumer, Module, NestModule, NotFoundException } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ALL_ENTITIES } from '@shared/domain/entity';
import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config';
import { CoreModule } from '@src/core';
import { LoggerModule } from '@src/core/logger';
import { LegacyLogger, LoggerModule } from '@src/core/logger';
import RedisStore from 'connect-redis';
import session from 'express-session';
import { RedisClientType } from 'redis';
import { ServerController } from './controller/server.controller';
import { serverConfig } from './server.config';

Expand Down Expand Up @@ -85,6 +89,45 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
};

const setupSessions = (
consumer: MiddlewareConsumer,
redisClient: RedisClientType | undefined,
logger: LegacyLogger
) => {
const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number;

let store: RedisStore | undefined;
if (redisClient) {
store = new RedisStore({
client: redisClient,
ttl: sessionDuration,
});
} else {
logger.warn(
'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!'
);
}

consumer
.apply(
session({
store,
secret: Configuration.get('SESSION__SECRET') as string,
resave: false,
saveUninitialized: false,
name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined,
proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined,
cookie: {
secure: Configuration.get('SESSION__SECURE') as boolean,
sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none',
httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean,
maxAge: sessionDuration * 1000,
},
})
)
.forRoutes('*');
};

/**
* Server Module used for production
*/
Expand All @@ -104,10 +147,22 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
// debug: true, // use it for locally debugging of queries
}),
LoggerModule,
RedisModule,
],
controllers: [ServerController],
})
export class ServerModule {}
export class ServerModule implements NestModule {
constructor(
@Inject(REDIS_CLIENT) private readonly redisClient: RedisClientType | undefined,
private readonly logger: LegacyLogger
) {
logger.setContext(ServerModule.name);
}

configure(consumer: MiddlewareConsumer) {
setupSessions(consumer, this.redisClient, this.logger);
}
}

/**
* Server module used for testing.
Expand All @@ -123,10 +178,22 @@ export class ServerModule {}
MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }),
RabbitMQWrapperTestModule,
LoggerModule,
RedisModule,
],
controllers: [ServerController],
})
export class ServerTestModule {
export class ServerTestModule implements NestModule {
constructor(
@Inject(REDIS_CLIENT) private readonly redisClient: RedisClientType | undefined,
private readonly logger: LegacyLogger
) {
logger.setContext(ServerTestModule.name);
}

configure(consumer: MiddlewareConsumer) {
setupSessions(consumer, undefined, this.logger);
}

static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule {
return {
module: ServerTestModule,
Expand Down

0 comments on commit 1571d13

Please sign in to comment.