bouncer/internal/setup/repository.go

44 lines
1019 B
Go

package setup
import (
"context"
"database/sql"
"net/url"
"forge.cadoles.com/cadoles/bouncer/internal/config"
"forge.cadoles.com/cadoles/bouncer/internal/datastore"
"forge.cadoles.com/cadoles/bouncer/internal/datastore/sqlite"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
func NewProxyRepository(ctx context.Context, conf config.DatabaseConfig) (datastore.ProxyRepository, error) {
driver := string(conf.Driver)
dsn := string(conf.DSN)
var repository datastore.ProxyRepository
logger.Debug(ctx, "initializing proxy repository", logger.F("driver", driver), logger.F("dsn", dsn))
switch driver {
case config.DatabaseDriverSQLite:
url, err := url.Parse(dsn)
if err != nil {
return nil, errors.WithStack(err)
}
db, err := sql.Open(driver, url.Host+url.Path)
if err != nil {
return nil, errors.WithStack(err)
}
repository = sqlite.NewProxyRepository(db)
default:
return nil, errors.Errorf("unsupported database driver '%s'", driver)
}
return repository, nil
}