Skip to content

Commit c350a4f

Browse files
author
thorwebdev
committed
feat: add upstash redis.
1 parent 5f55023 commit c350a4f

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ Find more [Supabase Edge Functions Examples here](https://supabase.com/docs/guid
1111
## Episodes
1212

1313
1. [ [Video](https://youtu.be/jZgyOJGWayQ) | [Code](./supabase/functions/og-image/) ] Generating OG Images
14+
1. [ [Video](https://www.youtube.com/watch?v=wW6L52v9Ldo) | [Code](./supabase/functions/og-image-storage-cdn/) ] Caching OG Images with Storage CDN
15+
1. [ [Video](https://www.youtube.com/watch?v=l2KlzGrhB6w) | [Code](./.github/workflows/deploy.yaml) ] Deploying from GitHub Actions
16+
1. [ [Video](https://www.youtube.com/watch?v=6OMVWiiycLs) | [Code](./supabase/functions/stripe-webhook/) ] Handling Stripe Webhooks
17+
1. [ [Video](https://www.youtube.com/watch?v=AWfE3a9J_uo) | [Code](./supabase/functions/telegram-bot/) ] Building a Telegram Bot
18+
1. [ [Video](https://www.youtube.com/watch?v=OwW0znboh60) | [Code](./supabase/functions/cloudflare-turnstile/) ] Better CAPTCHA with Cloudflare Turnstile
19+
1. [ [Video](https://www.youtube.com/watch?v=J24Bvo_m7DM) | [Code](./supabase/functions/discord-bot/) ] Creating Discord Slash Commands
20+
1. [ [Video](https://www.youtube.com/watch?v=ILr3cneZuFk) | [Code](./supabase/functions/import_map.json) ] Import Maps
21+
1. [ [Video](https://www.youtube.com/watch?v=cl7EuF1-RsY) | [Code](./supabase/functions/postgres-on-the-edge/) ] Postgres on the Edge
22+
1. [ [Video](https://www.youtube.com/watch?v=-U6DJcjVvGo) | [Docs](https://supabase.com/docs/guides/functions/schedule-functions) ] Scheduling Functions
23+
1. [ [Video](https://youtu.be/F505n0d9iuA) | [Code](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/postgres-on-the-edge) ] Caching Postgres on the Edge with PolyScale.ai
24+
1. [ [Video](https://youtu.be/29p8kIqyU_Y) | [Code](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/openai) ] OpenAI with Edge Functions

supabase/functions/import_map.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"oak": "https://deno.land/x/[email protected]/mod.ts",
55
"grammy": "https://deno.land/x/[email protected]/mod.ts",
66
"react": "https://esm.sh/[email protected]",
7-
"std/server": "https://deno.land/std@0.168.0/http/server.ts",
7+
"std/server": "https://deno.land/std@0.177.0/http/server.ts",
88
"stripe": "https://esm.sh/[email protected]?target=deno",
99
"sift": "https://deno.land/x/[email protected]/mod.ts",
1010
"@supabase/supabase-js": "https://esm.sh/@supabase/[email protected]",
11-
"postgres": "https://deno.land/x/[email protected]/mod.ts"
11+
"postgres": "https://deno.land/x/[email protected]/mod.ts",
12+
"Redis": "https://deno.land/x/[email protected]/mod.ts"
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { serve } from "std/server";
2+
import { Redis } from "Redis";
3+
console.log(`Function "upstash-redis-counter" up and running!`);
4+
serve(async (_req) => {
5+
try {
6+
const redis = new Redis({
7+
url: Deno.env.get("UPSTASH_REDIS_REST_URL")!,
8+
token: Deno.env.get("UPSTASH_REDIS_REST_TOKEN")!,
9+
});
10+
const deno_region = Deno.env.get("DENO_REGION");
11+
if (deno_region) {
12+
// Increment region counter
13+
await redis.hincrby("supa-edge-counter", deno_region, 1);
14+
} else {
15+
// Increment localhost counter
16+
await redis.hincrby("supa-edge-counter", "localhost", 1);
17+
}
18+
// Get all values
19+
const counterHash: Record<string, number> | null = await redis.hgetall(
20+
"supa-edge-counter"
21+
);
22+
const counters = Object.entries(counterHash!)
23+
.sort(([, a], [, b]) => b - a) // sort desc
24+
.reduce(
25+
(r, [k, v]) => ({
26+
total: r.total + v,
27+
regions: { ...r.regions, [k]: v },
28+
}),
29+
{
30+
total: 0,
31+
regions: {},
32+
}
33+
);
34+
return new Response(JSON.stringify({ counters }), { status: 200 });
35+
} catch (error) {
36+
return new Response(JSON.stringify({ error: error.message }), {
37+
status: 200,
38+
});
39+
}
40+
});

0 commit comments

Comments
 (0)