Changeset View
Changeset View
Standalone View
Standalone View
java/server/src/main/java/org/softwareheritage/graph/App.java
Show All 15 Lines | |||||
import com.martiansoftware.jsap.Switch; | import com.martiansoftware.jsap.Switch; | ||||
import com.martiansoftware.jsap.UnflaggedOption; | import com.martiansoftware.jsap.UnflaggedOption; | ||||
import io.javalin.Javalin; | import io.javalin.Javalin; | ||||
import io.javalin.http.Context; | import io.javalin.http.Context; | ||||
import io.javalin.plugin.json.JavalinJackson; | import io.javalin.plugin.json.JavalinJackson; | ||||
import org.softwareheritage.graph.Endpoint; | import org.softwareheritage.graph.Endpoint; | ||||
import org.softwareheritage.graph.Graph; | import org.softwareheritage.graph.Graph; | ||||
import org.softwareheritage.graph.SwhId; | import org.softwareheritage.graph.SwhPID; | ||||
import org.softwareheritage.graph.algo.Stats; | import org.softwareheritage.graph.algo.Stats; | ||||
/** | /** | ||||
* Web framework of the swh-graph server REST API. | * Web framework of the swh-graph server REST API. | ||||
* | * | ||||
* @author Thibault Allançon | * @author Thibault Allançon | ||||
* @version 0.0.1 | * @version 0.0.1 | ||||
* @since 0.0.1 | * @since 0.0.1 | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | private static void startServer(String graphPath, int port, boolean showTimings) throws IOException { | ||||
app.before("/walk/*", ctx -> { checkQueryStrings(ctx, "direction|edges|traversal"); }); | app.before("/walk/*", ctx -> { checkQueryStrings(ctx, "direction|edges|traversal"); }); | ||||
app.get("/stats/", ctx -> { ctx.json(stats); }); | app.get("/stats/", ctx -> { ctx.json(stats); }); | ||||
// Graph traversal endpoints | // Graph traversal endpoints | ||||
// By default the traversal is a forward DFS using all edges | // By default the traversal is a forward DFS using all edges | ||||
app.get("/leaves/:src", ctx -> { | app.get("/leaves/:src", ctx -> { | ||||
SwhId src = new SwhId(ctx.pathParam("src")); | SwhPID src = new SwhPID(ctx.pathParam("src")); | ||||
String direction = ctx.queryParam("direction", "forward"); | String direction = ctx.queryParam("direction", "forward"); | ||||
String edgesFmt = ctx.queryParam("edges", "*"); | String edgesFmt = ctx.queryParam("edges", "*"); | ||||
Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | ||||
Endpoint.Output output = endpoint.leaves(src); | Endpoint.Output output = endpoint.leaves(src); | ||||
ctx.json(formatEndpointOutput(output, showTimings)); | ctx.json(formatEndpointOutput(output, showTimings)); | ||||
}); | }); | ||||
app.get("/neighbors/:src", ctx -> { | app.get("/neighbors/:src", ctx -> { | ||||
SwhId src = new SwhId(ctx.pathParam("src")); | SwhPID src = new SwhPID(ctx.pathParam("src")); | ||||
String direction = ctx.queryParam("direction", "forward"); | String direction = ctx.queryParam("direction", "forward"); | ||||
String edgesFmt = ctx.queryParam("edges", "*"); | String edgesFmt = ctx.queryParam("edges", "*"); | ||||
Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | ||||
Endpoint.Output output = endpoint.neighbors(src); | Endpoint.Output output = endpoint.neighbors(src); | ||||
ctx.json(formatEndpointOutput(output, showTimings)); | ctx.json(formatEndpointOutput(output, showTimings)); | ||||
}); | }); | ||||
app.get("/visit/nodes/:src", ctx -> { | app.get("/visit/nodes/:src", ctx -> { | ||||
SwhId src = new SwhId(ctx.pathParam("src")); | SwhPID src = new SwhPID(ctx.pathParam("src")); | ||||
String direction = ctx.queryParam("direction", "forward"); | String direction = ctx.queryParam("direction", "forward"); | ||||
String edgesFmt = ctx.queryParam("edges", "*"); | String edgesFmt = ctx.queryParam("edges", "*"); | ||||
Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | ||||
Endpoint.Output output = endpoint.visitNodes(src); | Endpoint.Output output = endpoint.visitNodes(src); | ||||
ctx.json(formatEndpointOutput(output, showTimings)); | ctx.json(formatEndpointOutput(output, showTimings)); | ||||
}); | }); | ||||
app.get("/visit/paths/:src", ctx -> { | app.get("/visit/paths/:src", ctx -> { | ||||
SwhId src = new SwhId(ctx.pathParam("src")); | SwhPID src = new SwhPID(ctx.pathParam("src")); | ||||
String direction = ctx.queryParam("direction", "forward"); | String direction = ctx.queryParam("direction", "forward"); | ||||
String edgesFmt = ctx.queryParam("edges", "*"); | String edgesFmt = ctx.queryParam("edges", "*"); | ||||
Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | ||||
Endpoint.Output output = endpoint.visitPaths(src); | Endpoint.Output output = endpoint.visitPaths(src); | ||||
ctx.json(formatEndpointOutput(output, showTimings)); | ctx.json(formatEndpointOutput(output, showTimings)); | ||||
}); | }); | ||||
app.get("/walk/:src/:dst", ctx -> { | app.get("/walk/:src/:dst", ctx -> { | ||||
SwhId src = new SwhId(ctx.pathParam("src")); | SwhPID src = new SwhPID(ctx.pathParam("src")); | ||||
String dstFmt = ctx.pathParam("dst"); | String dstFmt = ctx.pathParam("dst"); | ||||
String direction = ctx.queryParam("direction", "forward"); | String direction = ctx.queryParam("direction", "forward"); | ||||
String edgesFmt = ctx.queryParam("edges", "*"); | String edgesFmt = ctx.queryParam("edges", "*"); | ||||
String algorithm = ctx.queryParam("traversal", "dfs"); | String algorithm = ctx.queryParam("traversal", "dfs"); | ||||
Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | Endpoint endpoint = new Endpoint(graph, direction, edgesFmt); | ||||
Endpoint.Output output = endpoint.walk(src, dstFmt, algorithm); | Endpoint.Output output = endpoint.walk(src, dstFmt, algorithm); | ||||
ctx.json(formatEndpointOutput(output, showTimings)); | ctx.json(formatEndpointOutput(output, showTimings)); | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |