add working signup + login
This commit is contained in:
parent
bc85b7c340
commit
d8cb8c8fa8
27 changed files with 600 additions and 39 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
|
||||
"emperror.dev/errors"
|
||||
"git.sleepycat.moe/sam/mercury/internal/database"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/oklog/ulid/v2"
|
||||
)
|
||||
|
@ -24,9 +25,55 @@ func NewBase(ctx context.Context, connString string) (*Base, error) {
|
|||
base := &Base{
|
||||
pool: pool,
|
||||
}
|
||||
|
||||
// create configuration
|
||||
if err := base.initSingletons(ctx); err != nil {
|
||||
return nil, errors.Wrap(err, "initializing configuration")
|
||||
}
|
||||
|
||||
return base, nil
|
||||
}
|
||||
|
||||
func (base *Base) initSingletons(ctx context.Context) error {
|
||||
err := NewConfigStore(base.pool).initConfig(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "initializing configuration")
|
||||
}
|
||||
|
||||
cfg, err := NewConfigStore(base.pool).Get(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting configuration")
|
||||
}
|
||||
|
||||
if cfg.InternalApplication == nil {
|
||||
tx, err := base.BeginTx(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating transaction")
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
app, err := NewTokenStore(tx).CreateApplication(ctx, database.InternalApplicationName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating internal application")
|
||||
}
|
||||
|
||||
newCfg := cfg
|
||||
newCfg.InternalApplication = &app.ID
|
||||
|
||||
cfg, err = NewConfigStore(tx).Set(ctx, cfg, newCfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "updating configuration")
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "committing transaction")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Acquire acquires a connection from the database pool.
|
||||
// It is the caller's responsibility to call the Release method.
|
||||
func (base *Base) Acquire(ctx context.Context) (ReleaseableQuerier, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue