Spaces:
Running
Running
Yuvraj Singh Chauhan commited on
added cors and worker
Browse filesFiles changed (2)
- app/main.py+9
- cloudflare-worker.jsAdded+35
3 unmodified lines45678944 unmodified lines5455565758593 unmodified linesfrom contextlib import asynccontextmanagerfrom fastapi import FastAPI, HTTPException, Request, Responsefrom fastapi.responses import JSONResponsefrom pydantic import BaseModel, field_validator44 unmodified lineslifespan=lifespan,)app.add_exception_handler(redis.exceptions.ConnectionError, redis_connection_error_handler)3 unmodified lines4567891044 unmodified lines55565758596061626364656667683 unmodified linesfrom contextlib import asynccontextmanagerfrom fastapi import FastAPI, HTTPException, Request, Responsefrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponsefrom pydantic import BaseModel, field_validator44 unmodified lineslifespan=lifespan,)app.add_middleware(CORSMiddleware,allow_origins=["https://pastebin.ysinghc.in"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)app.add_exception_handler(redis.exceptions.ConnectionError, redis_connection_error_handler)
1234567891011121314151617181920212223242526272829303132333435/** * Cloudflare Worker to keep the Pastebin API awake on Hugging Face. * This worker pings the /health endpoint of the API. * * Setup Instructions: * 1. Create a new Worker in the Cloudflare Dashboard. * 2. Paste this code into the editor. * 3. Go to 'Triggers' -> 'Cron Triggers'. * 4. Add a trigger for "Every Day" (e.g., `0 0 * * *`) or any interval under 48 hours. */
export default { async scheduled(event, env, ctx) { const TARGET_URL = "https://ysinghc-pastebin-api.hf.space/health";
console.log(`Pinging keep-alive endpoint: ${TARGET_URL}`);
try { const response = await fetch(TARGET_URL, { method: 'GET', headers: { 'User-Agent': 'Cloudflare-KeepAlive-Worker', }, });
if (response.ok) { console.log(`Successfully pinged API. Status: ${response.status}`); } else { console.error(`API responded with error. Status: ${response.status}`); } } catch (error) { console.error(`Error pinging API: ${error.message}`); } },};app/main.py+9
cloudflare-worker.jsAdded+35