Personal no-bs blogging platform.
Something went wrong. Try again.
1.3 kB · 39 lines
TypeScript
at master
123456789101112131415161718192021222324252627282930313233343536373839import { DOMAIN } from '@/lib/constants';import { createMiddleware } from 'hono/factory';import sub from '@/routes/blog';import { get_blog_domain } from '@/db/blogs';import { sub_store } from './subdomain_store';import { deleteCookie, getCookie } from 'hono/cookie';import { validate_session } from '@/db/db';
export default createMiddleware(async (c, next) => { const host = c.req.header('host')?.replaceAll(/:\d+$/g, "")!; c.set('host', host);
if (host == undefined) await next(); if ((host!.match(/\./g) || []).length > (DOMAIN.match(/\./g) || []).length && host.includes(DOMAIN)) { try { const blog = await get_blog_domain(host.replace(`.${DOMAIN}`, "")); const session_cookie = getCookie(c, 'session');
let session; if (session_cookie) { try { session = await validate_session(session_cookie); } catch (e) { deleteCookie(c, 'session'); } }
sub_store.set(c.req.raw, { blog, session }); return sub.fetch(c.req.raw); } catch (e) { console.error(e); return c.notFound(); } } else if (host === DOMAIN) { await next(); }
return c.text("You shouldn't be here!");});