Shlok Jain
Shlok Jain

Reputation: 31

Unable to run supabase edge functions locally

I have a simple edge functions script to run a prompt on the Gemini model and output back the result. Having used the debugger, the problem occurs at the code which listens to requests. The debugger mentions that there is a process already running at the port and hence there is an error.

Error at the terminal where I run the cURL command to post the request locally {"code":"BOOT_ERROR","message":"Worker failed to boot (please check logs)"}%

Error at the debugger

error: Uncaught (in promise) AddrInUse: Address already in use (os error 48)
    const listener = Deno.listen({
                          ^
    at Object.listen (ext:deno_net/01_net.js:534:35)
    at Server.listenAndServe (https://deno.land/[email protected]/http/server.ts:182:27)
    at serve (https://deno.land/[email protected]/http/server.ts:604:20)
    at startServer (file:///Users/shlokjain/Documents/dev/beacon/supabase/functions/essay-prompt/index.ts:9:19) /* point to serve function call */
    at file:///Users/shlokjain/Documents/dev/beacon/supabase/functions/essay-prompt/index.ts:66:1

code in the script

import { serve } from "https://deno.land/[email protected]/http/server.ts";

const portsToTry = [54321, 8080, 8081];

async function startServer() {
    for (const port of portsToTry) {
        try {
            await serve(async (req) => {
                // code

                return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json'} })
            }, { port })
            console.log(`Server listening on port ${port}`);
            break;
        } catch (err) {
            if (err instanceof Deno.errors.AddrInUse) {
                console.error(`Port ${port} is already in use`);
                continue;
            } else {
                throw err;
            }
        }
    }
}

startServer();
console.log("Hello from Functions!");

cURL command to request

curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/essay-prompt' \
    --header 'Authorization: Bearer API_KEY' \
    --header 'Content-Type: application/json' \
    --data '{"essayPrompt":"Functions"}'

Upvotes: 2

Views: 749

Answers (1)

Andrew Smith
Andrew Smith

Reputation: 1841

Why are you trying ports and logging the errors? you should not be using 54321 since this is the port your local Supabase setup runs on. Please take a look at example edge functions in the Supabase docs to see how they should be composed.

Upvotes: 0

Related Questions