Skip to content

Commit d1858d3

Browse files
authored
Merge pull request #10 from aibtcdev/feat/add-supabase
Adds durable object for Supabase
2 parents 688db62 + 37c580b commit d1858d3

8 files changed

+421
-61
lines changed

package-lock.json

+135-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "aibtcdev-api-cache",
33
"version": "0.0.1",
4+
"type": "module",
45
"private": true,
56
"scripts": {
67
"deploy": "wrangler deploy",
@@ -12,5 +13,8 @@
1213
"@cloudflare/workers-types": "^4.20241106.0",
1314
"typescript": "^5.5.2",
1415
"wrangler": "^3.60.3"
16+
},
17+
"dependencies": {
18+
"@supabase/supabase-js": "^2.46.1"
1519
}
1620
}

src/config.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
export const APP_CONFIG = {
2-
// supported services for API caching
3-
// each entry is a durable object that handles requests
4-
SUPPORTED_SERVICES: ['/hiro-api'],
5-
// VALUES BELOW CAN BE OVERRIDDEN BY DURABLE OBJECTS
6-
// default cache TTL used for KV
7-
CACHE_TTL: 900, // 15 minutes
8-
// default rate limiting settings
9-
MAX_REQUESTS_PER_INTERVAL: 30, // no more than 30 requests
10-
INTERVAL_MS: 15000, // in a span of 15 seconds
11-
MAX_RETRIES: 3, // max retries for failed fetches
12-
RETRY_DELAY: 1000, // multiplied by retry attempt number
13-
// how often to warm the cache, should be shorter than the cache TTL
14-
ALARM_INTERVAL_MS: 300000, // 5 minutes
15-
};
1+
import { Env } from '../worker-configuration';
2+
3+
export class AppConfig {
4+
private static instance: AppConfig;
5+
private env: Env;
6+
7+
private constructor(env: Env) {
8+
this.env = env;
9+
}
10+
11+
public static getInstance(env?: Env): AppConfig {
12+
if (!AppConfig.instance && env) {
13+
AppConfig.instance = new AppConfig(env);
14+
} else if (!AppConfig.instance) {
15+
throw new Error('AppConfig must be initialized with environment variables first');
16+
}
17+
return AppConfig.instance;
18+
}
19+
20+
public getConfig() {
21+
22+
return {
23+
// supported services for API caching
24+
// each entry is a durable object that handles requests
25+
SUPPORTED_SERVICES: ['/hiro-api', '/supabase'],
26+
// VALUES BELOW CAN BE OVERRIDDEN BY DURABLE OBJECTS
27+
// default cache TTL used for KV
28+
CACHE_TTL: 900, // 15 minutes
29+
// default rate limiting settings
30+
MAX_REQUESTS_PER_INTERVAL: 30, // no more than 30 requests
31+
INTERVAL_MS: 15000, // in a span of 15 seconds
32+
MAX_RETRIES: 3, // max retries for failed fetches
33+
RETRY_DELAY: 1000, // multiplied by retry attempt number
34+
// how often to warm the cache, should be shorter than the cache TTL
35+
ALARM_INTERVAL_MS: 300000, // 5 minutes
36+
// environment variables
37+
SUPABASE_URL: this.env.SUPABASE_URL,
38+
SUPABASE_SERVICE_KEY: this.env.SUPABASE_SERVICE_KEY,
39+
};
40+
}
41+
}

src/durable-objects/hiro-api-do.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { DurableObject } from 'cloudflare:workers';
22
import { Env } from '../../worker-configuration';
3-
import { APP_CONFIG } from '../config';
3+
import { AppConfig } from '../config';
44
import { RateLimitedFetcher } from '../rate-limiter';
55

66
/**
77
* Durable Object class for the Hiro API
88
*/
99
export class HiroApiDO extends DurableObject<Env> {
1010
// can override values here for all endpoints
11-
private readonly CACHE_TTL: number = APP_CONFIG.CACHE_TTL;
12-
private readonly MAX_REQUESTS_PER_MINUTE = APP_CONFIG.MAX_REQUESTS_PER_INTERVAL;
13-
private readonly INTERVAL_MS = APP_CONFIG.INTERVAL_MS;
14-
private readonly MAX_RETRIES = APP_CONFIG.MAX_RETRIES;
15-
private readonly RETRY_DELAY = APP_CONFIG.RETRY_DELAY;
16-
private readonly ALARM_INTERVAL_MS = APP_CONFIG.ALARM_INTERVAL_MS;
11+
private readonly CACHE_TTL: number;
12+
private readonly MAX_REQUESTS_PER_MINUTE: number;
13+
private readonly INTERVAL_MS: number;
14+
private readonly MAX_RETRIES: number;
15+
private readonly RETRY_DELAY: number;
16+
private readonly ALARM_INTERVAL_MS: number;
1717
// settings specific to this Durable Object
1818
private readonly BASE_API_URL: string = 'https://api.hiro.so';
1919
private readonly BASE_PATH: string = '/hiro-api';
@@ -49,6 +49,18 @@ export class HiroApiDO extends DurableObject<Env> {
4949
super(ctx, env);
5050
this.ctx = ctx;
5151
this.env = env;
52+
53+
// Initialize AppConfig with environment
54+
const config = AppConfig.getInstance(env).getConfig();
55+
56+
// Set configuration values
57+
this.CACHE_TTL = config.CACHE_TTL;
58+
this.MAX_REQUESTS_PER_MINUTE = config.MAX_REQUESTS_PER_INTERVAL;
59+
this.INTERVAL_MS = config.INTERVAL_MS;
60+
this.MAX_RETRIES = config.MAX_RETRIES;
61+
this.RETRY_DELAY = config.RETRY_DELAY;
62+
this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS;
63+
5264
this.fetcher = new RateLimitedFetcher(
5365
this.env,
5466
this.BASE_API_URL,

0 commit comments

Comments
 (0)