make sure we use proper context
This commit is contained in:
@@ -36,7 +36,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
|||||||
var continueWithoutSubmitting bool
|
var continueWithoutSubmitting bool
|
||||||
|
|
||||||
for {
|
for {
|
||||||
db, err := storage.NewMongoStorageInteractive(app.Config.MongoURI)
|
db, err := storage.NewMongoStorageInteractive(cmd.Context(), app.Config.MongoURI)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
mongoStorage = db
|
mongoStorage = db
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ var SubmitCmd = &cobra.Command{
|
|||||||
return fmt.Errorf("application context not initialized")
|
return fmt.Errorf("application context not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := storage.NewMongoStorageInteractive(app.Config.MongoURI)
|
db, err := storage.NewMongoStorageInteractive(cmd.Context(), app.Config.MongoURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to database: %w", err)
|
return fmt.Errorf("failed to connect to database: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,27 +18,27 @@ type MongoStorage struct {
|
|||||||
client *mongo.Client
|
client *mongo.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMongoStorage(uri, username, password string) (*MongoStorage, error) {
|
func NewMongoStorage(ctx context.Context, uri, username, password string) (*MongoStorage, error) {
|
||||||
credentials := options.Credential{
|
credentials := options.Credential{
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := mongo.Connect(context.Background(),
|
client, err := mongo.Connect(ctx,
|
||||||
options.Client().ApplyURI(uri).SetAuth(credentials))
|
options.Client().ApplyURI(uri).SetAuth(credentials))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to connect to MongoDB: %v", err)
|
return nil, fmt.Errorf("failed to connect to MongoDB: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.Ping(context.Background(), nil); err != nil {
|
if err := client.Ping(ctx, nil); err != nil {
|
||||||
return nil, fmt.Errorf("cannot reach MongoDB: %v", err)
|
return nil, fmt.Errorf("cannot reach MongoDB: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MongoStorage{client: client}, nil
|
return &MongoStorage{client: client}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMongoStorageInteractive(uri string) (*MongoStorage, error) {
|
func NewMongoStorageInteractive(ctx context.Context, uri string) (*MongoStorage, error) {
|
||||||
var username, password string
|
var username, password string
|
||||||
if os.Getenv("DB_USER") == "" {
|
if os.Getenv("DB_USER") == "" {
|
||||||
if err := huh.NewInput().
|
if err := huh.NewInput().
|
||||||
@@ -72,7 +72,7 @@ func NewMongoStorageInteractive(uri string) (*MongoStorage, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mongoClient, err := NewMongoStorage(uri, username, password)
|
mongoClient, err := NewMongoStorage(ctx, uri, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user