agentgate / tree / service/src/worker.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Cloudflare Workers entry (wrangler deploy). Vars come from wrangler.toml [vars].
// publicBaseHandler: requests from the api.youraitools.co proxy (allowlisted X-Forwarded-Host + this Worker's
// X-Forwarded-Prefix) get an app whose public URLs are https://api.youraitools.co/<prefix>; all other requests get
// the unchanged workers.dev app. See publicbase.js.
import { createApp } from './app.js';
import { configFromEnv } from './config.js';
import { watchdog } from './deadline.js';
import { Stats } from './stats.js';
import { publicBaseHandler } from './publicbase.js';
import { x402TrustResponse } from './x402trust.js';
export { StatsCounter } from './counter-do.js'; // usage counters (SQLite Durable Object, no KV writes)
let handler;
export default {
  // watchdog: a last-resort 503 (never charged) instead of a hung request; see deadline.js for the budgets.
  fetch: watchdog((request, env, ctx) => {
    const trust = x402TrustResponse(request, env); // x402-trust.com domain proof (public key)
    if (trust) return trust;
    if (!handler) {
      const stats = new Stats(env.STATS, { counter: env.COUNTER }); // one counter batch shared by both app instances
      handler = publicBaseHandler(env, (publicUrl) => createApp({ ...configFromEnv(env), stats, publicUrl }));
    }
    return handler(request, env, ctx);
  }),
};

agentgate-source: code mirror on Cloudflare Pages. All repos