Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds durable object for Supabase #10

Merged
merged 29 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8a37503
feat: Add Supabase Durable Object with stats endpoint and cache mecha…
whoabuddy Nov 12, 2024
c0f42b0
feat: Add Supabase Durable Object binding and update migrations
whoabuddy Nov 12, 2024
481470f
feat: install supabase sdk
whoabuddy Nov 12, 2024
79059ae
feat: Implement Supabase stats query with reusable function and inter…
whoabuddy Nov 12, 2024
e7f33b8
refactor: Replace mock data with actual Supabase stats fetching
whoabuddy Nov 12, 2024
84bb2e7
fix: Improve Supabase stats fetching and caching with error handling
whoabuddy Nov 12, 2024
7d392b7
refactor: Move Supabase configuration from env to APP_CONFIG
whoabuddy Nov 12, 2024
d38fec0
refactor: Update Supabase config keys and client initialization
whoabuddy Nov 12, 2024
f01ad2b
fix: intervene and actually fix the formatting
whoabuddy Nov 12, 2024
fab6b30
refactor: Update Supabase config to use environment variables
whoabuddy Nov 12, 2024
96776cc
chore: update types with cf tool
whoabuddy Nov 12, 2024
501fb76
fix: add route to call supabase do, fix types and vars
whoabuddy Nov 12, 2024
9e3976e
chore: formatting and export config
whoabuddy Nov 12, 2024
8165ba6
fix: intervene its an object not an array
whoabuddy Nov 12, 2024
6e95a52
fix: add function name from supabase dashboard
whoabuddy Nov 12, 2024
dd33bf4
refactor: Update Supabase config to use process.env and add Env import
whoabuddy Nov 12, 2024
d924463
refactor: Migrate configuration to singleton AppConfig pattern
whoabuddy Nov 12, 2024
6c29927
refactor: Update HiroApiDO to use AppConfig singleton pattern
whoabuddy Nov 12, 2024
52d2e3f
refactor: Update index to use AppConfig singleton and initialize with…
whoabuddy Nov 12, 2024
97e673b
fix: Correctly initialize AppConfig and access config after environme…
whoabuddy Nov 12, 2024
36d8f77
refactor: Improve AppConfig initialization and singleton pattern
whoabuddy Nov 12, 2024
be95c98
fix: Correct supportedServices reference in index.ts
whoabuddy Nov 12, 2024
e967a13
fix: small correction in format
whoabuddy Nov 12, 2024
97aec4f
fix: Update Supabase RPC call to properly handle stats retrieval
whoabuddy Nov 12, 2024
3c5e75e
fix: add error handling for stats
whoabuddy Nov 12, 2024
5e0bb7f
fix: Refactor Supabase client initialization with direct env variables
whoabuddy Nov 12, 2024
12dc871
fix: Update Supabase RPC call options to resolve stats fetching error
whoabuddy Nov 12, 2024
4489e0c
refactor: Improve Supabase RPC call with explicit typing and error ha…
whoabuddy Nov 12, 2024
37c580b
fix: Update Supabase RPC call to handle result data correctly
whoabuddy Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 135 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "aibtcdev-api-cache",
"version": "0.0.1",
"type": "module",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
Expand All @@ -12,5 +13,8 @@
"@cloudflare/workers-types": "^4.20241106.0",
"typescript": "^5.5.2",
"wrangler": "^3.60.3"
},
"dependencies": {
"@supabase/supabase-js": "^2.46.1"
}
}
56 changes: 41 additions & 15 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
export const APP_CONFIG = {
// supported services for API caching
// each entry is a durable object that handles requests
SUPPORTED_SERVICES: ['/hiro-api'],
// VALUES BELOW CAN BE OVERRIDDEN BY DURABLE OBJECTS
// default cache TTL used for KV
CACHE_TTL: 900, // 15 minutes
// default rate limiting settings
MAX_REQUESTS_PER_INTERVAL: 30, // no more than 30 requests
INTERVAL_MS: 15000, // in a span of 15 seconds
MAX_RETRIES: 3, // max retries for failed fetches
RETRY_DELAY: 1000, // multiplied by retry attempt number
// how often to warm the cache, should be shorter than the cache TTL
ALARM_INTERVAL_MS: 300000, // 5 minutes
};
import { Env } from '../worker-configuration';

