Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize the Mongod configuration properly in a Spring Boot context? #11

Closed
enolive opened this issue Jan 12, 2023 · 9 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@enolive
Copy link

enolive commented Jan 12, 2023

I am struggling to find a proper way to customize the embedded mongo.

I am looking for a way to override

  • the download location to an URL inside my company intranet as I am behind a "firewall of death" there 馃槈
  • the target directory to a path under the Working Directory as it seems to default to $HOME/.embedmongo which won't work correctly on our CI/CD system (for reasons 馃槈)

I am aware of the Customizations described in the Howto Document but I am not sure if I adapted it correctly to a Spring Boot Application. I am also aware that I can easily set the mongo version with the property de.flapdoodle.mongodb.embedded.version

  @Bean
  fun mongoConfiguration(net: Net): Mongod {
    return Mongod.builder()
      .distributionBaseUrl(Start.to(DistributionBaseUrl::class.java)
        .initializedWith(DistributionBaseUrl.of("https://my-company.site"))
      )
      .persistentBaseDir(Start.to(PersistentDir::class.java)
        .initializedWith(PersistentDir.inWorkingDir("target/embeddedmongo").get())
      )
      .net(Start.to(Net::class.java).initializedWith(net))
      .build()
  }

Problems that I see:

  • without setting the net property, Spring Boot tries to connect to the default mongo port (27017) which will fail. I got this hack by looking at the internals of EmbeddedMongoAutoConfiguration
  • PersistentDir.inWorkingDir returns a ThrowingSupplier I have no clue how to use properly in a Transition. Using get() seems like a dirty hack on an "Either-like" data type
  • Is there a better way to override the properties I mentioned above?

as you might see, I am using Kotlin. I can also provide a Java sample instead if needed!

@michaelmosmann
Copy link
Member

@enolive thats one way to go ..

you can set the base dir also with an env variable as you can see here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/main/src/main/java/de/flapdoodle/embed/mongo/transitions/ExtractFileSet.java#L60

You can also use:

Start.to(PersistenDir::class.java)
.providedBy(PersistentDir.inWorkingDir(".embeddedMongodbCustomPath")
.mapToUncheckedException(RuntimeException::new))

.. this way it will be evaluated later.. and you must handle a possible IOException .. in this case it will be mapped to a RuntimeException ..

There are at least two options .. a) use the builder pattern, b) use method overrides.. but i had some strange case, where this did not work in one case.. so builder pattern should work..

@michaelmosmann
Copy link
Member

@enolive spring 3.x changed some internals .. i have to find a way to restore this port handling from 2.7.x somehow...

@enolive
Copy link
Author

enolive commented Jan 13, 2023

thanks for your helpful pointers!

While the ENV variable solution would be a very elegant solution to my nasty CI/CD problem, it would force me to scatter the embedded mongo configuration to different locations which I'd like to avoid.

I like the .mapToUncheckedException(...) operation, but it seems to depend on a later version of de.flapdoodle.embed.mongo:4.3.* than the one specified as the peer dependency of *.spring30x:4.3.2 (same goes for the 4.3.3-SNAPSHOT version I find here in the repo). I want to avoid messing around with overriden peer dependencies if possible.
An updated version of spring30x with new peer dependencies would be really helpful to me 馃槈

@michaelmosmann
Copy link
Member

@enolive there will be a new version soon ..

@mcordeiro73
Copy link

I am similarly looking for a way to configure the storage directory of the embedded database. With Spring's old implementation this used to be accomplished by setting spring.mongodb.embedded.storage.databaseDir.

I tried de.flapdoodle.mongodb.embedded.storage.databaseDir but it appears that had no affect.

@michaelmosmann
Copy link
Member

@mcordeiro73 ah.. ok. I think i can reimplement this feature ..

@michaelmosmann michaelmosmann self-assigned this Jan 31, 2023
@michaelmosmann michaelmosmann added the enhancement New feature or request label Jan 31, 2023
@michaelmosmann
Copy link
Member

@mcordeiro73 feature is back as you can see here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/main/HowTo.md#custom-database-dir .. you must use de.flapdoodle.mongodb.embedded.databaseDir (backport to my spring3.0.x, spring2.7.x and spring2.6.x adapter) ..

can you close this, if this works for you? .. and if not, please open an new issue:)

@mcordeiro73
Copy link

mcordeiro73 commented Feb 13, 2023

@michaelmosmann The property worked great. Not sure I can close the issue since I didn't open it. @enolive would you like to do the honors?

@michaelmosmann
Copy link
Member

@mcordeiro73 @enolive .. so i will close this:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants