Skip to content

Commit

Permalink
refactor(be): env validation & parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Oct 9, 2024
1 parent 8567a59 commit 6abdc5c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
9 changes: 7 additions & 2 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ import { GraphQLModule } from "@nestjs/graphql";
import { ScheduleModule } from "@nestjs/schedule";

import { GRAPHQL_API_ROOT } from "src/constants/graphql.const";
import { Environment } from "src/enums/environment.enum";
import { LoggerMiddleware } from "src/middleware/logger.middleware";
import { DepartureModule } from "src/modules/departure/departure.module";
import { ImportModule } from "src/modules/import/import.module";
import { PlatformModule } from "src/modules/platform/platform.module";
import { PrismaModule } from "src/modules/prisma/prisma.module";
import { StopModule } from "src/modules/stop/stop.module";
import { envSchema } from "src/schema/env.schema";

@Module({
imports: [
PlatformModule,
DepartureModule,
ImportModule,
...(process.env.NODE_ENV === Environment.TEST ? [] : [ImportModule]),
StopModule,

PrismaModule,
ConfigModule.forRoot(),
ConfigModule.forRoot({
envFilePath: ".env.local",
validate: envSchema.parse,
}),
ScheduleModule.forRoot(),
CacheModule.register({
isGlobal: true,
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/enums/environment.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Environment {
TEST = "test",
}
6 changes: 1 addition & 5 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import {
async function bootstrap() {
const app = await NestFactory.create(AppModule);

if (process.env.GOLEMIO_API_KEY === undefined) {
throw new Error("GOLEMIO_API_KEY is not set");
}

const config = new DocumentBuilder()
.setTitle(SWAGGER_TITLE)
.setDescription(SWAGGER_DESCRIPTION)
Expand All @@ -28,6 +24,6 @@ async function bootstrap() {
customSiteTitle: SWAGGER_TITLE,
});

await app.listen(3001);
await app.listen(process.env.PORT ?? 3001);
}
bootstrap();
5 changes: 5 additions & 0 deletions apps/backend/src/modules/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { PrismaClient } from "@prisma/client";

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
constructor() {
super({
datasourceUrl: `postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.POSTGRES_DB}?schema=${process.env.DB_SCHEMA}`,
});
}
async onModuleInit() {
await this.$connect();
}
Expand Down
12 changes: 12 additions & 0 deletions apps/backend/src/schema/env.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";

export const envSchema = z.object({
GOLEMIO_API_KEY: z.string(),
POSTGRES_USER: z.string(),
POSTGRES_PASSWORD: z.string(),
POSTGRES_DB: z.string(),
DB_HOST: z.string(),
DB_PORT: z.coerce.number().int().positive(),
DB_SCHEMA: z.string(),
PORT: z.coerce.number().int().positive().optional(),
});

0 comments on commit 6abdc5c

Please sign in to comment.