feat: hcaptcha
This commit is contained in:
+31
-21
@@ -1,25 +1,35 @@
|
||||
import { verifyJWT } from '../middleware/auth';
|
||||
import { createErrorResponse, createSuccessResponse } from '../utils';
|
||||
import hCaptchaPlugin from "@cloudflare/pages-plugin-hcaptcha";
|
||||
|
||||
export async function onRequestGet(context) {
|
||||
try {
|
||||
// Verify the JWT token
|
||||
const authResult = await verifyJWT(context);
|
||||
if (authResult) {
|
||||
return authResult; // Return the error response from the middleware
|
||||
export const onRequestPost = [
|
||||
async (context) => {
|
||||
return hCaptchaPlugin({
|
||||
secret: context.env.hcaptcha_secret_key,
|
||||
sitekey: context.env.hcaptcha_site_key,
|
||||
onError: (context) => {
|
||||
console.error("hCaptcha error:", context.error);
|
||||
return createErrorResponse("hCaptcha verification failed", 403);
|
||||
}
|
||||
})(context);
|
||||
},
|
||||
async (context) => {
|
||||
try {
|
||||
// Verify the JWT token
|
||||
const authResult = await verifyJWT(context);
|
||||
if (authResult) {
|
||||
return authResult; // Return the error response from the middleware
|
||||
}
|
||||
|
||||
const input = { prompt: '用繁體中文生成一句名言佳句' };
|
||||
|
||||
const response = await context.env.AI.run('@cf/meta/llama-3.2-1b-instruct', input);
|
||||
const motto = response.response;
|
||||
|
||||
return createSuccessResponse({ motto });
|
||||
} catch (error) {
|
||||
console.error("Gen motto error:", error);
|
||||
return createErrorResponse("Get motto failed", 500);
|
||||
}
|
||||
|
||||
// Use Cloudflare AI Gateway to proxy the request to Google AI Studio
|
||||
const ai = context.env.AI;
|
||||
|
||||
const input = { prompt: '用繁體中文生成一句名言佳句' };
|
||||
|
||||
const response = await ai.run('@cf/meta/llama-3.2-1b-instruct', input);
|
||||
const motto = response.response;
|
||||
|
||||
return createSuccessResponse({ motto });
|
||||
} catch (error) {
|
||||
console.error("Gen motto error:", error);
|
||||
return createErrorResponse("Get motto failed", 500);
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user