Skip to content

Commit b026b32

Browse files
authored
Merge pull request #43 from Ajiet-DevNation/updated-ui
feat: add supabase keep-alive and dynamic upload bucket
2 parents e99139a + 84a0939 commit b026b32

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Keep-Alive
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * 0"
7+
8+
jobs:
9+
keep-alive:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
15+
- name: Generate random sleep duration
16+
id: random_sleep
17+
run: |
18+
RANDOM_SECONDS=$(( RANDOM % 1800 ))
19+
echo "Random sleep duration: ${RANDOM_SECONDS} seconds"
20+
echo "random_seconds=${RANDOM_SECONDS}" >> $GITHUB_OUTPUT
21+
22+
- name: Sleep for random duration
23+
run: sleep ${{ steps.random_sleep.outputs.random_seconds }}
24+
shell: bash
25+
26+
- name: Call keep-alive API on Vercel backend
27+
run: curl -X POST ${{ secrets.VERCEL_URL }}/api/keep-alive

src/app/api/keep-alive/route.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { supabaseAdmin } from "@/lib/supabase";
2+
import { NextResponse } from "next/server";
3+
4+
const generateRandomString = (sizeInBytes: number) => {
5+
const characters =
6+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7+
let result = "";
8+
const charactersLength = characters.length;
9+
for (let i = 0; i < sizeInBytes; i += 1) {
10+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
11+
}
12+
return result;
13+
};
14+
15+
export async function POST() {
16+
try {
17+
const fileSizeInBytes = 1024;
18+
const randomText = generateRandomString(fileSizeInBytes);
19+
const fileName = `keep-alive-${Date.now()}.txt`;
20+
21+
const { data, error } = await supabaseAdmin.storage
22+
.from(process.env.SUPABASE_TEXT_BUCKET!)
23+
.upload(`keep-alive/${fileName}`, randomText, {
24+
contentType: "text/plain",
25+
upsert: true,
26+
});
27+
28+
if (error) {
29+
console.error("Supabase upload error:", error);
30+
return NextResponse.json(
31+
{ msg: "Failed to upload file to Supabase", error: error.message },
32+
{ status: 500 }
33+
);
34+
}
35+
36+
return NextResponse.json({ msg: "Keep-alive file uploaded successfully", path: data.path });
37+
} catch (err) {
38+
console.error("Server error in keep-alive endpoint:", err);
39+
return NextResponse.json({ msg: "Server Error" }, { status: 500 });
40+
}
41+
}

src/app/api/upload/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function POST(req: Request) {
3434
// Upload to Supabase Storage
3535
// Ensure your bucket in Supabase is named 'images' and is set to Public
3636
const { error } = await supabaseAdmin.storage
37-
.from("project_images")
37+
.from(process.env.SUPABASE_IMAGE_BUCKET!)
3838
.upload(fileName, buffer, {
3939
contentType: file.type,
4040
upsert: false,
@@ -47,7 +47,7 @@ export async function POST(req: Request) {
4747

4848
// Get the Public URL
4949
const { data: { publicUrl } } = supabaseAdmin.storage
50-
.from("project_images")
50+
.from(process.env.SUPABASE_IMAGE_BUCKET!)
5151
.getPublicUrl(fileName);
5252

5353
return NextResponse.json({ url: publicUrl });

0 commit comments

Comments
 (0)