Changeset View
Changeset View
Standalone View
Standalone View
java/server/src/main/java/org/softwareheritage/graph/App.java
Show All 17 Lines | |||||
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.SwhId; | ||||
import org.softwareheritage.graph.algo.Stats; | import org.softwareheritage.graph.algo.Stats; | ||||
/** | /** | ||||
* Entrypoint 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 | ||||
*/ | */ | ||||
public class App { | public class App { | ||||
/** | |||||
* Main entrypoint. | |||||
* | |||||
* @param args command line arguments | |||||
*/ | |||||
public static void main(String[] args) throws IOException, JSAPException { | public static void main(String[] args) throws IOException, JSAPException { | ||||
SimpleJSAP jsap = new SimpleJSAP( | SimpleJSAP jsap = new SimpleJSAP( | ||||
App.class.getName(), | App.class.getName(), | ||||
"Server to load and query a compressed graph representation of Software Heritage archive.", | "Server to load and query a compressed graph representation of Software Heritage archive.", | ||||
new Parameter[] { | new Parameter[] { | ||||
new FlaggedOption("port", JSAP.INTEGER_PARSER, "5009", JSAP.NOT_REQUIRED, 'p', "port", | new FlaggedOption("port", JSAP.INTEGER_PARSER, "5009", JSAP.NOT_REQUIRED, 'p', "port", | ||||
"Binding port of the server."), | "Binding port of the server."), | ||||
new UnflaggedOption("graphPath", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, | new UnflaggedOption("graphPath", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.REQUIRED, | ||||
JSAP.NOT_GREEDY, "The basename of the compressed graph."), | JSAP.NOT_GREEDY, "The basename of the compressed graph."), | ||||
} | } | ||||
); | ); | ||||
JSAPResult config = jsap.parse(args); | JSAPResult config = jsap.parse(args); | ||||
if (jsap.messagePrinted()) { | if (jsap.messagePrinted()) { | ||||
System.exit(1); | System.exit(1); | ||||
} | } | ||||
String graphPath = config.getString("graphPath"); | String graphPath = config.getString("graphPath"); | ||||
int port = config.getInt("port"); | int port = config.getInt("port"); | ||||
startServer(graphPath, port); | startServer(graphPath, port); | ||||
} | } | ||||
/** | |||||
* Loads compressed graph and starts the web server to query it. | |||||
* | |||||
* @param graphPath basename of the compressed graph | |||||
* @param port binding port of the server | |||||
*/ | |||||
private static void startServer(String graphPath, int port) throws IOException { | private static void startServer(String graphPath, int port) throws IOException { | ||||
Graph graph = new Graph(graphPath); | Graph graph = new Graph(graphPath); | ||||
Stats stats = new Stats(graphPath); | Stats stats = new Stats(graphPath); | ||||
// Clean up on exit | // Clean up on exit | ||||
Runtime.getRuntime().addShutdownHook(new Thread() { | Runtime.getRuntime().addShutdownHook(new Thread() { | ||||
public void run() { | public void run() { | ||||
try { | try { | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | private static void startServer(String graphPath, int port) throws IOException { | ||||
}); | }); | ||||
app.exception(IllegalArgumentException.class, (e, ctx) -> { | app.exception(IllegalArgumentException.class, (e, ctx) -> { | ||||
ctx.status(400); | ctx.status(400); | ||||
ctx.result(e.getMessage()); | ctx.result(e.getMessage()); | ||||
}); | }); | ||||
} | } | ||||
/** | |||||
* Checks query strings names provided to the REST API. | |||||
* | |||||
* @param ctx Javalin HTTP request context | |||||
* @param allowedFmt a regular expression describing allowed query strings names | |||||
* @throws IllegalArgumentException unknown query string provided | |||||
*/ | |||||
private static void checkQueryStrings(Context ctx, String allowedFmt) { | private static void checkQueryStrings(Context ctx, String allowedFmt) { | ||||
Map<String, List<String>> queryParamMap = ctx.queryParamMap(); | Map<String, List<String>> queryParamMap = ctx.queryParamMap(); | ||||
for (String key : queryParamMap.keySet()) { | for (String key : queryParamMap.keySet()) { | ||||
if (!key.matches(allowedFmt)) { | if (!key.matches(allowedFmt)) { | ||||
throw new IllegalArgumentException("Unknown query string: " + key); | throw new IllegalArgumentException("Unknown query string: " + key); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } |