Skip to content

Commit e63fef9

Browse files
cstocktonChris Stockton
andauthored
feat(conf): add JSON config file support (#2540)
Adds two new replace directives to `go.mod` which point to the newly created `internal/forks` directory: * `github.com/joho/godotenv` => `./internal/forks/godotenv` * `github.com/kelseyhightower/envconfig` => `./internal/forks/envconfig` Each one is clone of the version we use from the public repos with no additional changes made. This may be a repeatable pattern we could use to work around some limitations of older packages and allow reaching into internals to ease migrating away from them. --------- Co-authored-by: Chris Stockton <chris.stockton@supabase.io>
1 parent 7e6f2e4 commit e63fef9

55 files changed

Lines changed: 3646 additions & 584 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ www/.DS_Store
2020
www/node_modules
2121
npm-debug.log
2222
.data
23+
tmp

cmd/root_cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/sirupsen/logrus"
77
"github.com/spf13/cobra"
88
"github.com/supabase/auth/internal/conf"
9+
"github.com/supabase/auth/internal/conf/confload"
910
"github.com/supabase/auth/internal/observability"
1011
)
1112

@@ -35,7 +36,9 @@ func loadGlobalConfig(ctx context.Context) *conf.GlobalConfiguration {
3536
panic("context must not be nil")
3637
}
3738

38-
config, err := conf.LoadGlobal(configFile)
39+
configLoader := confload.NewLoader()
40+
41+
config, err := configLoader.Startup(configFile, watchDir)
3942
if err != nil {
4043
logrus.Fatalf("Failed to load configuration: %+v", err)
4144
}

cmd/serve_cmd.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/supabase/auth/internal/api/apilimiter"
1818
"github.com/supabase/auth/internal/api/apiworker"
1919
"github.com/supabase/auth/internal/conf"
20+
"github.com/supabase/auth/internal/conf/confload"
2021
"github.com/supabase/auth/internal/mailer/templatemailer"
2122
"github.com/supabase/auth/internal/reloader"
2223
"github.com/supabase/auth/internal/storage"
@@ -32,15 +33,9 @@ var serveCmd = cobra.Command{
3233
}
3334

3435
func serve(ctx context.Context) {
35-
if err := conf.LoadFile(configFile); err != nil {
36-
logrus.WithError(err).Fatal("unable to load config")
37-
}
36+
configLoader := confload.NewLoader()
3837

39-
if err := conf.LoadDirectory(watchDir); err != nil {
40-
logrus.WithError(err).Error("unable to load config from watch dir")
41-
}
42-
43-
config, err := conf.LoadGlobalFromEnv()
38+
config, err := configLoader.Startup(configFile, watchDir)
4439
if err != nil {
4540
logrus.WithError(err).Fatal("unable to load config")
4641
}
@@ -161,7 +156,10 @@ func serve(ctx context.Context) {
161156
previousLim = latestLim
162157
}
163158

164-
rl := reloader.NewReloader(rc, watchDir)
159+
rlFunc := func(dir string) (*conf.GlobalConfiguration, error) {
160+
return configLoader.Reload(dir)
161+
}
162+
rl := reloader.NewReloaderFunc(rc, watchDir, rlFunc)
165163
err = rl.Watch(ctx, fn)
166164
}()
167165
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,7 @@ require (
172172
)
173173

174174
go 1.25.11
175+
176+
replace (
177+
github.com/joho/godotenv => ./internal/forks/godotenv
178+
)

