summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-08-01 22:06:28 +0800
committerKeuin <[email protected]>2022-08-01 22:06:28 +0800
commit999424c5a0d0555950ebff06a69a181ebf2325ea (patch)
treedead4877975963ba763201e43f833b15ba1ae047 /src
parentf97b39768461c49057b48967dc461351bc07b1f9 (diff)
Bind and initialize ApiEndpoint.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/keuin/crosslink/api/ApiServer.java19
-rw-r--r--src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java4
2 files changed, 18 insertions, 5 deletions
diff --git a/src/main/java/com/keuin/crosslink/api/ApiServer.java b/src/main/java/com/keuin/crosslink/api/ApiServer.java
index 8a3bbce..2570cb0 100644
--- a/src/main/java/com/keuin/crosslink/api/ApiServer.java
+++ b/src/main/java/com/keuin/crosslink/api/ApiServer.java
@@ -7,6 +7,8 @@ import com.keuin.crosslink.api.request.JsonHttpExchange;
import com.keuin.crosslink.api.request.JsonReqHandler;
import com.keuin.crosslink.data.PlayerInfo;
import com.keuin.crosslink.data.ServerInfo;
+import com.keuin.crosslink.messaging.endpoint.system.ApiEndpoint;
+import com.keuin.crosslink.messaging.sender.ISender;
import com.keuin.crosslink.plugin.common.ICoreAccessor;
import com.keuin.crosslink.util.LoggerNaming;
import com.sun.net.httpserver.HttpServer;
@@ -16,7 +18,9 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
import java.util.Objects;
+import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@@ -105,16 +109,23 @@ public class ApiServer implements IApiServer {
@Override
protected void handle(JsonHttpExchange exchange) throws IOException {
var req = exchange.getRequestBody();
- var sender = req.get("sender");
- var message = req.get("message");
- if (!sender.isTextual() || !message.isTextual()) {
+ var sender = req.get("sender").textValue();
+ var message = req.get("message").textValue();
+ if (sender == null || message == null) {
exchange.setResponseCode(400);
var resp = exchange.getResponseBody();
resp.put("success", false);
resp.put("message", "Illegal parameter type.");
return;
}
-
+ // the message will be sent from ApiEndpoint (system:api)
+ ApiEndpoint.INSTANCE.offerMessage(
+ ISender.create(
+ sender,
+ UUID.nameUUIDFromBytes(sender.getBytes(StandardCharsets.UTF_8))
+ ),
+ message
+ );
}
})
.build().forEach((p, h) -> server.createContext(p).setHandler(h));
diff --git a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java
index 69b4299..dff53a5 100644
--- a/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java
+++ b/src/main/java/com/keuin/crosslink/plugin/common/PluginMain.java
@@ -12,6 +12,7 @@ import com.keuin.crosslink.messaging.config.remote.InvalidEndpointConfigurationE
import com.keuin.crosslink.messaging.config.remote.RemoteEndpointFactory;
import com.keuin.crosslink.messaging.config.router.RouterConfigurer;
import com.keuin.crosslink.messaging.endpoint.IEndpoint;
+import com.keuin.crosslink.messaging.endpoint.system.ApiEndpoint;
import com.keuin.crosslink.messaging.router.IRouter;
import com.keuin.crosslink.plugin.common.environ.PluginEnvironment;
import com.keuin.crosslink.util.LoggerNaming;
@@ -103,7 +104,8 @@ public final class PluginMain {
}
// API endpoint (send messages from HTTP api)
-
+ ApiEndpoint.INSTANCE.setRouter(messageRouter);
+ messageRouter.addEndpoint(ApiEndpoint.INSTANCE);
// register all endpoints on the router
for (IEndpoint ep : endpoints) {