feat: initial commit
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
43
internal/setup/repository.go
Normal file
43
internal/setup/repository.go
Normal file
@ -0,0 +1,43 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user