diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a133f27bb..130f5e46a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -5,7 +5,6 @@ on: branches: - main paths: - - "worker/**" - "web/**" - "src/**" - "wrangler.jsonc" diff --git a/hk.pkl b/hk.pkl index 4109c3c03..7dfe73836 100644 --- a/hk.pkl +++ b/hk.pkl @@ -8,7 +8,7 @@ local linters = new Mapping { stage = "*" } ["tsc"] { - glob = List("src/**/*.ts", "worker/**/*.ts") + glob = List("src/**/*.ts") check = "bunx tsc --noEmit" batch = true } diff --git a/scripts/github-token.js b/scripts/github-token.js index 5ecc64a9e..fcff185d4 100755 --- a/scripts/github-token.js +++ b/scripts/github-token.js @@ -3,8 +3,7 @@ /** * GitHub Token Manager Helper for GitHub Actions * - * This script fetches a GitHub token from the token manager API - * and can optionally record usage for rate limit tracking. + * This script fetches a GitHub token from the token manager API. * * Usage in GitHub Actions: * @@ -25,7 +24,6 @@ import https from "https"; import http from "http"; -import fs from "fs"; async function makeRequest(url, options = {}) { return new Promise((resolve, reject) => { @@ -69,45 +67,6 @@ async function makeRequest(url, options = {}) { }); } -async function recordUsage(baseUrl, secret, tokenId, endpoint, rateLimitInfo) { - const usageUrl = `${baseUrl}/api/token/usage`; - - return new Promise((resolve, reject) => { - const urlObj = new URL(usageUrl); - const client = urlObj.protocol === "https:" ? https : http; - - const payload = JSON.stringify({ - token_id: tokenId, - endpoint, - remaining_requests: rateLimitInfo?.remaining, - reset_at: rateLimitInfo?.reset - ? new Date(rateLimitInfo.reset * 1000).toISOString() - : undefined, - }); - - const req = client.request( - usageUrl, - { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${secret}`, - "Content-Length": Buffer.byteLength(payload), - }, - }, - (res) => { - let data = ""; - res.on("data", (chunk) => (data += chunk)); - res.on("end", () => resolve({ status: res.statusCode, data })); - }, - ); - - req.on("error", reject); - req.write(payload); - req.end(); - }); -} - async function markRateLimited(baseUrl, secret, tokenId, resetTime) { const rateLimitUrl = `${baseUrl}/api/token/rate-limit`; @@ -212,7 +171,7 @@ async function main() { console.error(` Total tokens: ${response.data.total}`); } else { console.error( - "❌ Unknown action. Available actions: get-token, record-usage, mark-rate-limited, stats", + "❌ Unknown action. Available actions: get-token, mark-rate-limited, stats", ); process.exit(1); } @@ -227,4 +186,4 @@ if (import.meta.url === `file://${process.argv[1]}`) { main(); } -export { makeRequest, recordUsage, markRateLimited }; +export { makeRequest, markRateLimited }; diff --git a/tsconfig.json b/tsconfig.json index bce6b1152..a465c8085 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -112,6 +112,6 @@ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, - "include": ["src/**/*", "worker/**/*"], + "include": ["src/**/*"], "exclude": ["web", "node_modules", "dist", "scripts"] } diff --git a/web/wrangler.jsonc b/web/wrangler.jsonc deleted file mode 100644 index ebff4c707..000000000 --- a/web/wrangler.jsonc +++ /dev/null @@ -1,32 +0,0 @@ -{ - "$schema": "node_modules/wrangler/config-schema.json", - "name": "mise-tools-web", - "compatibility_date": "2025-04-18", - "compatibility_flags": ["nodejs_compat"], - "d1_databases": [ - { - "binding": "DB", - "database_name": "mise-versions", - "database_id": "c484af77-97c7-4d1d-b9d0-0d7743c6f4e5", - }, - { - "binding": "ANALYTICS_DB", - "database_name": "mise-tools-track", - "database_id": "21a8b89a-c2cc-4a8a-9805-b4bcfcd4f6c8", - }, - ], - "kv_namespaces": [ - { - "binding": "GITHUB_CACHE", - "id": "4f4aae307f5e4dfda604e16f239a047b", - }, - { - "binding": "DOWNLOAD_DEDUPE", - "id": "812c4a213f404e4badedfc04d63c1ef9", - }, - { - "binding": "SESSION", - "id": "placeholder-session-kv", - }, - ], -} diff --git a/worker/index.ts b/worker/index.ts deleted file mode 100644 index 654d6a17a..000000000 --- a/worker/index.ts +++ /dev/null @@ -1,113 +0,0 @@ -// Cloudflare Worker entry point -// Wraps Astro's generated worker and adds scheduled handler support -import { drizzle } from "drizzle-orm/d1"; -import { runMigrations } from "../src/migrations.js"; -import { - runAnalyticsMigrations, - setupAnalytics, -} from "../src/analytics/index.js"; - -// Type for environment bindings -interface Env { - DB: D1Database; - ANALYTICS_DB: D1Database; - ASSETS: Fetcher; - GITHUB_CACHE: KVNamespace; - GITHUB_APP_ID: string; - GITHUB_PRIVATE_KEY: string; - GITHUB_CLIENT_ID: string; - GITHUB_CLIENT_SECRET: string; - GITHUB_WEBHOOK_SECRET: string; - API_SECRET: string; -} - -let migrationsCompleted = false; - -// Run migrations once -async function ensureMigrations(env: Env): Promise { - if (migrationsCompleted) return; - - try { - console.log("Running database migrations..."); - const db = drizzle(env.DB); - await runMigrations(db); - - // Run analytics database migrations - const analyticsDb = drizzle(env.ANALYTICS_DB); - await runAnalyticsMigrations(analyticsDb); - - migrationsCompleted = true; - console.log("Migrations completed"); - } catch (error) { - console.error("Migration error:", error); - } -} - -// Dynamically import Astro's handler (built by Astro at build time) -async function getAstroHandler() { - // @ts-expect-error - This path is generated at build time by Astro - const astroModule = await import("../web/dist/_worker.js/index.js"); - return astroModule.default; -} - -export default { - // Forward fetch requests to Astro - async fetch( - request: Request, - env: Env, - ctx: ExecutionContext, - ): Promise { - // Run migrations once on first request - await ensureMigrations(env); - - // Forward to Astro's handler - const astroApp = await getAstroHandler(); - return astroApp.fetch(request, env, ctx); - }, - - // Cron trigger for daily aggregation - async scheduled( - _event: ScheduledEvent, - env: Env, - _ctx: ExecutionContext, - ): Promise { - console.log("Running scheduled tasks..."); - - // Ensure migrations are run - await ensureMigrations(env); - - const analyticsDb = drizzle(env.ANALYTICS_DB); - const analytics = setupAnalytics(analyticsDb); - - // 1. Aggregate old data (data older than 90 days) - const aggregateResult = await analytics.aggregateOldData(); - console.log( - `Aggregation complete: ${aggregateResult.aggregated} groups aggregated, ${aggregateResult.deleted} rows deleted`, - ); - - // 2. Populate rollup tables for yesterday (and today so far) - const now = new Date(); - const yesterday = new Date(now); - yesterday.setUTCDate(yesterday.getUTCDate() - 1); - const yesterdayStr = yesterday.toISOString().split("T")[0]; - const todayStr = now.toISOString().split("T")[0]; - - // Populate yesterday's full data - const yesterdayResult = await analytics.populateRollupTables( - yesterdayStr, - env.ANALYTICS_DB, - ); - console.log( - `Rollup tables populated for ${yesterdayStr}: ${yesterdayResult.toolStats} tools, ${yesterdayResult.backendStats} backends`, - ); - - // Also update today's partial data - const todayResult = await analytics.populateRollupTables( - todayStr, - env.ANALYTICS_DB, - ); - console.log( - `Rollup tables updated for ${todayStr}: ${todayResult.toolStats} tools, ${todayResult.backendStats} backends`, - ); - }, -};