Skip to content

Commit

Permalink
Merge pull request #210 from HttpMarco/development
Browse files Browse the repository at this point in the history
Release 1.0.0.6-SNAPSHOT
  • Loading branch information
HttpMarco authored Nov 29, 2024
2 parents f1e5287 + 2626dab commit d0d2891
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 1.0.0.5-SNAPSHOT
version = 1.0.0.6-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public String message(String message) {
return response.toString();
}

public String errorMessage(String errorCode, String message){
var response = new JsonObject();
response.addProperty("errorCode", errorCode);
response.addProperty("message", message);
return response.toString();
}

public boolean isNumber(String number) {
try {
Integer.parseInt(number);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void createGroup(Context context) {
context.status(400).json(message("Name cannot be empty"));
return;
}
if(Node.instance().groupProvider().find(request.name()) != null) {
context.status(400).json(errorMessage("group/alreadyExists","Name already exists"));
return;
}


var platform = Node.instance().platformService().find(request.platform());
if (platform == null) {
Expand Down Expand Up @@ -132,6 +137,6 @@ public void shutdownGroup(Context context) {

context.status(200).json(message("Stopping all services of the group " + request.groupName()));
}

//TODO put method to update
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ public void list(Context context) {
var response = new JsonObject();
var platforms = new JsonArray();

Node.instance().platformService().platforms().forEach(platform -> platforms.add(platform.id()));
Node.instance().platformService().platforms().forEach(platform -> {
var platformObject = new JsonObject();
platformObject.addProperty("id", platform.id());
platformObject.addProperty("type", platform.type().toString());

platforms.add(platformObject);
});
response.add("platforms", platforms);

context.status(200).json(response.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package dev.httpmarco.polocloud.modules.rest.socket;

import dev.httpmarco.polocloud.modules.rest.RestModule;
import io.javalin.websocket.WsCloseContext;
import io.javalin.websocket.WsConnectContext;
import io.javalin.websocket.WsErrorContext;
import io.javalin.websocket.WsMessageContext;
import io.javalin.websocket.*;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Getter
@Accessors(fluent = true)
public abstract class WebSocket {
public abstract class WebSocket implements SocketSendable {

private final String path;
private final String permission;
protected final RestModule restModule;
private final List<WsContext> connectedClients;

public WebSocket(String path, String permission, RestModule restModule) {
this.path = path;
this.permission = permission;
this.restModule = restModule;

this.connectedClients = new ArrayList<>();
}

public abstract void onConnect(WsConnectContext context);
Expand All @@ -29,4 +32,16 @@ public WebSocket(String path, String permission, RestModule restModule) {
public abstract void onMessage(WsMessageContext context);

public abstract void onError(WsErrorContext context);

@Override
public void send(String content) {
for (var client : this.connectedClients) {
if (!client.session.isOpen()) {
this.connectedClients.remove(client);
continue;
}

client.send(content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import dev.httpmarco.polocloud.modules.rest.RestModule;
import dev.httpmarco.polocloud.modules.rest.socket.WebSocket;
import dev.httpmarco.polocloud.modules.rest.socket.web.impl.v1.ConsoleLogWebWebSocket;
import dev.httpmarco.polocloud.modules.rest.socket.web.impl.v1.NotifyWebSocket;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;


@Accessors(fluent = true)
public class WebSocketService {

public static final String API_PATH = "/polocloud/api/v1";
private final RestModule restModule;
private final WebSocketAuthService webSocketAuthService;
@Getter
private final List<WebSocket> webSockets;

public WebSocketService(RestModule restModule) {
Expand All @@ -29,6 +33,7 @@ private void registerWebSockets(WebSocket... webSocket) {

public void invoke() {
registerWebSockets(
new NotifyWebSocket(this.restModule),
new ConsoleLogWebWebSocket(this.restModule)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

import dev.httpmarco.polocloud.api.event.impl.services.log.ServiceLogEvent;
import dev.httpmarco.polocloud.modules.rest.RestModule;
import dev.httpmarco.polocloud.modules.rest.socket.SocketSendable;
import dev.httpmarco.polocloud.modules.rest.socket.WebSocket;
import dev.httpmarco.polocloud.node.Node;
import io.javalin.websocket.*;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

@Log4j2
public class ConsoleLogWebWebSocket extends WebSocket implements SocketSendable {
public class ConsoleLogWebWebSocket extends WebSocket {

private final List<WsContext> connectedClients;

public ConsoleLogWebWebSocket(RestModule restModule) {
super("/{service}/log", "polocloud.service.screen", restModule);

this.connectedClients = new ArrayList<>();
}

@Override
Expand All @@ -46,13 +39,13 @@ public void onConnect(WsConnectContext context) {
}
});

this.connectedClients.add(context);
connectedClients().add(context);
context.enableAutomaticPings();
}

@Override
public void onClose(WsCloseContext context) {
this.connectedClients.remove(context);
connectedClients().remove(context);
}

@Override
Expand All @@ -64,11 +57,4 @@ public void onMessage(WsMessageContext context) {
public void onError(WsErrorContext context) {

}

@Override
public void send(String content) {
for (var client : this.connectedClients) {
client.send(content);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dev.httpmarco.polocloud.modules.rest.socket.web.impl.v1;

import com.google.gson.Gson;
import dev.httpmarco.polocloud.api.event.impl.services.ServiceStartEvent;
import dev.httpmarco.polocloud.api.event.impl.services.ServiceStoppingEvent;
import dev.httpmarco.polocloud.modules.rest.RestModule;
import dev.httpmarco.polocloud.modules.rest.socket.WebSocket;
import dev.httpmarco.polocloud.node.Node;
import io.javalin.websocket.WsCloseContext;
import io.javalin.websocket.WsConnectContext;
import io.javalin.websocket.WsErrorContext;
import io.javalin.websocket.WsMessageContext;

import java.util.Map;

public class NotifyWebSocket extends WebSocket {

public NotifyWebSocket(RestModule restModule) {
super("/notify", "polocloud.notify", restModule);
}

@Override
public void onConnect(WsConnectContext context) {

/*
* We need this for every notification type
* Node.instance().eventProvider().listen(.class, event -> {
*
* });
*/

/*
* Whe notify the client when a new service starts
*/
Node.instance().eventProvider().listen(ServiceStartEvent.class, event -> {
var service = event.service();

sendNotification("service-start", Map.of(
"id", service.id(),
"name", service.name()
));
});

/*
* Whe notify the client when a new service stops
*/
Node.instance().eventProvider().listen(ServiceStoppingEvent.class, event -> {
var service = event.service();

sendNotification("service-stop", Map.of(
"id", service.id(),
"name", service.name()
));
});

connectedClients().add(context);
context.enableAutomaticPings();
}

@Override
public void onClose(WsCloseContext context) {
connectedClients().remove(context);
}

@Override
public void onMessage(WsMessageContext context) {

}

@Override
public void onError(WsErrorContext context) {

}

public void sendNotification(String type, Object data) {
var message = Map.of(
"type", type,
"data", data
);

send(new Gson().toJson(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public void prepare(@NotNull PlatformGroupDisplay display, @NotNull ClusterLocal
target.getParent().toFile().mkdirs();
Files.createFile(target);
}
case APPEND_OR_REPLACE -> {
if (!Files.exists(target)) {
target.getParent().toFile().mkdirs();
Files.createFile(target);
}
}
}


Expand All @@ -77,8 +83,8 @@ public void prepare(@NotNull PlatformGroupDisplay display, @NotNull ClusterLocal
var content = replacement.value()
.replaceAll("%hostname%", service.hostname())
.replaceAll("%port%", String.valueOf(service.port()))
.replaceAll("%bungeecord_use%", String.valueOf(Node.instance().groupProvider().groups().stream().anyMatch(it -> it.platform().platform().equalsIgnoreCase("bungeecord"))));

.replaceAll("%bungeecord_use%", String.valueOf(Node.instance().groupProvider().groups().stream().anyMatch(it -> it.platform().platform().equalsIgnoreCase("bungeecord"))))
.replaceAll("%velocity_use%", String.valueOf(Node.instance().groupProvider().groups().stream().anyMatch(it -> it.platform().platform().equalsIgnoreCase("velocity"))));
replacer.rewrite(s -> s.startsWith(replacement.indicator()), fileType.replacer().apply(new Pair<>(replacement.indicator(), content)));
}
replacer.write();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
public enum PlatformFileStrategy {

COPY_FROM_CLASSPATH_IF_NOT_EXISTS,
DIRECT_CREATE

// CREATE OR OVERWRITE EXISTING FILES
DIRECT_CREATE,

// if file exists, replace the content
// if not, create the file
APPEND_OR_REPLACE

}
11 changes: 8 additions & 3 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@
},
{
"file": "config/paper-global.yml",
"strategy": "DIRECT_CREATE",
"replacements": [],
"strategy": "APPEND_OR_REPLACE",
"replacements": [
{
"indicator": " enabled",
"value": "%velocity_use%"
}
],
"appends": [
"proxies:\n velocity:\n enabled: %velocity_use%\n secret: %forwarding_secret%"
]
Expand Down Expand Up @@ -661,7 +666,7 @@
"url": "https://meta.fabricmc.net/v2/versions/loader/1.19.2/0.16.9/1.0.1/server/jar"
},
{
"version": "1.19.2",
"version": "1.19.1",
"url": "https://meta.fabricmc.net/v2/versions/loader/1.19.1/0.16.9/1.0.1/server/jar"
},
{
Expand Down

0 comments on commit d0d2891

Please sign in to comment.