Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions server/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"context"
"log"
"net/http"
"os"

"github.com/botsman/tppVerifier/app"
"github.com/botsman/tppVerifier/app/cert"
"github.com/botsman/tppVerifier/app/verify"

// "github.com/botsman/tppVerifier/server/mongo"
"github.com/botsman/tppVerifier/server/sqlite"
"github.com/botsman/tppVerifier/server/mongo"
// "github.com/botsman/tppVerifier/server/sqlite"
)

func main() {
ctx := context.Background()

connStr := os.Getenv("DATABASE_URL")
// Uncomment the backend you want to use:
// repo, err := mongo.NewMongoRepo(ctx, "mongodb://localhost:27017/tppVerifier")
repo, err := sqlite.NewSQLiteRepo("../data/sqlite.db")
repo, err := mongo.NewMongoRepo(ctx, connStr)
// repo, err := sqlite.NewSQLiteRepo(ctx, connStr)
if err != nil {
log.Fatalf("Failed to initialize repository: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// NewSQLiteRepo creates a TppRepository backed by SQLite, using the given database file path.
func NewSQLiteRepo(path string) (db.TppRepository, error) {
func NewSQLiteRepo(_ context.Context, path string) (db.TppRepository, error) {
dbConn, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
Expand Down
23 changes: 9 additions & 14 deletions tools/certdb/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"github.com/botsman/tppVerifier/app/cert"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"os"
"time"
)

Expand All @@ -15,27 +13,24 @@ type MongoCertDb struct {
Database *mongo.Database
}

func setupMongoCertDb() (*MongoCertDb, error) {
mongoURI := os.Getenv("MONGO_URL")
if mongoURI == "" {
mongoURI = "mongodb://localhost:27017"
}
clientOptions := options.Client().ApplyURI(mongoURI)

client, err := mongo.Connect(context.TODO(), clientOptions)
func setupMongoCertDb(ctx context.Context, connStr string) (*MongoCertDb, error) {
opts := options.Client().ApplyURI(connStr)
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Fatal(err)
return nil, err
}

err = client.Ping(context.TODO(), nil)
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
return nil, err
}
db := client.Database(opts.Auth.AuthSource)
if db == nil {
db = client.Database("tppVerifier")
}
return &MongoCertDb{
Client: client,
Database: client.Database("tppVerifier"),
Database: db,
}, nil
}

Expand Down
7 changes: 3 additions & 4 deletions tools/certdb/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"time"
)

func Run() error {
ctx := context.Background()
func Run(ctx context.Context, connStr string) error {
// Choose DB implementation here (Mongo or SQLite)
db, err := setupMongoCertDb()
// db, err := setupSqliteCertDb("data/sqlite.db")
db, err := setupMongoCertDb(ctx, connStr)
// db, err := setupSqliteCertDb(ctx, connStr)
if err != nil {
return fmt.Errorf("DB setup failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/certdb/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type SqliteCertDb struct {
DB *sql.DB
}

func setupSqliteCertDb(path string) (*SqliteCertDb, error) {
func setupSqliteCertDb(_ context.Context, path string) (*SqliteCertDb, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
Expand Down
23 changes: 9 additions & 14 deletions tools/tppdb/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package tppdb

import (
"context"
"log"
"os"

"github.com/botsman/tppVerifier/app/models"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -16,27 +14,24 @@ type MongoDb struct {
Database *mongo.Database
}

func setupMongoDb() (*MongoDb, error) {
mongoURI := os.Getenv("MONGO_URL")
if mongoURI == "" {
mongoURI = "mongodb://localhost:27017"
}
clientOptions := options.Client().ApplyURI(mongoURI)

client, err := mongo.Connect(context.TODO(), clientOptions)
func setupMongoDb(ctx context.Context, connStr string) (*MongoDb, error) {
opts := options.Client().ApplyURI(connStr)
client, err := mongo.Connect(ctx, opts)
if err != nil {
log.Fatal(err)
return nil, err
}

err = client.Ping(context.TODO(), nil)
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
return nil, err
}
db := client.Database(opts.Auth.AuthSource)
if db == nil {
db = client.Database("tppVerifier")
}
return &MongoDb{
Client: client,
Database: client.Database("tppVerifier"),
Database: db,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions tools/tppdb/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type db interface {
Disconnect(ctx context.Context) error
}

func Run() error {
func Run(ctx context.Context, connStr string) error {
// Download and parse the registry
// populate DB
// 1. Download metadata at https://euclid.eba.europa.eu/register/api/filemetadata?t=1737374419184
Expand All @@ -27,11 +27,11 @@ func Run() error {
if err != nil {
return err
}
client, err := setupMongoDb()
// client, err := setupSqliteDb("data/sqlite.db")
client, err := setupMongoDb(ctx, connStr)
// client, err := setupSqliteDb(ctx, connStr)
if err != nil {
return err
}
defer client.Disconnect(context.TODO())
defer client.Disconnect(ctx)
return saveTPPs(client, tppChan)
}