Move mx logic to top level functions middleware
This commit is contained in:
@@ -166,7 +166,7 @@ export async function loader({
|
|||||||
}: {
|
}: {
|
||||||
context: RequestContext;
|
context: RequestContext;
|
||||||
}): Promise<{ [k: string]: any }> {
|
}): Promise<{ [k: string]: any }> {
|
||||||
if (await context.env.DATA.get("mx"))
|
if (context.data.mx)
|
||||||
throw new Response(null, {
|
throw new Response(null, {
|
||||||
status: 503,
|
status: 503,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import getPermissions from "./permissions.js";
|
import getPermissions from "./permissions.js";
|
||||||
import { jsonError } from "./common.js";
|
import { jsonError } from "./common.js";
|
||||||
|
import { PrismaClient } from "../generated/prisma/client.js";
|
||||||
|
import { PrismaD1 } from "@prisma/adapter-d1";
|
||||||
import * as Sentry from "@sentry/cloudflare";
|
import * as Sentry from "@sentry/cloudflare";
|
||||||
|
|
||||||
async function constructHTML(context: RequestContext) {
|
async function constructHTML(context: RequestContext) {
|
||||||
@@ -28,6 +30,27 @@ async function generateTokenHash(token: string) {
|
|||||||
.replace(/=/g, "");
|
.replace(/=/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function initializePrisma(context: RequestContext) {
|
||||||
|
const adapter = new PrismaD1(context.env.D1);
|
||||||
|
context.data.prisma = new PrismaClient({ adapter });
|
||||||
|
|
||||||
|
return await context.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mxAndBypassCheck(context: RequestContext) {
|
||||||
|
if (!(await context.env.DATA.get("mx"))) return await context.next();
|
||||||
|
const cookies = context.request.headers.get("cookie")?.split(/; *?/);
|
||||||
|
const isAPI = new URL(context.request.url).pathname.startsWith("/api");
|
||||||
|
|
||||||
|
if (!cookies?.length || !cookies.find((c) => c.startsWith("mxb="))) {
|
||||||
|
if (isAPI) return jsonError("API is undergoing maintenance", 503);
|
||||||
|
|
||||||
|
context.data.mx = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await context.next();
|
||||||
|
}
|
||||||
|
|
||||||
async function refreshAuth(context: RequestContext) {
|
async function refreshAuth(context: RequestContext) {
|
||||||
const { current_user: currentUser } = context.data;
|
const { current_user: currentUser } = context.data;
|
||||||
|
|
||||||
@@ -395,5 +418,6 @@ export const onRequest = [
|
|||||||
setTheme,
|
setTheme,
|
||||||
constructHTML,
|
constructHTML,
|
||||||
setBody,
|
setBody,
|
||||||
|
initializePrisma,
|
||||||
setHeaders,
|
setHeaders,
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user