starting caching work

This commit is contained in:
Kaushik Narayan R 2025-03-11 23:54:03 -07:00
parent a6ecf7df88
commit 1520dd830c
3 changed files with 35 additions and 27 deletions

View File

@ -2,5 +2,4 @@ BASE_DOMAIN = 127.0.0.1
REDIRECT_URI = http://127.0.0.1:9001/api/auth/callback
APP_URI = http://127.0.0.1:3000
DB_URI = postgres://your_database_username:your_database_password@127.0.0.1:your_database_port/your_database_name
REDIS_HOST = 127.0.0.1
REDIS_PORT = 6379
REDIS_URI = redis://127.0.0.1:6379

27
config/redis.ts Normal file
View File

@ -0,0 +1,27 @@
import { createClient } from "redis";
import curriedLogger from "../utils/logger.ts";
import { sleep } from "../utils/flake.ts";
const logger = curriedLogger(import.meta.filename);
if (!process.env["REDIS_URI"])
throw new TypeError("Redis connection URI not defined");
// Initialize
const redisClient: ReturnType<typeof createClient> = createClient({
url: process.env["REDIS_URI"],
});
// Check connection
(async () => {
try {
await redisClient.connect();
while (!redisClient.isReady) await sleep(100);
logger.info("Connected to Redis store");
} catch (error) {
logger.error("Redis connection error", { error });
throw error;
}
})();
export { redisClient };

View File

@ -8,15 +8,20 @@ import cors from "cors";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import { createClient } from "redis";
import { RedisStore } from "connect-redis";
import { redisClient } from "./config/redis.ts";
import { sessionName } from "./constants.ts";
import seqConn from "./models/index.ts";
import { isAuthenticated } from "./middleware/authCheck.ts";
import { getCurrentUsersProfile } from "./api/spotify.ts";
import authRoutes from "./routes/auth.ts";
import playlistRoutes from "./routes/playlists.ts";
import operationRoutes from "./routes/operations.ts";
import curriedLogger from "./utils/logger.ts";
const logger = curriedLogger(import.meta.filename);
@ -29,9 +34,6 @@ if (
) {
throw new TypeError("TRUST_PROXY must be 0 or 1");
}
if (isNaN(Number(process.env["REDIS_PORT"]))) {
throw new TypeError("REDIS_PORT must be a number");
}
if (!process.env["SESSION_SECRET"]) {
throw new TypeError("SESSION_SECRET cannot be undefined");
}
@ -39,24 +41,6 @@ if (!process.env["SESSION_SECRET"]) {
// Enable this if you run behind a proxy (e.g. nginx)
app.set("trust proxy", process.env["TRUST_PROXY"]);
// Configure Redis client and connect
const redisClient = createClient({
socket: {
host: process.env["REDIS_HOST"],
port: Number(process.env["REDIS_PORT"]),
},
});
redisClient
.connect()
.then(() => {
logger.info("Connected to Redis store");
})
.catch((error) => {
logger.error("Redis connection error", { error });
cleanupFunc();
});
const redisStore = new RedisStore({ client: redisClient });
// Configure session middleware
@ -119,9 +103,7 @@ app.use("/auth-health", isAuthenticated, async (req, res) => {
return null;
}
});
import authRoutes from "./routes/auth.ts";
import playlistRoutes from "./routes/playlists.ts";
import operationRoutes from "./routes/operations.ts";
// Routes
app.use("/api/auth/", authRoutes);
app.use("/api/playlists", isAuthenticated, playlistRoutes);