Skip to content

Commit

Permalink
Remove redis module and setup of sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper committed Jan 17, 2024
1 parent 72bee55 commit 1463782
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 112 deletions.
1 change: 0 additions & 1 deletion apps/server/src/infra/redis/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './redis.module';
export * from './interface/redis.constants';
32 changes: 0 additions & 32 deletions apps/server/src/infra/redis/redis.module.ts

This file was deleted.

75 changes: 4 additions & 71 deletions apps/server/src/modules/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -30,15 +29,12 @@ 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, Inject, MiddlewareConsumer, Module, NestModule, NotFoundException } from '@nestjs/common';
import { DynamicModule, Module, 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 { LegacyLogger, LoggerModule } from '@src/core/logger';
import RedisStore from 'connect-redis';
import session from 'express-session';
import { RedisClientType } from 'redis';
import { LoggerModule } from '@src/core/logger';
import { ServerController } from './controller/server.controller';
import { serverConfig } from './server.config';

Expand Down Expand Up @@ -89,45 +85,6 @@ 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 @@ -147,22 +104,10 @@ const setupSessions = (
// debug: true, // use it for locally debugging of queries
}),
LoggerModule,
RedisModule,
],
controllers: [ServerController],
})
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);
}
}
export class ServerModule {}

/**
* Server module used for testing.
Expand All @@ -178,22 +123,10 @@ export class ServerModule implements NestModule {
MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }),
RabbitMQWrapperTestModule,
LoggerModule,
RedisModule,
],
controllers: [ServerController],
})
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);
}

export class ServerTestModule {
static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule {
return {
module: ServerTestModule,
Expand Down
14 changes: 6 additions & 8 deletions apps/server/src/modules/tldraw/tldraw-test.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@infra/database';
import { defaultMikroOrmOptions } from '@modules/server';
import { HttpModule } from '@nestjs/axios';
import { DynamicModule, Module } from '@nestjs/common';
import { MongoMemoryDatabaseModule, MongoDatabaseModuleOptions } from '@infra/database';
import { Logger, LoggerModule } from '@src/core/logger';
import { ConfigModule } from '@nestjs/config';
import { createConfigModuleOptions } from '@src/config';
import { RedisModule } from '@infra/redis';
import { defaultMikroOrmOptions } from '@modules/server';
import { HttpModule } from '@nestjs/axios';
import { MetricsService } from './metrics';
import { Logger, LoggerModule } from '@src/core/logger';
import { config } from './config';
import { TldrawController } from './controller/tldraw.controller';
import { TldrawService } from './service/tldraw.service';
import { MetricsService } from './metrics';
import { TldrawRepo } from './repo/tldraw.repo';
import { TldrawService } from './service/tldraw.service';

const imports = [
MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }),
LoggerModule,
ConfigModule.forRoot(createConfigModuleOptions(config)),
RedisModule,
HttpModule,
];
const providers = [Logger, TldrawService, TldrawRepo, MetricsService];
Expand Down

0 comments on commit 1463782

Please sign in to comment.