Skip to content

Commit 8aaeb4a

Browse files
authored
Merge branch 'main' into kartikk/fix
2 parents ba2b2da + b389838 commit 8aaeb4a

File tree

8 files changed

+131
-17
lines changed

8 files changed

+131
-17
lines changed

cmd/functions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,23 @@ var (
9595
}
9696

9797
envFilePath string
98+
serveAll bool
9899

99100
functionsServeCmd = &cobra.Command{
100101
Use: "serve <Function name>",
101102
Short: "Serve a Function locally",
102-
Args: cobra.ExactArgs(1),
103+
Args: cobra.RangeArgs(0, 1),
103104
RunE: func(cmd *cobra.Command, args []string) error {
104105
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
105106
// Fallback to config if user did not set the flag.
106107
if !cmd.Flags().Changed("no-verify-jwt") {
107108
noVerifyJWT = nil
108109
}
109-
return serve.Run(ctx, args[0], envFilePath, noVerifyJWT, importMapPath, afero.NewOsFs())
110+
slug := ""
111+
if len(args) > 0 {
112+
slug = args[0]
113+
}
114+
return serve.Run(ctx, slug, envFilePath, noVerifyJWT, importMapPath, serveAll, afero.NewOsFs())
110115
},
111116
}
112117
)
@@ -120,6 +125,7 @@ func init() {
120125
functionsServeCmd.Flags().BoolVar(noVerifyJWT, "no-verify-jwt", false, "Disable JWT verification for the Function.")
121126
functionsServeCmd.Flags().StringVar(&envFilePath, "env-file", "", "Path to an env file to be populated to the Function environment.")
122127
functionsServeCmd.Flags().StringVar(&importMapPath, "import-map", "", "Path to import map file.")
128+
functionsServeCmd.Flags().BoolVar(&serveAll, "all", false, "Serve all functions (caution: Experimental feature)")
123129
functionsDownloadCmd.Flags().StringVar(&projectRef, "project-ref", "", "Project ref of the Supabase project.")
124130
functionsCmd.AddCommand(functionsDeleteCmd)
125131
functionsCmd.AddCommand(functionsDeployCmd)

internal/db/diff/diff.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func DiffSchema(ctx context.Context, source, target string, schema []string, p u
8282
utils.DifferImage,
8383
nil,
8484
args,
85+
nil,
86+
"",
8587
stream.Stdout(),
8688
stream.Stderr(),
8789
); err != nil {
@@ -95,6 +97,8 @@ func DiffSchema(ctx context.Context, source, target string, schema []string, p u
9597
utils.DifferImage,
9698
nil,
9799
append([]string{"--schema", s}, args...),
100+
nil,
101+
"",
98102
stream.Stdout(),
99103
stream.Stderr(),
100104
); err != nil {

internal/db/start/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func StartDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...f
4545
Env: []string{
4646
"POSTGRES_PASSWORD=postgres",
4747
"POSTGRES_HOST=/var/run/postgresql",
48+
"LC_ALL=C.UTF-8",
4849
},
4950
Healthcheck: &container.HealthConfig{
5051
Test: []string{"CMD", "pg_isready", "-U", "postgres", "-h", "localhost", "-p", "5432"},

internal/functions/serve/serve.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/docker/pkg/stdcopy"
1515
"github.com/joho/godotenv"
1616
"github.com/spf13/afero"
17+
"github.com/spf13/viper"
1718
"github.com/supabase/cli/internal/utils"
1819
)
1920

@@ -40,7 +41,11 @@ func ParseEnvFile(envFilePath string) ([]string, error) {
4041
return env, nil
4142
}
4243

43-
func Run(ctx context.Context, slug string, envFilePath string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
44+
func Run(ctx context.Context, slug string, envFilePath string, noVerifyJWT *bool, importMapPath string, serveAll bool, fsys afero.Fs) error {
45+
if serveAll {
46+
return runServeAll(ctx, envFilePath, noVerifyJWT, importMapPath, fsys)
47+
}
48+
4449
// 1. Sanity checks.
4550
{
4651
if err := utils.LoadConfigFS(fsys); err != nil {
@@ -216,3 +221,94 @@ func Run(ctx context.Context, slug string, envFilePath string, noVerifyJWT *bool
216221
fmt.Println("Stopped serving " + utils.Bold(localFuncDir))
217222
return nil
218223
}
224+
225+
func runServeAll(ctx context.Context, envFilePath string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
226+
// 1. Sanity checks.
227+
{
228+
if err := utils.LoadConfigFS(fsys); err != nil {
229+
return err
230+
}
231+
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
232+
return err
233+
}
234+
if envFilePath != "" {
235+
if _, err := fsys.Stat(envFilePath); err != nil {
236+
return fmt.Errorf("Failed to read env file: %w", err)
237+
}
238+
}
239+
if importMapPath != "" {
240+
// skip
241+
} else if f, err := fsys.Stat(utils.FallbackImportMapPath); err == nil && !f.IsDir() {
242+
importMapPath = utils.FallbackImportMapPath
243+
}
244+
if importMapPath != "" {
245+
if _, err := fsys.Stat(importMapPath); err != nil {
246+
return fmt.Errorf("Failed to read import map: %w", err)
247+
}
248+
}
249+
}
250+
251+
// 2. Parse user defined env
252+
userEnv, err := ParseEnvFile(envFilePath)
253+
if err != nil {
254+
return err
255+
}
256+
257+
// 3. Start container
258+
{
259+
_ = utils.Docker.ContainerRemove(ctx, utils.DenoRelayId, types.ContainerRemoveOptions{
260+
RemoveVolumes: true,
261+
Force: true,
262+
})
263+
264+
env := []string{
265+
"JWT_SECRET=" + utils.JWTSecret,
266+
"SUPABASE_URL=http://" + utils.KongId + ":8000",
267+
"SUPABASE_ANON_KEY=" + utils.AnonKey,
268+
"SUPABASE_SERVICE_ROLE_KEY=" + utils.ServiceRoleKey,
269+
"SUPABASE_DB_URL=postgresql://postgres:postgres@localhost:" + strconv.FormatUint(uint64(utils.Config.Db.Port), 10) + "/postgres",
270+
}
271+
verifyJWTEnv := "VERIFY_JWT=true"
272+
if noVerifyJWT != nil {
273+
verifyJWTEnv = "VERIFY_JWT=false"
274+
}
275+
env = append(env, verifyJWTEnv)
276+
277+
cwd, err := os.Getwd()
278+
if err != nil {
279+
return err
280+
}
281+
282+
binds := []string{
283+
filepath.Join(cwd, utils.FunctionsDir) + ":" + relayFuncDir + ":ro,z",
284+
utils.DenoRelayId + ":/root/.cache/deno:rw,z",
285+
}
286+
// If a import map path is explcitly provided, mount it as a separate file
287+
if importMapPath != "" {
288+
binds = append(binds, filepath.Join(cwd, importMapPath)+":"+customDockerImportMapPath+":ro,z")
289+
}
290+
291+
fmt.Println("Serving " + utils.Bold(utils.FunctionsDir))
292+
293+
cmd := []string{"start", "--dir", relayFuncDir, "-p", "8081"}
294+
if viper.GetBool("DEBUG") {
295+
cmd = append(cmd, "--verbose")
296+
}
297+
if err := utils.DockerRunOnceWithStream(
298+
ctx,
299+
utils.EdgeRuntimeImage,
300+
append(env, userEnv...),
301+
cmd,
302+
binds,
303+
utils.DenoRelayId,
304+
os.Stdout,
305+
os.Stderr,
306+
); err != nil {
307+
return err
308+
}
309+
}
310+
311+
fmt.Println("Stopped serving " + utils.Bold(utils.FunctionsDir))
312+
return nil
313+
314+
}

internal/functions/serve/serve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestServeCommand(t *testing.T) {
3535
Post("/v" + utils.Docker.ClientVersion() + "/containers").
3636
Reply(http.StatusServiceUnavailable)
3737
// Run test
38-
err := Run(context.Background(), "test-func", "", nil, "", fsys)
38+
err := Run(context.Background(), "test-func", "", nil, "", false, fsys)
3939
// Check error
4040
assert.ErrorContains(t, err, "request returned Service Unavailable for API route and version http://localhost/v1.41/containers/supabase_deno_relay_serve/exec")
4141
assert.Empty(t, apitest.ListUnmatchedRequests())

internal/start/start.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ EOF
175175

176176
"GOTRUE_EXTERNAL_PHONE_ENABLED=true",
177177
"GOTRUE_SMS_AUTOCONFIRM=true",
178+
179+
"GOTRUE_SECURITY_REFRESH_TOKEN_ROTATION_ENABLED=false",
178180
}
179181

180182
for name, config := range utils.Config.Auth.External {

internal/utils/docker.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,23 @@ func DockerRunOnce(ctx context.Context, image string, env []string, cmd []string
302302
stderr = os.Stderr
303303
}
304304
var out bytes.Buffer
305-
err := DockerRunOnceWithStream(ctx, image, env, cmd, &out, stderr)
305+
err := DockerRunOnceWithStream(ctx, image, env, cmd, nil, "", &out, stderr)
306306
return out.String(), err
307307
}
308308

309-
func DockerRunOnceWithStream(ctx context.Context, image string, env []string, cmd []string, stdout, stderr io.Writer) error {
309+
func DockerRunOnceWithStream(ctx context.Context, image string, env, cmd, binds []string, containerName string, stdout, stderr io.Writer) error {
310310
// Cannot rely on docker's auto remove because
311311
// 1. We must inspect exit code after container stops
312312
// 2. Context cancellation may happen after start
313313
container, err := DockerStart(ctx, container.Config{
314314
Image: image,
315315
Env: env,
316316
Cmd: cmd,
317-
}, container.HostConfig{}, "")
317+
}, container.HostConfig{
318+
Binds: binds,
319+
// Allows containerized functions on Linux to reach host OS
320+
ExtraHosts: []string{"host.docker.internal:host-gateway"},
321+
}, containerName)
318322
if err != nil {
319323
return err
320324
}

internal/utils/misc.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@ const (
2828
Pg14Image = "supabase/postgres:14.1.0.89"
2929
Pg15Image = "supabase/postgres:15.1.0.33"
3030
// Append to ServiceImages when adding new dependencies below
31-
KongImage = "library/kong:2.8.1"
32-
InbucketImage = "inbucket/inbucket:3.0.3"
33-
PostgrestImage = "postgrest/postgrest:v10.1.1.20221215"
34-
DifferImage = "supabase/pgadmin-schema-diff:cli-0.0.5"
35-
MigraImage = "djrobstep/migra:3.0.1621480950"
36-
PgmetaImage = "supabase/postgres-meta:v0.60.7"
37-
StudioImage = "supabase/studio:20230127-6bfd87b"
38-
DenoRelayImage = "supabase/deno-relay:v1.5.0"
39-
ImageProxyImage = "darthsim/imgproxy:v3.8.0"
31+
KongImage = "library/kong:2.8.1"
32+
InbucketImage = "inbucket/inbucket:3.0.3"
33+
PostgrestImage = "postgrest/postgrest:v10.1.1.20221215"
34+
DifferImage = "supabase/pgadmin-schema-diff:cli-0.0.5"
35+
MigraImage = "djrobstep/migra:3.0.1621480950"
36+
PgmetaImage = "supabase/postgres-meta:v0.60.7"
37+
StudioImage = "supabase/studio:20230127-6bfd87b"
38+
DenoRelayImage = "supabase/deno-relay:v1.5.0"
39+
ImageProxyImage = "darthsim/imgproxy:v3.8.0"
40+
EdgeRuntimeImage = "supabase/edge-runtime:v1.0.9"
4041
// Update initial schemas in internal/utils/templates/initial_schemas when
4142
// updating any one of these.
4243
GotrueImage = "supabase/gotrue:v2.40.1"
43-
RealtimeImage = "supabase/realtime:v2.1.0"
44+
RealtimeImage = "supabase/realtime:v2.4.0"
4445
StorageImage = "supabase/storage-api:v0.26.1"
4546
// Should be kept in-sync with DenoRelayImage
4647
DenoVersion = "1.28.0"

0 commit comments

Comments
 (0)