feat: Use CF

This commit is contained in:
Wanjohi
2025-06-06 09:09:11 +03:00
parent a47dc91b22
commit 5fd5608e6e
8 changed files with 129 additions and 212 deletions

View File

@@ -25,6 +25,7 @@
"@aws-sdk/client-sqs": "^3.806.0",
"@nestri/core": "workspace:",
"actor-core": "^0.8.0",
"aws4fetch": "^1.0.20",
"hono": "^4.7.8",
"hono-openapi": "^0.4.8"
}

View File

@@ -2,7 +2,6 @@ import "zod-openapi/extend";
import { cors } from "hono/cors";
import { GameApi } from "./game";
import { SteamApi } from "./steam";
import { ImageApi } from "./image";
import { auth } from "./utils/auth";
import { FriendApi } from "./friend";
import { logger } from "hono/logger";
@@ -25,10 +24,6 @@ app
return next();
})
.use(auth)
// Private routes
app.
route("/image", ImageApi.route)
const routes = app
.get("/", (c) => c.text("Hello World!"))

View File

@@ -1,67 +1,89 @@
import { Hono } from "hono";
import { AwsClient } from 'aws4fetch'
import { Resource } from "sst";
import { HTTPException } from "hono/http-exception";
export namespace ImageRoute {
export const route = new Hono()
.get(
"/:hashWithExt",
async (c) => {
const { hashWithExt } = c.req.param();
// const { hashWithExt } = c.req.param();
// Validate format
// Split hash and extension
const match = hashWithExt.match(/^([a-zA-Z0-9_-]+)\.(avif|webp)$/);
if (!match) {
throw new HTTPException(400, { message: "Invalid image hash or format" });
}
const [, hash, format] = match;
const query = c.req.query();
// Parse dimensions
const width = parseInt(query.w || query.width || "");
const height = parseInt(query.h || query.height || "");
const dpr = parseFloat(query.dpr || "1");
if (isNaN(width) || width <= 0) {
throw new HTTPException(400, { message: "Invalid width" });
}
if (!isNaN(height) && height < 0) {
throw new HTTPException(400, { message: "Invalid height" });
}
if (dpr < 1 || dpr > 4) {
throw new HTTPException(400, { message: "Invalid dpr" });
}
console.log("url",Resource.Api.url)
const imageBytes = await fetch(`${Resource.Api.url}/image/${hash}`,{
method:"POST",
body:JSON.stringify({
dpr,
width,
height,
format
})
const client = new AwsClient({
accessKeyId: Resource.ImageInvokerAccessKey.key,
secretAccessKey: Resource.ImageInvokerAccessKey.secret,
})
console.log("imahe",imageBytes.headers)
const LAMBDA_URL = `https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/${Resource.ImageProcessor.name}/invocations`
// Normalize and build cache key
// const cacheKey = `${hash}_${format}_w${width}${height ? `_h${height}` : ""}_dpr${dpr}`;
const lambdaResponse = await client.fetch(LAMBDA_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ world: "hello" }),
})
// Add aggressive caching
// c.header("Cache-Control", "public, max-age=315360000, immutable");
if (!lambdaResponse.ok) {
console.error(await lambdaResponse.text())
return c.json({ error: `Lambda API returned ${lambdaResponse.status}` }, { status: 500 })
}
// Placeholder image response (to be replaced by real logic)
return c.newResponse(await imageBytes.arrayBuffer(),
// {
// headers: {
// ...imageBytes.headers
// }
// }
);
console.log(await lambdaResponse.json())
// // Validate format
// // Split hash and extension
// const match = hashWithExt.match(/^([a-zA-Z0-9_-]+)\.(avif|webp)$/);
// if (!match) {
// throw new HTTPException(400, { message: "Invalid image hash or format" });
// }
// const [, hash, format] = match;
// const query = c.req.query();
// // Parse dimensions
// const width = parseInt(query.w || query.width || "");
// const height = parseInt(query.h || query.height || "");
// const dpr = parseFloat(query.dpr || "1");
// if (isNaN(width) || width <= 0) {
// throw new HTTPException(400, { message: "Invalid width" });
// }
// if (!isNaN(height) && height < 0) {
// throw new HTTPException(400, { message: "Invalid height" });
// }
// if (dpr < 1 || dpr > 4) {
// throw new HTTPException(400, { message: "Invalid dpr" });
// }
// console.log("url",Resource.Api.url)
// const imageBytes = await fetch(`${Resource.Api.url}/image/${hash}`,{
// method:"POST",
// body:JSON.stringify({
// dpr,
// width,
// height,
// format
// })
// })
// console.log("imahe",imageBytes.headers)
// // Normalize and build cache key
// // const cacheKey = `${hash}_${format}_w${width}${height ? `_h${height}` : ""}_dpr${dpr}`;
// // Add aggressive caching
// // c.header("Cache-Control", "public, max-age=315360000, immutable");
// // Placeholder image response (to be replaced by real logic)
// return c.newResponse(await imageBytes.arrayBuffer(),
// // {
// // headers: {
// // ...imageBytes.headers
// // }
// // }
// );
return c.text("success")
}

View File

@@ -1 +1,5 @@
export const handler = () => { }
export async function handler(event: any) {
console.log('Task completion event received:', JSON.stringify(event, null, 2));
return JSON.stringify({ hello: "world" })
}