Skip to content

Commit

Permalink
Updates for REST API server
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 8, 2024
1 parent ca840fb commit 5a2e4f2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
8 changes: 4 additions & 4 deletions convex-cli/src/main/java/convex/cli/local/LocalStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ private List<AKeyPair> getPeerKeyPairs(int n) {
}
int left=n-keyPairList.size();
if (left>0) {
log.warn("Insufficient key pairs specified. Additional "+left+" keypair(s) will be generated");
informWarning("Insufficient key pairs specified. Additional "+left+" keypair(s) will be generated");
for (int i=0; i<left; i++) {
AKeyPair kp=AKeyPair.generate();
keyPairList.add(kp);
log.warn("Generated key: "+kp.getAccountKey().toChecksumHex()+" Priv: "+kp.getSeed());
log.info("Generated key: "+kp.getAccountKey().toChecksumHex()+" Priv: "+kp.getSeed());
}
}

Expand All @@ -111,7 +111,7 @@ public void run() {
try {
peerPorts = Helpers.getPortList(ports, count);
} catch (NumberFormatException e) {
log.warn("cannot convert port number " + e);
informWarning("Cannot convert port number " + e);
return;
}
if (peerPorts.length < count) {
Expand All @@ -130,7 +130,7 @@ public void run() {
}
launchRestAPI(servers.get(0));
} catch (Throwable t) {
log.warn("Failed to start REST server: "+t);
informWarning("Failed to start REST server: "+t);
}

// Loop until we end
Expand Down
2 changes: 1 addition & 1 deletion convex-restapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<properties>
<json.simple.version>1.1.1</json.simple.version>
<javalin.version>6.1.6</javalin.version>
<javalin.version>6.2.0</javalin.version>
</properties>

<build>
Expand Down
20 changes: 17 additions & 3 deletions convex-restapi/src/main/java/convex/restapi/RESTServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.javalin.community.ssl.SslPlugin;
import io.javalin.config.JavalinConfig;
import io.javalin.http.staticfiles.Location;
import io.javalin.openapi.JsonSchemaLoader;
import io.javalin.openapi.JsonSchemaResource;
import io.javalin.openapi.plugin.OpenApiPlugin;
import io.javalin.openapi.plugin.redoc.ReDocPlugin;
import io.javalin.openapi.plugin.swagger.SwaggerPlugin;
Expand Down Expand Up @@ -98,18 +100,30 @@ protected SslPlugin getSSLPlugin(HashMap<Keyword, Object> config) {
}

protected void addOpenApiPlugins(JavalinConfig config) {
String docsPath="openapi-plugin/openapi-default.json";

config.registerPlugin(new OpenApiPlugin(pluginConfig -> {

pluginConfig.withDefinitionConfiguration((version, definition) -> {
pluginConfig
.withDocumentationPath(docsPath)
.withDefinitionConfiguration((version, definition) -> {
definition.withOpenApiInfo(info -> {
info.setTitle("Convex REST API");
info.setVersion("0.1.1");
});
});
}));

config.registerPlugin(new SwaggerPlugin());
config.registerPlugin(new ReDocPlugin());
config.registerPlugin(new SwaggerPlugin(swaggerConfiguration->{
swaggerConfiguration.setDocumentationPath(docsPath);
}));
config.registerPlugin(new ReDocPlugin(reDocConfiguration -> {
reDocConfiguration.setDocumentationPath(docsPath);
}));

for (JsonSchemaResource generatedJsonSchema : new JsonSchemaLoader().loadGeneratedSchemes()) {
System.out.println(generatedJsonSchema.getName());
}
}

protected ChainAPI chainAPI;
Expand Down
13 changes: 9 additions & 4 deletions convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import convex.core.util.Utils;
import convex.java.JSON;
import convex.restapi.RESTServer;
import convex.restapi.model.CreateAccountRequest;
import io.javalin.Javalin;
import io.javalin.http.BadRequestResponse;
import io.javalin.http.Context;
Expand Down Expand Up @@ -112,11 +113,15 @@ public void getData(Context ctx) {
operationId = "createAccount",
summary="Create a new Convex account",
requestBody = @OpenApiRequestBody(
description = "Complex bodies",
description = "Create Account request, must provide an accountKey for the new Account",
content= {@OpenApiContent(
from = String.class,
// type = "application/json",
example = "{}")}))
from = CreateAccountRequest.class,
type = "application/json",
exampleObjects = {
@OpenApiExampleProperty(name="accountKey", value = "d82e78594610f708ad47f666bbacbab1711760652cb88bf7515ed6c3ae84a08d")
}
//example = "{\"accountKey\": \"d82e78594610f708ad47f666bbacbab1711760652cb88bf7515ed6c3ae84a08d\"}")}
)}))
public void createAccount(Context ctx) {
Map<String, Object> req=getJSONBody(ctx);
Object key = req.get("accountKey");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package convex.restapi.model;

import io.javalin.openapi.OpenApiByFields;

@OpenApiByFields
public class CreateAccountRequest {
public String accountKey;
}

0 comments on commit 5a2e4f2

Please sign in to comment.