Update man page

This commit is contained in:
AsamK 2022-11-02 17:46:20 +01:00
parent 1ad0e94b64
commit c628e27d2e
5 changed files with 59 additions and 49 deletions

View file

@ -73,7 +73,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
subparser.addArgument("--http")
.nargs("?")
.setConst("localhost:8080")
.help("Expose a JSON-RPC interface as http endpoint.");
.help("Expose a JSON-RPC interface as http endpoint (default localhost:8080).");
subparser.addArgument("--no-receive-stdout")
.help("Dont print received messages to stdout.")
.action(Arguments.storeTrue());

View file

@ -41,54 +41,56 @@ public class HttpServerHandler {
public void init() throws IOException {
logger.info("Starting server on " + address.toString());
logger.info("Starting server on " + address.toString());
final var server = HttpServer.create(address, 0);
server.setExecutor(Executors.newFixedThreadPool(10));
final var server = HttpServer.create(address, 0);
server.setExecutor(Executors.newFixedThreadPool(10));
server.createContext("/api/v1/rpc", httpExchange -> {
server.createContext("/api/v1/rpc", httpExchange -> {
if (!"POST".equals(httpExchange.getRequestMethod())) {
sendResponse(405, null, httpExchange);
return;
}
if (!"POST".equals(httpExchange.getRequestMethod())) {
sendResponse(405, null, httpExchange);
return;
}
if (!"application/json".equals(httpExchange.getRequestHeaders().getFirst("Content-Type"))) {
sendResponse(415, null, httpExchange);
return;
}
if (!"application/json".equals(httpExchange.getRequestHeaders().getFirst("Content-Type"))) {
sendResponse(415, null, httpExchange);
return;
}
try {
try {
final Object[] result = {null};
final var jsonRpcSender = new JsonRpcSender(s -> {
if (result[0] != null) {
throw new AssertionError("There should only be a single JSON-RPC response");
}
result[0] = s;
});
final var jsonRpcReader = new JsonRpcReader(jsonRpcSender, httpExchange.getRequestBody());
jsonRpcReader.readMessages((method, params) -> commandHandler.handleRequest(objectMapper, method, params),
response -> logger.debug("Received unexpected response for id {}", response.getId()));
if (result[0] !=null) {
sendResponse(200, result[0], httpExchange);
} else {
sendResponse(201, null, httpExchange);
final Object[] result = {null};
final var jsonRpcSender = new JsonRpcSender(s -> {
if (result[0] != null) {
throw new AssertionError("There should only be a single JSON-RPC response");
}
}
catch (Throwable aEx) {
logger.error("Failed to process request.", aEx);
sendResponse(200, JsonRpcResponse.forError(
new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
"An internal server error has occurred.", null), null), httpExchange);
}
});
result[0] = s;
});
server.start();
final var jsonRpcReader = new JsonRpcReader(jsonRpcSender, httpExchange.getRequestBody());
jsonRpcReader.readMessages((method, params) -> commandHandler.handleRequest(objectMapper,
method,
params), response -> logger.debug("Received unexpected response for id {}", response.getId()));
if (result[0] != null) {
sendResponse(200, result[0], httpExchange);
} else {
sendResponse(201, null, httpExchange);
}
} catch (Throwable aEx) {
logger.error("Failed to process request.", aEx);
sendResponse(200,
JsonRpcResponse.forError(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
"An internal server error has occurred.",
null), null),
httpExchange);
}
});
server.start();
}