Skip to content
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
1 change: 0 additions & 1 deletion typescript/packages/toolshed/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { RedisClientType } from "redis";
export interface AppBindings {
Variables: {
logger: Logger;
blobbyRedis: RedisClientType;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ import {
getUserBlobs,
removeBlobFromUser,
} from "./lib/redis.ts";
import type { RedisClientType } from "redis";
import { DiskStorage } from "@/routes/storage/blobby/lib/storage.ts";
import env from "@/env.ts";

const DATA_DIR = `${env.CACHE_DIR}/blobby`;

export const storage = new DiskStorage(DATA_DIR);
await storage.init();
import { getRedisClient, storage } from "./utils.ts";

export const uploadBlobHandler: AppRouteHandler<
typeof uploadBlob
> = async (c) => {
const redis = c.get("blobbyRedis");
if (!redis) throw new Error("Redis client not found in context");
const redis = await getRedisClient();
const logger = c.get("logger");
const key = c.req.param("key");
const content = await c.req.json();
Expand Down Expand Up @@ -90,8 +82,7 @@ export const getBlobPathHandler: AppRouteHandler<
export const listBlobsHandler: AppRouteHandler<typeof listBlobs> = async (
c,
) => {
const redis: RedisClientType = c.get("blobbyRedis");
if (!redis) throw new Error("Redis client not found in context");
const redis = await getRedisClient();
const logger = c.get("logger");
const showAll = c.req.query("all") === "true";
const showAllWithData = c.req.query("allWithData") !== undefined;
Expand Down Expand Up @@ -174,8 +165,7 @@ export const listBlobsHandler: AppRouteHandler<typeof listBlobs> = async (
export const deleteBlobHandler: AppRouteHandler<typeof deleteBlob> = async (
c,
) => {
const redis = c.get("blobbyRedis");
if (!redis) throw new Error("Redis client not found in context");
const redis = await getRedisClient();
const logger = c.get("logger");
const key = c.req.param("key");

Expand Down
30 changes: 0 additions & 30 deletions typescript/packages/toolshed/routes/storage/blobby/blobby.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,10 @@
import { createRouter } from "@/lib/create-app.ts";
import * as handlers from "./blobby.handlers.ts";
import * as routes from "./blobby.routes.ts";
import env from "@/env.ts";
import { createClient } from "redis";
import type { RedisClientType } from "redis";
import { cors } from "@hono/hono/cors";

const router = createRouter();

router.use("/api/storage/blobby/*", async (c, next) => {
const logger = c.get("logger");
try {
const redis = createClient({
url: env.BLOBBY_REDIS_URL,
});

redis.on("error", (err) => {
logger.error({ err }, "Redis client error");
});

logger.info("Connecting to Redis...");
if (!redis.isOpen) {
await redis.connect();
}
logger.info("Redis connected successfully");

c.set("blobbyRedis", redis as RedisClientType);
await next();
logger.info("Closing Redis connection");
await redis.quit();
} catch (error) {
logger.error(error, "Error in Redis middleware");
throw error;
}
});

router.use(
"/api/storage/blobby/*",
cors({
Expand Down
Loading