From ea7a6ac27a614ea94adf57b397de7e6da172d67b Mon Sep 17 00:00:00 2001 From: Pavel Botsman <11509664+botsman@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:34:45 +0300 Subject: [PATCH] improve building docker image --- Dockerfile | 5 +++-- server/mongo/db.go | 22 +++++++++++++++++++++- tools/tppdb/parse.go | 3 --- tools/tppdb/run.go | 10 +++++----- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index a6595b4..6b5aff4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.23-alpine AS builder +FROM golang:1.25-alpine AS builder WORKDIR /app @@ -8,10 +8,11 @@ RUN go mod verify COPY . . -RUN cd server && go build -o /app/main . +RUN CGO_ENABLED=0 go build -o /app/main ./server FROM scratch +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /app/main /app/main ENTRYPOINT ["/app/main"] diff --git a/server/mongo/db.go b/server/mongo/db.go index c48b239..5e10008 100644 --- a/server/mongo/db.go +++ b/server/mongo/db.go @@ -3,6 +3,7 @@ package mongo import ( "context" "errors" + "strings" "github.com/botsman/tppVerifier/app/cert" "github.com/botsman/tppVerifier/app/db" @@ -22,10 +23,29 @@ func NewMongoRepo(ctx context.Context, connStr string) (db.TppRepository, error) if err != nil { return nil, err } - db := client.Database(opts.Auth.AuthSource) + dbName, err := extractDatabaseName(connStr) + if err != nil { + return nil, err + } + if dbName == "" { + return nil, errors.New("database name is required in connection string") + } + db := client.Database(dbName) return &TppMongoRepository{db: db}, nil } +func extractDatabaseName(connStr string) (string, error) { + opts := options.Client().ApplyURI(connStr) + if opts.Auth != nil && opts.Auth.AuthSource != "" { + return opts.Auth.AuthSource, nil + } + connStrParts := strings.Split(connStr, "/") + if len(connStrParts) < 2 { + return "", errors.New("database name not found in connection string") + } + return connStrParts[len(connStrParts)-1], nil +} + type TppMongoRepository struct { db *mongo.Database } diff --git a/tools/tppdb/parse.go b/tools/tppdb/parse.go index 341b7c5..980c193 100644 --- a/tools/tppdb/parse.go +++ b/tools/tppdb/parse.go @@ -392,15 +392,12 @@ func parseRegistry() (<-chan models.TPP, error) { for _, tpps := range registry { for _, rawTpp := range tpps { if rawTpp.CA_OwnerID == "" || rawTpp.Code == "" { - // log.Printf("Skipping TPP with missing CA_OwnerID or Code: %+v\n", rawTpp) continue } if rawTpp.Type == "" { - // log.Printf("Skipping TPP with missing Type: %+v\n", rawTpp) continue } if rawTpp.AuthorizedAt == nil || rawTpp.AuthorizedAt.IsZero() { - // log.Printf("Skipping TPP with missing AuthorizedAt: %+v\n", rawTpp) continue } if !slices.Contains([]string{"PSD_AISP", "PSD_PI", "PSD_EMI"}, rawTpp.Type) { diff --git a/tools/tppdb/run.go b/tools/tppdb/run.go index 9ed07c6..8315a51 100644 --- a/tools/tppdb/run.go +++ b/tools/tppdb/run.go @@ -19,6 +19,11 @@ func Run(ctx context.Context, connStr string) error { // 3. Unzip the file // 4. Parse the file // 5. Save the parsed data to the DB + client, err := setupMongoDb(ctx, connStr) + // client, err := setupSqliteDb(ctx, connStr) + if err != nil { + return err + } getRegistry() defer deleteRegistry() @@ -27,11 +32,6 @@ func Run(ctx context.Context, connStr string) error { if err != nil { return err } - client, err := setupMongoDb(ctx, connStr) - // client, err := setupSqliteDb(ctx, connStr) - if err != nil { - return err - } defer client.Disconnect(ctx) return saveTPPs(client, tppChan) }