internal/api/api_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010
"github.com/supabase/auth/internal/api/apilimiter"
1111
"github.com/supabase/auth/internal/conf"
12+
"github.com/supabase/auth/internal/conf/confload"
1213
"github.com/supabase/auth/internal/crypto"
1314
"github.com/supabase/auth/internal/storage"
1415
"github.com/supabase/auth/internal/storage/test"
@@ -29,7 +30,7 @@ func init() {
2930
// Using this function allows us to keep track of the database connection
3031
// and cleaning up data between tests.
3132
func setupAPIForTest(opts ...Option) (*API, *conf.GlobalConfiguration, error) {
32-
config, err := conf.LoadGlobal(apiTestConfig)
33+
config, err := confload.LoadGlobal(apiTestConfig)
3334
if err != nil {
3435
return nil, nil, err
3536
}
@@ -43,7 +44,7 @@ func setupAPIForTest(opts ...Option) (*API, *conf.GlobalConfiguration, error) {
4344
}
4445

4546
func setupAPIForTestWithCallback(cb func(*conf.GlobalConfiguration, *storage.Connection)) (*API, *conf.GlobalConfiguration, error) {
46-
config, err := conf.LoadGlobal(apiTestConfig)
47+
config, err := confload.LoadGlobal(apiTestConfig)
4748
if err != nil {
4849
return nil, nil, err
4950
}

internal/api/errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/sirupsen/logrus"
1111
"github.com/stretchr/testify/require"
1212
"github.com/supabase/auth/internal/api/apierrors"
13-
"github.com/supabase/auth/internal/conf"
13+
"github.com/supabase/auth/internal/conf/confload"
1414
"github.com/supabase/auth/internal/observability"
1515
)
1616

@@ -71,7 +71,7 @@ func TestHandleResponseErrorWithHTTPError(t *testing.T) {
7171

7272
func TestRecoverer(t *testing.T) {
7373
var logBuffer bytes.Buffer
74-
config, err := conf.LoadGlobal(apiTestConfig)
74+
config, err := confload.LoadGlobal(apiTestConfig)
7575
require.NoError(t, err)
7676
require.NoError(t, observability.ConfigureLogging(&config.Logging))
7777

internal/api/oauthserver/authorize_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/supabase/auth/internal/api/apierrors"
2020
"github.com/supabase/auth/internal/api/shared"
2121
"github.com/supabase/auth/internal/conf"
22+
"github.com/supabase/auth/internal/conf/confload"
2223
"github.com/supabase/auth/internal/hooks/v0hooks"
2324
"github.com/supabase/auth/internal/models"
2425
"github.com/supabase/auth/internal/storage"
@@ -28,7 +29,7 @@ import (
2829

2930
func TestValidateRequestOrigin(t *testing.T) {
3031
// Setup test configuration
31-
globalConfig, err := conf.LoadGlobal(oauthServerTestConfig)
32+
globalConfig, err := confload.LoadGlobal(oauthServerTestConfig)
3233
require.NoError(t, err)
3334

3435
// Set up test site URL for validation
@@ -135,7 +136,7 @@ func TestValidateRequestOrigin(t *testing.T) {
135136
}
136137

137138
func TestValidateRequestOriginEdgeCases(t *testing.T) {
138-
globalConfig, err := conf.LoadGlobal(oauthServerTestConfig)
139+
globalConfig, err := confload.LoadGlobal(oauthServerTestConfig)
139140
require.NoError(t, err)
140141

141142
globalConfig.SiteURL = "https://example.com"
@@ -208,7 +209,7 @@ type OAuthAuthorizeTestSuite struct {
208209
}
209210

210211
func TestOAuthAuthorize(t *testing.T) {
211-
globalConfig, err := conf.LoadGlobal(oauthServerTestConfig)
212+
globalConfig, err := confload.LoadGlobal(oauthServerTestConfig)
212213
require.NoError(t, err)
213214

214215
conn, err := test.SetupDBConnection(globalConfig)

internal/api/oauthserver/handlers_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/suite"
1515
"github.com/supabase/auth/internal/api/shared"
1616
"github.com/supabase/auth/internal/conf"
17+
"github.com/supabase/auth/internal/conf/confload"
1718
"github.com/supabase/auth/internal/hooks/v0hooks"
1819
"github.com/supabase/auth/internal/models"
1920
"github.com/supabase/auth/internal/storage"
@@ -31,7 +32,7 @@ type OAuthClientTestSuite struct {
3132
}
3233

3334
func TestOAuthClientHandler(t *testing.T) {
34-
globalConfig, err := conf.LoadGlobal(oauthServerTestConfig)
35+
globalConfig, err := confload.LoadGlobal(oauthServerTestConfig)
3536
require.NoError(t, err)
3637

3738
conn, err := test.SetupDBConnection(globalConfig)

internal/api/oauthserver/service_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/require"
99
"github.com/stretchr/testify/suite"
1010
"github.com/supabase/auth/internal/conf"
11+
"github.com/supabase/auth/internal/conf/confload"
1112
"github.com/supabase/auth/internal/hooks/v0hooks"
1213
"github.com/supabase/auth/internal/models"
1314
"github.com/supabase/auth/internal/storage"
@@ -26,7 +27,7 @@ type OAuthServiceTestSuite struct {
2627
}
2728

2829
func TestOAuthService(t *testing.T) {
29-
globalConfig, err := conf.LoadGlobal(serviceTestConfig)
30+
globalConfig, err := confload.LoadGlobal(serviceTestConfig)
3031
require.NoError(t, err)
3132

3233
conn, err := test.SetupDBConnection(globalConfig)

internal/api/saml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
"github.com/crewjam/saml"
1212
"github.com/stretchr/testify/require"
13-
"github.com/supabase/auth/internal/conf"
13+
"github.com/supabase/auth/internal/conf/confload"
1414
)
1515

1616
func TestSAMLMetadataWithAPI(t *tst.T) {
17-
config, err := conf.LoadGlobal(apiTestConfig)
17+
config, err := confload.LoadGlobal(apiTestConfig)
1818
require.NoError(t, err)
1919
config.API.ExternalURL = "https://projectref.supabase.co/auth/v1/"
2020
config.SAML.Enabled = true

0 commit comments

Comments
 (0)