supabase edge function 빠르게 시작하기

notion database에서 값을 가져오고 싶었는데 생각해보니, client단에서 notion api call을 하면 CORS 에러가 났다. 그래서 Proxy server가 필요했고, lambda로 만들까 하다가 생각해보니 supabase에 edge function이라는 기능이 있었다. 그래서 이것을 사용해 봤다!

처음에 무척 애를 먹었다. supabase cli 설치가 마음대로 안됐다. 결국 유튜브 동영상을 보고 명령어를 찾았다.

brew install supabase/tap/supabase

설치를 하고 함수 생성한 뒤 바로 배포했다.

supabase functions new notion-api-proxy
supabase functions deploy notion-api-proxy --project-ref ${PROJECT_REF}

Edge functions 대시보드에 들어가면 배포한 함수가 짜잔! image

새로 생성한 함수는 functions directory 안에 들어 있다.

image

서버는 deno로 되어 있다. 와우. 한번도 안 써봤는데 잘 됐다. image

로컬로 돌려보기 위한 과정을 쭉 적어보면, docker가 필요했다.

 supabase functions serve notion-api-proxy
Error: cannot read config in /Users/songdonghoon/study: open supabase/config.toml: no such file or directory
Have you set up the project with supabase init?

 supabase init
Finished supabase init.

 supabase functions serve notion-api-proxy
Error: supabase start is not running.
Try rerunning the command with --debug to troubleshoot the error.

 supabase start
Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  in github.com/supabase/cli/internal/utils.AssertDockerIsRunning:48
  in github.com/supabase/cli/internal/start.Run:36
Try rerunning the command with --debug to troubleshoot the error.
Status: Downloaded newer image for public.ecr.aws/supabase/deno-relay:v1.6.0
Seeding data supabase/seed.sql...
Started supabase local development setup.

         API URL: http://localhost:54321
         ...

 supabase functions serve notion-api-proxy
Starting supabase/functions/notion-api-proxy
Serving supabase/functions/notion-api-proxy
Watcher Process started.
Check file:///home/deno/functions/notion-api-proxy/index.ts
Hello from Functions!
Listening on http://localhost:8000/

그리고 코드 아래에 있는 invoke 명령어를 사용하면 테스트 해볼 수 있었다.

curl --request POST 'https://<project_ref>.functions.supabase.co/hello-world' \ --header 'Authorization: Bearer ANON_KEY' \ --header 'Content-Type: application/json' \ --data '{ "name":"Functions" }'

참고

https://supabase.com/docs/guides/functions/quickstart


Written by@Donghoon Song
사람들의 꿈을 이어주는 코멘토에서 일하고 있습니다.

InstagramGitHubTwitterLinkedIn