Skip to content

Multi environment support #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -31,5 +31,5 @@ GOOGLE_CLIENT_SECRET=''
GOOGLE_REDIRECT_URI = ''

# DATABSES
DATABASE_URL=""
REDIS_URL=""
REDIS_URL=""
MONGO_DATABASE_URL=""
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -74,8 +74,10 @@ web_modules/

# dotenv environment variable files
.env
.env.development
.env.development.local
.env.test.local
.env.production
.env.production.local
.env.local

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -24,16 +24,18 @@ Before you get started, make sure you have the following installed on your machi
```sh
docker compose up -d
```

2. **Install Dependencies**:

- Use pnpm to install all the necessary dependencies:
```sh
pnpm i
```

3. **Configure Environment Variables**:

- Create a `.env` file in the root directory.
- Use the provided `.env.sample` as a template to enter all the required environment variables.
- Create `.env.development` and `.env.production` files in the root directory.
- Use the provided `.env.sample` as a template to set up the environment variables in these files.

## What's Included
Comment on lines -35 to 38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Consider explaining the reason for separate development and production environment files.

While separating environments is a good practice, a brief explanation of why you're using two files now might help users understand the change better.

Suggested change
- Create a `.env` file in the root directory.
- Use the provided `.env.sample` as a template to enter all the required environment variables.
- Create `.env.development` and `.env.production` files in the root directory.
- Use the provided `.env.sample` as a template to set up the environment variables in these files.
## What's Included
- Create `.env.development` and `.env.production` files in the root directory:
- `.env.development` for local development settings
- `.env.production` for production environment settings
- This separation allows for different configurations based on the environment
- Use the provided `.env.sample` as a template to set up the environment variables in these files


4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
"description": "",
"main": "dist/main.js",
"scripts": {
"dev": "dotenv -e .env -- ts-node-dev --transpile-only ./src/main.ts",
"dev": "dotenvx run -f .env.development -- ts-node-dev --transpile-only ./src/main.ts",
"prod": "dotenvx run -f .env.production -- ts-node-dev --transpile-only ./src/main.ts",
"seeder": "ts-node --transpile-only ./src/seeder.ts",
"build": "tsup --config build.ts",
"start:prod": "dotenv -e .env -- node ./dist/main.js",
@@ -55,6 +56,7 @@
"@aws-sdk/client-s3": "^3.606.0",
"@bull-board/api": "^5.19.0",
"@bull-board/express": "^5.16.0",
"@dotenvx/dotenvx": "^1.14.0",
"@prisma/client": "^5.13.0",
"@types/compression": "^1.7.2",
"argon2": "^0.30.3",
8,724 changes: 4,879 additions & 3,845 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

33 changes: 27 additions & 6 deletions src/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import dotenv from 'dotenv';
import { z } from 'zod';
import dotenvx from '@dotenvx/dotenvx';
import { z, ZodError } from 'zod';

dotenv.config();
dotenvx.config();

const prettyPrintErrors = (errObj: ZodError) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider making prettyPrintErrors more flexible

Instead of logging directly to the console, consider having the function return a formatted string. This would make it more reusable in different contexts, allowing the caller to decide how to handle the output.

const prettyPrintErrors = (errObj: ZodError): string => {
  let output = '';
  const formattedError = errObj.format();
  Object.entries(formattedError).forEach(([key, value]) => {
    if (value && '_errors' in value) {
      output += `${key}: ${value._errors.join(', ')}\n`;
    }
  });
  return output.trim();
};

const formattedError = errObj.format();
Object.entries(formattedError).forEach(([key, value]) => {
if (key !== '_errors') {
console.log(`${key}:`);
if (Array.isArray(value?._errors)) {
value._errors.forEach((errorMessage: string) => {
console.log(` - ${errorMessage}`);
});
}
}
});
};

// Remove .optional() from requried schema properties

const configSchema = z.object({
REDIS_URL: z.string().url(),
PORT: z.string().regex(/^\d+$/).transform(Number),
DATABASE_URL: z.string().url().optional(),
MONGO_DATABASE_URL: z.string().url(),
SMTP_HOST: z.string().min(1).optional(),
SMTP_PORT: z.string().regex(/^\d+$/).transform(Number).optional(),
@@ -34,6 +47,14 @@ const configSchema = z.object({

export type Config = z.infer<typeof configSchema>;

const config = configSchema.parse(process.env);
const config = configSchema.safeParse(process.env);

if (!config.success) {
console.log('------------------------------------------------');
console.log('There is an error with the environment variables');
console.log('------------------------------------------------');
console.log(prettyPrintErrors(config.error));
process.exit(1);
}

export default config;
export default config.data;
4 changes: 2 additions & 2 deletions src/upload/upload.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response } from 'express';
import { errorResponse, successResponse } from '../utils/api.utils';
import { updateUser } from '../user/user.services';
import { UserType } from '../types';
import { updateUser } from '../modules/user/user.services';
import { UserType } from '../modules/user/user.dto';

export const handleProfileUpload = async (req: Request, res: Response) => {
try {