import { Context, Hono } from 'hono'; // import { ipRestriction } from 'hono/ip-restriction'; import type { KVNamespace } from './env'; import { book, upload, ssr } from './routers/index'; declare global { let my_kv: KVNamespace; } const app = new Hono().basePath('/'); // Register route modules app.route('/book', book); app.route('/upload', upload); app.route('/ssr', ssr); // IP restriction middleware (optional) // app.use( // '*', // ipRestriction( // c => ({ // remote: { // // @ts-expect-error // address: c.req.raw.eo.clientIp, // addressType: // String( // // @ts-expect-error // c.req.raw.eo.clientIp // ).indexOf('::') === -1 // ? 'IPv4' // : 'IPv6' // } // }), // { // denyList: [], // allowList: [ '127.0.0.1', '::1'] // } // ) // ); const notFound = async (c: Context) => { return c.html( ` 404 - Page Not Found

404

Page Not Found

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

← Go back to home

`, 404 ); }; // Fallback to static directory app.notFound(async (c) => { const url = new URL(c.req.url); if (url.pathname === '/') { url.pathname = '/index.html'; } try { const res = await fetch(url.toString(), { headers: c.req.header() }); if (res.ok) { const contentType = res.headers.get('Content-Type')!; const body = await res.arrayBuffer(); return new Response(body, { status: res.status, headers: { 'Content-Type': contentType, 'Cache-Control': 'public, max-age=3600', }, }); } } catch (error) { return notFound(c); } return notFound(c); }); app.onError((err, c) => { return c.html( ` 500 - Internal Server Error

500

Internal Server Error

Something went wrong on our server. Please try again later.

Error: ${err.message}

← Go back to home

`, 500 ); }); // EdgeOne Functions export export function onRequest(context: { request: Request; params: Record; env: Record; }): Response | Promise { return app.fetch(context.request, context.env); }