currently refactoring a lot
This commit is contained in:
parent
700b5d5d10
commit
31eb8a92eb
42 changed files with 454 additions and 433 deletions
|
|
@ -23,7 +23,7 @@ dependencies {
|
|||
api("org.mongodb:bson:5.8.0")
|
||||
|
||||
// http://forge.kentoj.de/scrow/-/packages/maven/de.kentoj.scrow:kencommandapi-core
|
||||
api("de.kentoj.scrow:kencommandapi-core:0.22")
|
||||
api("de.kentoj.scrow:kencommandapi-core:0.23")
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
package de.kentoj.scrowlib.connection;
|
||||
|
||||
import org.springframework.r2dbc.core.DatabaseClient;
|
||||
|
||||
public interface DatabaseConnectionFactory extends AutoCloseable {
|
||||
|
||||
DatabaseClient create();
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
package de.kentoj.scrowlib.connection;
|
||||
|
||||
import io.r2dbc.pool.ConnectionPool;
|
||||
import io.r2dbc.pool.ConnectionPoolConfiguration;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.r2dbc.core.DatabaseClient;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class DatabaseConnectionFactoryImpl implements DatabaseConnectionFactory {
|
||||
|
||||
private final ConnectionPool backingFactory;
|
||||
|
||||
public DatabaseConnectionFactoryImpl(ConnectionFactory connectionFactory) {
|
||||
this(new ConnectionPool(ConnectionPoolConfiguration.builder()
|
||||
.connectionFactory(connectionFactory)
|
||||
.build()));
|
||||
}
|
||||
|
||||
// FIXME
|
||||
// its been a while and i no longer remember what had to be fixed
|
||||
@Override
|
||||
public DatabaseClient create() {
|
||||
return DatabaseClient.create(backingFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
backingFactory.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,8 @@ import java.util.List;
|
|||
|
||||
public class ScrowMessageStyle implements MessageStyle {
|
||||
|
||||
public static ScrowMessageStyle GENERIC = new ScrowMessageStyle("Scrow", NamedTextColor.WHITE);
|
||||
|
||||
@Getter
|
||||
private final Component prefix;
|
||||
@Getter
|
||||
|
|
@ -32,16 +34,6 @@ public class ScrowMessageStyle implements MessageStyle {
|
|||
.append(msg.colorIfAbsent(NamedTextColor.GRAY));
|
||||
}
|
||||
|
||||
public Component list(Component header, List<? extends Component> list) {
|
||||
var res = ok(header);
|
||||
for (Component component : list) {
|
||||
res = res.appendNewline()
|
||||
.append(Component.text(" - ").color(NamedTextColor.GRAY))
|
||||
.append(component.colorIfAbsent(NamedTextColor.GRAY));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component err(Component msg) {
|
||||
return ok(msg.color(NamedTextColor.RED));
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package de.kentoj.scrowlib.servermanager;
|
||||
package de.kentoj.scrowlib.instancemanager;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package de.kentoj.scrowlib.instancemanager;
|
||||
|
||||
import de.kentoj.scrowlib.messaging.MessageBroker;
|
||||
import io.nats.client.Connection;
|
||||
import org.bson.Document;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static de.kentoj.scrowlib.utils.ResultUtils.unwrap;
|
||||
|
||||
public class InstanceManagerImpl implements InstanceManager {
|
||||
|
||||
private final MessageBroker messageBroker;
|
||||
|
||||
public InstanceManagerImpl(Connection natsCon) {
|
||||
messageBroker = new MessageBroker(natsCon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerInstance deployInstance(String template) {
|
||||
var result = messageBroker.request("instance.deploy", new Document("template", template));
|
||||
var document = unwrap(result, "failed to deploy instance of " + template);
|
||||
return ServerInstance.fromDocument(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean destroyInstance(String handle) {
|
||||
var result = messageBroker.request("instance.destroy", new Document("handle", handle));
|
||||
return unwrap(result.mapSuccess(__ -> true)
|
||||
.recover(this::wasNotFound, __ -> false), "failed to destroy instance " + handle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServerInstance> getInstances() {
|
||||
var result = messageBroker.request("instance.list", new Document());
|
||||
return unwrap(result.mapSuccess(doc -> doc.getList("instances", Document.class)
|
||||
.stream().map(ServerInstance::fromDocument).toList()),
|
||||
"failed to get instances");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ServerInstance getInstance(String handle) {
|
||||
var result = messageBroker.request("instance.get", new Document("handle", handle));
|
||||
return unwrap(result.mapSuccess(ServerInstance::fromDocument)
|
||||
.recover(this::wasNotFound, __ -> null), "failed to get instance " + handle);
|
||||
}
|
||||
|
||||
private boolean wasNotFound(String failure) {
|
||||
return "no instance found".equals(failure);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.kentoj.scrowlib.servermanager;
|
||||
package de.kentoj.scrowlib.instancemanager;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import lombok.Value;
|
||||
|
|
@ -12,5 +12,5 @@ public interface RequestHandler {
|
|||
*
|
||||
* @return result to send back to the requesting client.
|
||||
*/
|
||||
CompletionStage<Result<Document, String>> handle(Document request);
|
||||
CompletionStage<Result<Document, String>> handle(Document doc);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package de.kentoj.scrowlib.messaging;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SyncRequestHandler extends RequestHandler{
|
||||
Result<Document, String> handleSync(Document doc);
|
||||
|
||||
@Override
|
||||
default CompletionStage<Result<Document, String>> handle(Document doc) {
|
||||
return CompletableFuture.completedFuture(handleSync(doc));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
package de.kentoj.scrowlib.servermanager;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import de.kentoj.scrowlib.messaging.MessageBroker;
|
||||
import io.nats.client.Connection;
|
||||
import org.bson.Document;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class InstanceManagerImpl implements InstanceManager {
|
||||
|
||||
private final MessageBroker messageBroker;
|
||||
|
||||
public InstanceManagerImpl(Connection natsCon) {
|
||||
messageBroker = new MessageBroker(natsCon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<ServerInstance, String> deployInstance(String template) {
|
||||
var result = messageBroker.request("instance.deploy", new Document("template", template));
|
||||
return result.mapSuccess(ServerInstance::fromDocument);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Object, String> destroyInstance(String handle) {
|
||||
var result = messageBroker.request("instance.destroy", new Document("handle", handle));
|
||||
return result.mapSuccess(__ -> null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<ServerInstance>, String> getInstances() {
|
||||
var result = messageBroker.request("instance.list", new Document());
|
||||
return result.mapSuccess(doc -> doc.getList("instances", Document.class)
|
||||
.stream()
|
||||
.map(ServerInstance::fromDocument)
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<@Nullable ServerInstance, String> getInstance(String handle) {
|
||||
var result = messageBroker.request("instance.get", new Document("handle", handle));
|
||||
return result.mapSuccess(ServerInstance::fromDocument);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package de.kentoj.scrowlib.utils;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.NONE)
|
||||
public class ResultUtils {
|
||||
public static <S> S unwrap(Result<S, String> result, String message) {
|
||||
Optional<String> err = result.getFailure();
|
||||
if (err.isPresent())
|
||||
throw new RuntimeException(message + ": " + err.orElseThrow());
|
||||
return result.getSuccess().orElseThrow();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue