init Cf page project (#85)

init cf page project
This commit is contained in:
zizifn
2023-01-02 02:18:20 +08:00
committed by GitHub
parent 2e794541d0
commit d291067219
28 changed files with 1102 additions and 115 deletions

31
functions/_middleware.ts Normal file
View File

@@ -0,0 +1,31 @@
const skipUrls = ["ws"];
async function errorHandling(context: EventContext<any, any, any>) {
try {
return await context.next();
} catch (err) {
return new Response(`${err.message}\n${err.stack}`, { status: 500 });
}
}
function authentication(context: EventContext<any, any, any>) {
// skip authentication
if (skipUrls.filter((url) => context.request.url.endsWith(url))) {
return context.next();
}
const basicAuth = context.request.headers.get("Authorization") || "";
const authString = basicAuth.split(" ")?.[1] || "";
if (!atob(authString).includes("test")) {
return new Response(``, {
status: 401,
headers: {
"content-type": "text/html; charset=utf-8",
"WWW-Authenticate": "Basic",
},
});
} else {
return context.next();
}
}
export const onRequest = [errorHandling, authentication];