Skip to content
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

feat: add .env example #33

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# MongoDB connection string
MONGO_URI=mongodb://root:example@localhost:27017/phoenix?authSource=admin
# Server port
PORT=4000
# Your unique JWT secret
JWT_SECRET=your_jwt_secret
# Log level
LOG_LEVEL=debug

KAFKAJS_NO_PARTITIONER_WARNING=1
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ To install dependencies:
bun install
```

To run:

To create .env file and run:

```bash
cp .env.example .env
bun run start
```

Expand Down
3 changes: 2 additions & 1 deletion e2e-tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ApolloServer } from 'apollo-server-express';
import jwt from 'jsonwebtoken';
import PluginLoader from '../src/plugins/plugin-loader'; // Adjust the path as needed
import { setMongoServer } from './teardown';
import env from '../src/config/config';

const JWT_SECRET = process.env.JWT_SECRET || 'your_jwt_secret';
const JWT_SECRET = env.JWT_SECRET || 'your_jwt_secret';

let server: ApolloServer;
let app: express.Application;
Expand Down
6 changes: 4 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import dotenv from 'dotenv';
dotenv.config();

const env = cleanEnv(process.env, {
MONGO_URI: str({ desc: 'MongoDB connection string' }),
MONGO_URI: str({ desc: 'MongoDB connection string',
default: "mongodb://root:example@localhost:27017/phoenix?authSource=admin" }),
PORT: port({ default: 4000 }),
JWT_SECRET: str({ desc: 'Secret key for JWT token' }),
JWT_SECRET: str({ desc: 'Secret key for JWT token', default: 'your-secret'}),
JTW_EXPIRY: str({ default: '1y' }),
MODE: str({ choices: ['server', 'worker', 'dev'], default: 'dev' }),
REDIS_HOST: str({ default: 'localhost' }),
REDIS_PORT: port({ default: 6379 }),
KAFKA_BROKER: str({ default: 'localhost:29092' }),
});

export default env;
3 changes: 2 additions & 1 deletion src/event/kafka-event-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Kafka, type Producer, type Consumer } from 'kafkajs';
import { Container, Service } from 'typedi';
import env from '../config/config.ts';

@Service()
class KafkaEventService {
Expand All @@ -12,7 +13,7 @@ class KafkaEventService {
constructor() {
this.kafka = new Kafka({
clientId: 'my-app',
brokers: [process.env.KAFKA_BROKER || 'localhost:29092'], // Use environment variable or default to localhost
brokers: [env.KAFKA_BROKER || 'localhost:29092'], // Use environment variable or default to localhost
});

this.producer = this.kafka.producer();
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Request, Response, NextFunction } from 'express';
import jwt, { type JwtPayload } from 'jsonwebtoken';
import logger from '../config/logger.ts';
import env from '../config/config.ts';

const loggerCtx = { context: 'auth-middleware' };

Expand All @@ -18,7 +19,7 @@ export const authenticate = (req: Request, res: Response, next: NextFunction) =>
}

try {
const secret = process.env.JWT_SECRET;
const secret = env.JWT_SECRET;
if (!secret) {
throw new Error('JWT_SECRET is not defined');
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/auth-plugin/resolvers/auth-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AuthResolver {
throw new Error('Invalid credentials');
}

const token = jwt.sign({ role: user.role, id: user._id }, process.env.JWT_SECRET!, { expiresIn: env.JTW_EXPIRY });
const token = jwt.sign({ role: user.role, id: user._id }, env.JWT_SECRET!, { expiresIn: env.JTW_EXPIRY });
return token;
}

Expand Down
9 changes: 5 additions & 4 deletions src/plugins/sample-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SampleService } from './services/sample-service';
import KafkaEventService from '../../event/kafka-event-service';
import { Queue, Job } from 'bullmq';
import logger from '../../config/logger.ts';
import env from '../../config/config.ts';
Copy link
Contributor Author

@brent-hoover brent-hoover Jun 26, 2024

Choose a reason for hiding this comment

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

Not sure how this would work in a real plugin context. I think we handled this in RC with just each plugin having it's own config. Realistically you should not need to know the connection parameters to just create queues and workers.

Not solving this problem in this PR.


const loggerCtx = { context: 'sample-plugin/index' };

Expand All @@ -27,8 +28,8 @@ export default {
// Define and register the queue for this plugin
const sampleQueue = new Queue('sampleQueue', {
connection: {
host: process.env.REDIS_HOST || 'localhost',
port: Number(process.env.REDIS_PORT) || 6379,
host: env.REDIS_HOST || 'localhost',
port: Number(env.REDIS_PORT) || 6379,
},
});

Expand Down Expand Up @@ -56,8 +57,8 @@ export default {
processor: sampleJobProcessor,
options: {
connection: {
host: process.env.REDIS_HOST || 'localhost',
port: Number(process.env.REDIS_PORT) || 6379,
host: env.REDIS_HOST || 'localhost',
port: Number(env.REDIS_PORT) || 6379,
},
},
};
Expand Down
Loading