generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add database-backed config resolver + provider for production (#…
…1570) Fixes #1548 This doesn't hook anything up yet - just adds the provider and resolver that we'll use in prod Changes: * Adds `DBConfig[Resolver|Provider]` to the `configuration` package, where the resolver does very little, and the provider executes most of the actual database SQL queries. For DB-backed configs, all the data needed to retrieve a config can be found in the `ref` without a separate key, so the URLs are all simple `db://` stubs that exist only to satisfy the existing provider and resolver interfaces, which expect URLs to be passed as keys. * Refactors the 3 Err types, `isNotFound`, and `translatePGError` out of the `dal` package into a separate `dalerrors` * Adds a public getter for the `db` field in `dal.DAL` so that we can connect to the same DB in the database config resolver/provider
- Loading branch information
Showing
11 changed files
with
429 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package configuration | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/TBD54566975/ftl/backend/controller/dal" | ||
"github.com/alecthomas/types/optional" | ||
) | ||
|
||
// DBConfigProvider is a configuration provider that stores configuration in its key. | ||
type DBConfigProvider struct { | ||
dal DBConfigProviderDAL | ||
} | ||
|
||
type DBConfigProviderDAL interface { | ||
GetModuleConfiguration(ctx context.Context, module optional.Option[string], name string) ([]byte, error) | ||
SetModuleConfiguration(ctx context.Context, module optional.Option[string], name string, value []byte) error | ||
UnsetModuleConfiguration(ctx context.Context, module optional.Option[string], name string) error | ||
} | ||
|
||
var _ MutableProvider[Configuration] = DBConfigProvider{} | ||
|
||
func NewDBConfigProvider(dal DBConfigProviderDAL) DBConfigProvider { | ||
return DBConfigProvider{ | ||
dal: dal, | ||
} | ||
} | ||
|
||
func (DBConfigProvider) Role() Configuration { return Configuration{} } | ||
func (DBConfigProvider) Key() string { return "db" } | ||
func (DBConfigProvider) Writer() bool { return true } | ||
|
||
func (d DBConfigProvider) Load(ctx context.Context, ref Ref, key *url.URL) ([]byte, error) { | ||
value, err := d.dal.GetModuleConfiguration(ctx, ref.Module, ref.Name) | ||
if err != nil { | ||
return nil, dal.ErrNotFound | ||
} | ||
return value, nil | ||
} | ||
|
||
func (d DBConfigProvider) Store(ctx context.Context, ref Ref, value []byte) (*url.URL, error) { | ||
err := d.dal.SetModuleConfiguration(ctx, ref.Module, ref.Name, value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &url.URL{Scheme: "db"}, nil | ||
} | ||
|
||
func (d DBConfigProvider) Delete(ctx context.Context, ref Ref) error { | ||
return d.dal.UnsetModuleConfiguration(ctx, ref.Module, ref.Name) | ||
} |
Oops, something went wrong.