Add RethinkDBConnector and RethinkDBService
- Configure dependency
This commit is contained in:
parent
d4c6c4165b
commit
375722e3a4
@ -17,6 +17,10 @@ dependencies {
|
|||||||
testImplementation 'io.rest-assured:rest-assured'
|
testImplementation 'io.rest-assured:rest-assured'
|
||||||
compileOnly 'org.projectlombok:lombok:1.18.32'
|
compileOnly 'org.projectlombok:lombok:1.18.32'
|
||||||
annotationProcessor 'org.projectlombok:lombok:1.18.32'
|
annotationProcessor 'org.projectlombok:lombok:1.18.32'
|
||||||
|
implementation ('com.rethinkdb:rethinkdb-driver:2.4.4') {
|
||||||
|
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind' // exclude older jackson-databind version
|
||||||
|
}
|
||||||
|
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.1' // Manually add newer jackson-databind version without CVE
|
||||||
}
|
}
|
||||||
|
|
||||||
group 'de.w665'
|
group 'de.w665'
|
||||||
|
23
src/main/java/de/w665/fluidcms/db/RethinkDBConfig.java
Normal file
23
src/main/java/de/w665/fluidcms/db/RethinkDBConfig.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package de.w665.fluidcms.db;
|
||||||
|
|
||||||
|
import jakarta.inject.Singleton;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Singleton
|
||||||
|
public class RethinkDBConfig {
|
||||||
|
@ConfigProperty(name = "fluidcms.database.host")
|
||||||
|
String host;
|
||||||
|
|
||||||
|
@ConfigProperty(name = "fluidcms.database.port")
|
||||||
|
int port;
|
||||||
|
|
||||||
|
@ConfigProperty(name = "fluidcms.database.name")
|
||||||
|
String database;
|
||||||
|
|
||||||
|
@ConfigProperty(name = "fluidcms.database.autoresetonstartup")
|
||||||
|
boolean autoresetonstartup;
|
||||||
|
}
|
37
src/main/java/de/w665/fluidcms/db/RethinkDBConnector.java
Normal file
37
src/main/java/de/w665/fluidcms/db/RethinkDBConnector.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package de.w665.fluidcms.db;
|
||||||
|
|
||||||
|
import com.rethinkdb.RethinkDB;
|
||||||
|
import com.rethinkdb.net.Connection;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.annotation.PreDestroy;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import jakarta.inject.Singleton;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Singleton
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public class RethinkDBConnector {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
RethinkDBConfig config;
|
||||||
|
|
||||||
|
private final RethinkDB r = RethinkDB.r;
|
||||||
|
private Connection connection;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void connectToDatabase() {
|
||||||
|
connection = r.connection().hostname(config.getHost()).port(config.getPort()).connect();
|
||||||
|
log.info("Connected to RethinkDB at " + config.getHost() + ":" + config.getPort() + " on database " + config.getDatabase());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void closeConnection() {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/main/java/de/w665/fluidcms/db/RethinkDBService.java
Normal file
42
src/main/java/de/w665/fluidcms/db/RethinkDBService.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package de.w665.fluidcms.db;
|
||||||
|
|
||||||
|
import com.rethinkdb.RethinkDB;
|
||||||
|
import com.rethinkdb.gen.exc.ReqlOpFailedError;
|
||||||
|
import com.rethinkdb.net.Connection;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@ApplicationScoped
|
||||||
|
public class RethinkDBService {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
RethinkDBConfig config;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
RethinkDBConnector connector;
|
||||||
|
|
||||||
|
private Connection connection;
|
||||||
|
private RethinkDB r;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.r = connector.getR();
|
||||||
|
this.connection = connector.getConnection();
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initialize() {
|
||||||
|
|
||||||
|
log.debug("Initializing RethinkDB database...");
|
||||||
|
// rethinkdb check if database exists
|
||||||
|
try {
|
||||||
|
r.dbCreate(config.getDatabase()).run(connection).stream();
|
||||||
|
log.debug("Database " + config.getDatabase() + " created");
|
||||||
|
} catch (ReqlOpFailedError e) {
|
||||||
|
log.debug("Database " + config.getDatabase() + " already exists. Error: " + e.getClass().getSimpleName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,27 @@
|
|||||||
package de.w665.fluidcms.rest.mapping;
|
package de.w665.fluidcms.rest.mapping;
|
||||||
|
|
||||||
|
import de.w665.fluidcms.db.RethinkDBConfig;
|
||||||
|
import de.w665.fluidcms.db.RethinkDBService;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
import jakarta.ws.rs.GET;
|
import jakarta.ws.rs.GET;
|
||||||
import jakarta.ws.rs.Path;
|
import jakarta.ws.rs.Path;
|
||||||
|
import jakarta.ws.rs.Produces;
|
||||||
|
import jakarta.ws.rs.core.MediaType;
|
||||||
import org.jboss.resteasy.reactive.RestResponse;
|
import org.jboss.resteasy.reactive.RestResponse;
|
||||||
|
|
||||||
@Path("/test")
|
@Path("/test")
|
||||||
public class TestResource {
|
public class TestResource {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
RethinkDBConfig rethinkDBConfig;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
RethinkDBService rethinkDBService;
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
public RestResponse<String> test() {
|
public RestResponse<String> test() {
|
||||||
return RestResponse.ResponseBuilder.ok("Test").build();
|
this.rethinkDBService.initialize();
|
||||||
|
return RestResponse.ResponseBuilder.ok(rethinkDBConfig.getHost()).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
quarkus.quinoa.dev-server.port=4200
|
quarkus.quinoa.dev-server.port=4200
|
||||||
quarkus.quinoa.build-dir=dist
|
quarkus.quinoa.build-dir=dist
|
||||||
quarkus.http.port=80
|
quarkus.http.port=80
|
||||||
quarkus.resteasy-reactive.path=/api/v1
|
quarkus.resteasy-reactive.path=/api/v1
|
||||||
|
|
||||||
|
# Database
|
||||||
|
fluidcms.database.host=localhost
|
||||||
|
fluidcms.database.port=28015
|
||||||
|
fluidcms.database.name=fluidcms
|
||||||
|
fluidcms.database.autoresetonstartup=false
|
Loading…
x
Reference in New Issue
Block a user