export class AppConfig {
private static instance: AppConfig;
private env: Env;

private constructor(env: Env) {
this.env = env;
}

public static getInstance(env?: Env): AppConfig {
if (!AppConfig.instance && env) {
AppConfig.instance = new AppConfig(env);
} else if (!AppConfig.instance) {
throw new Error('AppConfig must be initialized with environment variables first');
}
return AppConfig.instance;
}

public getConfig() {

return {
// supported services for API caching
// each entry is a durable object that handles requests
SUPPORTED_SERVICES: ['/hiro-api', '/supabase'],
// VALUES BELOW CAN BE OVERRIDDEN BY DURABLE OBJECTS
// default cache TTL used for KV
CACHE_TTL: 900, // 15 minutes
// default rate limiting settings
MAX_REQUESTS_PER_INTERVAL: 30, // no more than 30 requests
INTERVAL_MS: 15000, // in a span of 15 seconds
MAX_RETRIES: 3, // max retries for failed fetches
RETRY_DELAY: 1000, // multiplied by retry attempt number
// how often to warm the cache, should be shorter than the cache TTL
ALARM_INTERVAL_MS: 300000, // 5 minutes
// environment variables
SUPABASE_URL: this.env.SUPABASE_URL,
SUPABASE_SERVICE_KEY: this.env.SUPABASE_SERVICE_KEY,
};
}
}
26 changes: 19 additions & 7 deletions src/durable-objects/hiro-api-do.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { DurableObject } from 'cloudflare:workers';
import { Env } from '../../worker-configuration';
import { APP_CONFIG } from '../config';
import { AppConfig } from '../config';
import { RateLimitedFetcher } from '../rate-limiter';

/**
* Durable Object class for the Hiro API
*/
export class HiroApiDO extends DurableObject<Env> {
// can override values here for all endpoints
private readonly CACHE_TTL: number = APP_CONFIG.CACHE_TTL;
private readonly MAX_REQUESTS_PER_MINUTE = APP_CONFIG.MAX_REQUESTS_PER_INTERVAL;
private readonly INTERVAL_MS = APP_CONFIG.INTERVAL_MS;
private readonly MAX_RETRIES = APP_CONFIG.MAX_RETRIES;
private readonly RETRY_DELAY = APP_CONFIG.RETRY_DELAY;
private readonly ALARM_INTERVAL_MS = APP_CONFIG.ALARM_INTERVAL_MS;
private readonly CACHE_TTL: number;
private readonly MAX_REQUESTS_PER_MINUTE: number;
private readonly INTERVAL_MS: number;
private readonly MAX_RETRIES: number;
private readonly RETRY_DELAY: number;
private readonly ALARM_INTERVAL_MS: number;
// settings specific to this Durable Object
private readonly BASE_API_URL: string = 'https://api.hiro.so';
private readonly BASE_PATH: string = '/hiro-api';
Expand Down Expand Up @@ -49,6 +49,18 @@ export class HiroApiDO extends DurableObject<Env> {
super(ctx, env);
this.ctx = ctx;
this.env = env;

// Initialize AppConfig with environment
const config = AppConfig.getInstance(env).getConfig();

// Set configuration values
this.CACHE_TTL = config.CACHE_TTL;
this.MAX_REQUESTS_PER_MINUTE = config.MAX_REQUESTS_PER_INTERVAL;
this.INTERVAL_MS = config.INTERVAL_MS;
this.MAX_RETRIES = config.MAX_RETRIES;
this.RETRY_DELAY = config.RETRY_DELAY;
this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS;

this.fetcher = new RateLimitedFetcher(
this.env,
this.BASE_API_URL,
Expand Down
Loading