Merge pull request 'feature/database-api' (#2) from feature/database-api into main
Reviewed-on: https://git.walzen665.de/Walzen665/fluidcms/pulls/2 approved
This commit is contained in:
commit
23709c3497
@ -17,6 +17,10 @@ dependencies {
|
||||
testImplementation 'io.rest-assured:rest-assured'
|
||||
compileOnly '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'
|
||||
|
13
db/docker-compose.yml
Normal file
13
db/docker-compose.yml
Normal file
@ -0,0 +1,13 @@
|
||||
version: '3.8'
|
||||
services:
|
||||
rethinkdb:
|
||||
image: rethinkdb:latest
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- rethinkdb_data:/data
|
||||
ports:
|
||||
- "28015:28015" # Client driver port
|
||||
- "8080:8080" # Admin UI port
|
||||
|
||||
volumes:
|
||||
rethinkdb_data:
|
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 io.quarkus.runtime.Startup;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Singleton
|
||||
public class RethinkDBService {
|
||||
|
||||
@Inject
|
||||
RethinkDBConfig config;
|
||||
|
||||
@Inject
|
||||
RethinkDBConnector connector;
|
||||
|
||||
private Connection connection;
|
||||
private RethinkDB r;
|
||||
|
||||
@Startup
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
43
src/main/java/de/w665/fluidcms/db/repo/TestRepo.java
Normal file
43
src/main/java/de/w665/fluidcms/db/repo/TestRepo.java
Normal file
@ -0,0 +1,43 @@
|
||||
package de.w665.fluidcms.db.repo;
|
||||
|
||||
import com.rethinkdb.RethinkDB;
|
||||
import com.rethinkdb.net.Connection;
|
||||
import de.w665.fluidcms.db.RethinkDBConfig;
|
||||
import de.w665.fluidcms.db.RethinkDBConnector;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@ApplicationScoped
|
||||
public class TestRepo {
|
||||
|
||||
@Inject
|
||||
RethinkDBConnector connector;
|
||||
|
||||
@Inject
|
||||
RethinkDBConfig config;
|
||||
|
||||
private final String TABLE_NAME = "test";
|
||||
private RethinkDB r;
|
||||
private Connection connection;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.r = this.connector.getR();
|
||||
this.connection = this.connector.getConnection();
|
||||
|
||||
// Check if table exists
|
||||
try {
|
||||
r.db(config.getDatabase()).tableCreate(TABLE_NAME).run(connection);
|
||||
log.debug("Table " + TABLE_NAME + " created");
|
||||
} catch (Exception e) {
|
||||
log.debug("Table " + TABLE_NAME + " already exists");
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(String value) {
|
||||
r.db(config.getDatabase()).table(TABLE_NAME).insert(r.hashMap("value", value)).run(connection);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package de.w665.fluidcms.rest.mapping;
|
||||
|
||||
import de.w665.fluidcms.db.repo.TestRepo;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.jboss.resteasy.reactive.RestResponse;
|
||||
|
||||
@Path("/test")
|
||||
public class TestResource {
|
||||
|
||||
@Inject
|
||||
TestRepo testRepo;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public RestResponse<String> test() {
|
||||
testRepo.insert("testString");
|
||||
return RestResponse.ResponseBuilder.ok("Inserted testString into db.").build();
|
||||
}
|
||||
}
|
@ -1,2 +1,11 @@
|
||||
quarkus.quinoa.dev-server.port=4200
|
||||
quarkus.quinoa.build-dir=dist
|
||||
quarkus.http.port=80
|
||||
quarkus.resteasy-reactive.path=/api/v1
|
||||
quarkus.log.category."de.w665.fluidcms".level=DEBUG
|
||||
|
||||
# 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