Skip to content

Commit

Permalink
added redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Gautam authored and Abhinav Gautam committed Oct 15, 2024
1 parent afaa2b7 commit edcdbb2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dotenv": "^16.4.5",
"express": "^4.21.0",
"puppeteer": "21.1.0",
"redis": "^4.7.0",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/routes/queue-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const router = express.Router();
import { v4 } from "uuid";
import { queue, queueEvents}from "../utils/queue-client";
import {Job} from "bullmq";

import {redisClient} from "../utils/redis-client";

type response = {
path: string
Expand All @@ -12,9 +12,11 @@ type response = {
router.post("/pdf", async (req: Request, res: Response) => {
try {
let apiKey = req.headers["x-api-key"];
if (apiKey != process.env.API_KEY) {
throw new Error("Invalid Api Key");
let valid = await redisClient.get(`tokens:${apiKey}`);
if(!valid) {
throw new Error("Api Key Wrong Or Expired");
}

const { task } = req.body;

let job: Job|undefined = await queue.add(v4(),task);
Expand Down
16 changes: 16 additions & 0 deletions src/utils/redis-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createClient } from 'redis';
import {queue} from "./queue-client";

export const redisClient = createClient(
{
url: `redis://${process.env.REDIS_USERNAME}:${process.env.REDIS_PASSWORD}@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/${process.env.REDIS_TOKEN_DB_INDEX}`
}
);

redisClient.on('error', err => console.log('Redis Client Error', err));

redisClient.connect().then(() => {
console.log("Redis Client Connected");
}).catch(reason => {
console.log("Redis Client Connection Failed", reason);
})

0 comments on commit edcdbb2

Please sign in to comment.