Skip to content

Commit

Permalink
Add peer admin REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 23, 2024
1 parent 28b4acb commit 50862d7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions convex-restapi/src/main/java/convex/restapi/RESTServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import convex.restapi.api.ChainAPI;
import convex.restapi.api.DLAPI;
import convex.restapi.api.DepAPI;
import convex.restapi.api.PeerAdminAPI;
import io.javalin.Javalin;
import io.javalin.community.ssl.SslPlugin;
import io.javalin.config.JavalinConfig;
Expand Down Expand Up @@ -129,13 +130,18 @@ protected void addOpenApiPlugins(JavalinConfig config) {
protected ChainAPI chainAPI;
protected DepAPI depAPI;
protected DLAPI dlAPI;
protected PeerAdminAPI peerAPI;

private void addAPIRoutes(Javalin app) {
chainAPI = new ChainAPI(this);
chainAPI.addRoutes(app);

depAPI = new DepAPI(this);
depAPI.addRoutes(app);

peerAPI = new PeerAdminAPI(this);
peerAPI.addRoutes(app);


dlAPI = new DLAPI(this);
dlAPI.addRoutes(app);
Expand Down
74 changes: 74 additions & 0 deletions convex-restapi/src/main/java/convex/restapi/api/PeerAdminAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package convex.restapi.api;

import java.util.Arrays;
import java.util.HashSet;

import convex.peer.Server;
import convex.restapi.RESTServer;
import convex.restapi.model.CreateAccountResponse;
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.InternalServerErrorResponse;
import io.javalin.openapi.HttpMethod;
import io.javalin.openapi.OpenApi;
import io.javalin.openapi.OpenApiContent;
import io.javalin.openapi.OpenApiResponse;

public class PeerAdminAPI extends ABaseAPI {

public Server server;

public PeerAdminAPI(RESTServer restServer) {
super(restServer);
server = restServer.getServer();
}

private static final String ROUTE = "/api/v1/";

@Override
public void addRoutes(Javalin app) {
String prefix = ROUTE;

app.post(prefix + "peer/shutdown", this::shutDown);

}

@OpenApi(path = ROUTE + "peer/shutdown",
methods = HttpMethod.POST,
operationId = "shutdownPeer",
tags = { "Admin"},
summary = "Shut down the current peer",
responses = {
@OpenApiResponse(
status = "200",
description = "Peer shutdown initiated",
content = {
@OpenApiContent(
type = "application/json",
from = CreateAccountResponse.class) })
})
public void shutDown(Context ctx) {
ensureLocalAdmin(ctx);
try {
server.shutdown();
} catch (Exception e) {
e.printStackTrace();
throw new InternalServerErrorResponse("Failed to initiate sutdown: "+e.getMessage());
}
ctx.result("Shutdown initiated.");
}

private HashSet<String> authorisedIPs = new HashSet<String>(Arrays.asList("127.0.0.1","::1","[0:0:0:0:0:0:0:1]"));

private void ensureLocalAdmin(Context ctx) {
String ip=ctx.ip();
if (authorisedIPs.contains(ip)) {
return;
} else {
throw new io.javalin.http.UnauthorizedResponse("Can't performa admin actions from IP: "+ip);
}
}



}

0 comments on commit 50862d7

Please sign in to comment.