Current HikariModule
contains hard-coded value in Java code, which is not a good practice, much better would be use values defined in db.properties
. How to achieve this? Do I need create a custom ConfigurableModule<MyModule.Settings>
and register HikariModule
inside MyModule
? I have not found the way how to register a module inside a module. Thanks!
public class App {
public static void main(String[] args) throws Exception {
RatpackServer.start(s -> s
.serverConfig( configBuilder -> configBuilder
.findBaseDir()
.props("db.properties")
.require("/database", Settings.class)
)
.registry(Guice.registry( bindings -> bindings
.module(HikariModule.class, hm -> {
hm.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
hm.addDataSourceProperty("url", "jdbc:postgresql://localhost:5433/ratpack");
hm.setUsername("postgres");
hm.setPassword("postgres");
}).bind(DatabaseInit.class)
))
.handlers( chain -> chain
...
)
);
}
}
Let's say you have a postgres.yaml
file in src/ratpack/postgres.yaml
whose contents are:
db:
dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
username: postgres
password: password
dataSourceProperties:
databaseName: modern
serverName: 192.168.99.100
portNumber: 5432
In that same directory let's say you have an empty .ratpack
file.
From your main class you can then do this:
RatpackServer.start(serverSpec -> serverSpec
.serverConfig(config -> config
.baseDir(BaseDir.find()) // locates the .ratpack file
.yaml("postgres.yaml") // finds file relative to directory containing .ratpack file
.require("/db", HikariConfig.class) // bind props from yaml file to HikariConfig class
).registry(Guice.registry(bindings -> bindings
.module(HikariModule.class) // this will use HikariConfig to configure the module
)).handlers(...));
There's a full working example here https://github.com/danhyun/modern-java-web