• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-query / 24981500304

27 Apr 2026 07:11AM UTC coverage: 56.977% (+0.8%) from 56.165%
24981500304

push

github

web-flow
Merge pull request #86 from knowledgepixels/feature/62-phase-3b-spaces-route

feat: add /spaces listing route with HTML and JSON output (#62)

405 of 804 branches covered (50.37%)

Branch coverage included in aggregate %.

1163 of 1948 relevant lines covered (59.7%)

8.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/query/MainVerticle.java
1
package com.knowledgepixels.query;
2

3
import com.github.jsonldjava.shaded.com.google.common.base.Charsets;
4
import com.knowledgepixels.query.GrlcSpec.InvalidGrlcSpecException;
5
import io.micrometer.prometheus.PrometheusMeterRegistry;
6
import io.vertx.core.AbstractVerticle;
7
import io.vertx.core.Future;
8
import io.vertx.core.Promise;
9
import io.vertx.core.buffer.Buffer;
10
import io.vertx.core.http.*;
11
import io.vertx.ext.web.Router;
12
import io.vertx.ext.web.RoutingContext;
13
import io.vertx.ext.web.handler.CorsHandler;
14
import io.vertx.ext.web.handler.StaticHandler;
15
import io.vertx.ext.web.proxy.handler.ProxyHandler;
16
import io.vertx.httpproxy.*;
17
import io.vertx.micrometer.PrometheusScrapingHandler;
18
import io.vertx.micrometer.backends.BackendRegistries;
19
import org.eclipse.rdf4j.model.Value;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

23
import java.io.InputStream;
24
import java.net.URLEncoder;
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.List;
28
import java.util.Scanner;
29
import java.util.concurrent.Executors;
30
import java.util.concurrent.TimeUnit;
31

32
/**
33
 * Main verticle that coordinates the incoming HTTP requests.
34
 */
35
@GeneratedFlagForDependentElements
36
public class MainVerticle extends AbstractVerticle {
37

38
    private static String css = null;
39

40
    private static final Logger log = LoggerFactory.getLogger(MainVerticle.class);
41

42
    /**
43
     * Start the main verticle.
44
     *
45
     * @param startPromise the promise to complete when the verticle is started
46
     * @throws Exception if an error occurs during startup
47
     */
48
    @Override
49
    public void start(Promise<Void> startPromise) throws Exception {
50
        if (!FeatureFlags.trustStateEnabled()) {
51
            log.warn("Trust state feature disabled via NANOPUB_QUERY_ENABLE_TRUST_STATE=false — "
52
                    + "no trust snapshots will be fetched or materialised, and the 'trust' repo will not be auto-created.");
53
        }
54
        if (!FeatureFlags.spacesEnabled()) {
55
            log.warn("Spaces feature disabled via NANOPUB_QUERY_ENABLE_SPACES=false — "
56
                    + "no space-relevant nanopubs will be extracted into npa:spacesGraph, "
57
                    + "and the 'spaces' repo will not be auto-created.");
58
        }
59
        if (!FeatureFlags.fullRepoEnabled()) {
60
            log.warn("Writes to the 'full' repo disabled via NANOPUB_QUERY_ENABLE_FULL_REPO=false — "
61
                    + "generic SPARQL queries against /repo/full will return an empty store.");
62
        }
63
        if (!FeatureFlags.textRepoEnabled()) {
64
            log.warn("Writes to the 'text' repo disabled via NANOPUB_QUERY_ENABLE_TEXT_REPO=false — "
65
                    + "full-text search via /repo/text will return nothing.");
66
        }
67
        if (!FeatureFlags.last30dRepoEnabled()) {
68
            log.warn("Writes to the 'last30d' repo disabled via NANOPUB_QUERY_ENABLE_LAST30D_REPO=false — "
69
                    + "the /repo/last30d endpoint will be empty; rewrite queries against /repo/full with a date filter.");
70
        }
71
        HttpClient httpClient = vertx.createHttpClient(
72
                new HttpClientOptions()
73
                        .setConnectTimeout(Utils.getEnvInt("NANOPUB_QUERY_VERTX_CONNECT_TIMEOUT", 1000))
74
                        .setIdleTimeoutUnit(TimeUnit.SECONDS)
75
                        .setIdleTimeout(Utils.getEnvInt("NANOPUB_QUERY_VERTX_IDLE_TIMEOUT", 60))
76
                        .setReadIdleTimeout(Utils.getEnvInt("NANOPUB_QUERY_VERTX_IDLE_TIMEOUT", 60))
77
                        .setWriteIdleTimeout(Utils.getEnvInt("NANOPUB_QUERY_VERTX_IDLE_TIMEOUT", 60)),
78
                new PoolOptions().setHttp1MaxSize(200).setHttp2MaxSize(200)
79
        );
80

81
        HttpServer proxyServer = vertx.createHttpServer(
82
                new HttpServerOptions().setMaxInitialLineLength(65536)
83
        );
84
        Router proxyRouter = Router.router(vertx);
85
        proxyRouter.route().handler(CorsHandler.create().addRelativeOrigin(".*"));
86

87
        // Metrics
88
        final var metricsHttpServer = vertx.createHttpServer();
89
        final var metricsRouter = Router.router(vertx);
90
        metricsHttpServer.requestHandler(metricsRouter).listen(9394);
91

92
        final var metricsRegistry = (PrometheusMeterRegistry) BackendRegistries.getDefaultNow();
93
        final var collector = new MetricsCollector(metricsRegistry);
94
        metricsRouter.route("/metrics").handler(PrometheusScrapingHandler.create(metricsRegistry));
95
        // ----------
96
        // This part is only used if the redirection is not done through Nginx.
97
        // See nginx.conf and this bug report: https://github.com/eclipse-rdf4j/rdf4j/discussions/5120
98
        HttpProxy rdf4jProxy = HttpProxy.reverseProxy(httpClient);
99
        String proxy = Utils.getEnvString("RDF4J_PROXY_HOST", "rdf4j");
100
        int proxyPort = Utils.getEnvInt("RDF4J_PROXY_PORT", 8080);
101
        rdf4jProxy.origin(proxyPort, proxy);
102

103
        rdf4jProxy.addInterceptor(new ProxyInterceptor() {
×
104

105
            @Override
106
            @GeneratedFlagForDependentElements
107
            public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
108
                ProxyRequest request = context.request();
109
                request.setURI(request.getURI().replaceAll("/", "_").replaceFirst("^_repo_", "/rdf4j-server/repositories/"));
110
                // For later to try to get HTML tables out:
111
//                                if (request.headers().get("Accept") == null) {
112
//                                        request.putHeader("Accept", "text/html");
113
//                                }
114
//                                request.putHeader("Accept", "application/json");
115
                return ProxyInterceptor.super.handleProxyRequest(context);
116
            }
117

118
            @Override
119
            @GeneratedFlagForDependentElements
120
            public Future<Void> handleProxyResponse(ProxyContext context) {
121
                ProxyResponse resp = context.response();
122
                resp.putHeader("Access-Control-Allow-Origin", "*");
123
                resp.putHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
124
                // For later to try to get HTML tables out:
125
//                                String acceptHeader = context.request().headers().get("Accept");
126
//                                if (acceptHeader != null && acceptHeader.contains("text/html")) {
127
//                                        resp.putHeader("Content-Type", "text/html");
128
//                                        resp.setBody(Body.body(Buffer.buffer("<html><body><strong>test</strong></body></html>")));
129
//                                }
130
                return ProxyInterceptor.super.handleProxyResponse(context);
131
            }
132

133
        });
134
        // ----------
135

136
        proxyRouter.route(HttpMethod.GET, "/repo").handler(req -> handleRedirect(req, "/repo"));
137
        proxyRouter.route(HttpMethod.GET, "/repo/*").handler(ProxyHandler.create(rdf4jProxy));
138
        proxyRouter.route(HttpMethod.POST, "/repo/*").handler(ProxyHandler.create(rdf4jProxy));
139
        proxyRouter.route(HttpMethod.HEAD, "/repo/*").handler(ProxyHandler.create(rdf4jProxy));
140
        proxyRouter.route(HttpMethod.OPTIONS, "/repo/*").handler(ProxyHandler.create(rdf4jProxy));
141
        proxyRouter.route(HttpMethod.GET, "/tools/*").handler(req -> {
142
            final String yasguiPattern = "^/tools/([a-zA-Z0-9-_]+)(/([a-zA-Z0-9-_]+))?/yasgui\\.html$";
143
            if (req.normalizedPath().matches(yasguiPattern)) {
144
                String repo = req.normalizedPath().replaceFirst(yasguiPattern, "$1$2");
145
                req.response()
146
                        .putHeader("content-type", "text/html")
147
                        .end("<!DOCTYPE html>\n"
148
                             + "<html lang=\"en\">\n"
149
                             + "<head>\n"
150
                             + "<meta charset=\"utf-8\">\n"
151
                             + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"
152
                             + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
153
                             + "<title>Nanopub Query SPARQL Editor for repository: " + repo + "</title>\n"
154
                             + "<link rel=\"stylesheet\" href=\"/style.css\">\n"
155
                             + "<link href='https://cdn.jsdelivr.net/yasgui/2.6.1/yasgui.min.css' rel='stylesheet' type='text/css'/>\n"
156
                             + "<style>.yasgui .endpointText {display:none !important;}</style>\n"
157
                             + "<script type=\"text/javascript\">localStorage.clear();</script>\n"
158
                             + "</head>\n"
159
                             + "<body>\n"
160
                             + "<h3>Nanopub Query SPARQL Editor for repository: " + repo + "</h3>\n"
161
                             + "<div id='yasgui'></div>\n"
162
                             + "<script src='https://cdn.jsdelivr.net/yasgui/2.6.1/yasgui.min.js'></script>\n"
163
                             + "<script type=\"text/javascript\">\n"
164
                             + "var yasgui = YASGUI(document.getElementById(\"yasgui\"), {\n"
165
                             + "  yasqe:{sparql:{endpoint:'/repo/" + repo + "'},value:'" + Utils.defaultQuery.replaceAll("\n", "\\\\n") + "'}\n"
166
                             + "});\n"
167
                             + "</script>\n"
168
                             + "</body>\n"
169
                             + "</html>");
170
            } else {
171
                req.response()
172
                        .putHeader("content-type", "text/plain")
173
                        .setStatusCode(404)
174
                        .end("not found");
175
            }
176
        });
177
        proxyRouter.route(HttpMethod.GET, "/page").handler(req -> handleRedirect(req, "/page"));
178
        proxyRouter.route(HttpMethod.GET, "/page/*").handler(req -> {
179
            final String pagePattern = "^/page/([a-zA-Z0-9-_]+)(/([a-zA-Z0-9-_]+))?$";
180
            if (req.normalizedPath().matches(pagePattern)) {
181
                String repo = req.normalizedPath().replaceFirst(pagePattern, "$1$2");
182
                req.response()
183
                        .putHeader("content-type", "text/html")
184
                        .end("<!DOCTYPE html>\n"
185
                             + "<html lang=\"en\">\n"
186
                             + "<head>\n"
187
                             + "<meta charset=\"utf-8\">\n"
188
                             + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"
189
                             + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
190
                             + "<title>Nanopub Query repo: " + repo + "</title>\n"
191
                             + "<link rel=\"stylesheet\" href=\"/style.css\">\n"
192
                             + "</head>\n"
193
                             + "<body>\n"
194
                             + "<h3>Nanopub Query repo: " + repo + "</h3>\n"
195
                             + "<p>Endpoint: <a href=\"/repo/" + repo + "\">/repo/" + repo + "</a></p>"
196
                             + "<p>YASGUI: <a href=\"/tools/" + repo + "/yasgui.html\">/tools/" + repo + "/yasgui.hml</a></p>"
197
                             + "</body>\n"
198
                             + "</html>");
199
            } else {
200
                req.response()
201
                        .putHeader("content-type", "text/plain")
202
                        .setStatusCode(404)
203
                        .end("not found");
204
            }
205
        });
206
        proxyRouter.route(HttpMethod.GET, "/").handler(req -> {
207
            vertx.<String>executeBlocking(() -> {
208
                String repos = "";
209
                List<String> repoList = new ArrayList<>(TripleStore.get().getRepositoryNames());
210
                Collections.sort(repoList);
211
                for (String s : repoList) {
212
                    if (s.startsWith("pubkey_") || s.startsWith("type_")) continue;
213
                    repos += "<li><code><a href=\"/page/" + s + "\">" + s + "</a></code></li>";
214
                }
215
                String pinnedApisValue = Utils.getEnvString("NANOPUB_QUERY_PINNED_APIS", "");
216
                String[] pinnedApis = pinnedApisValue.split(" ");
217
                String pinnedApiLinks = "";
218
                if (!pinnedApisValue.isEmpty()) {
219
                    for (String s : pinnedApis) {
220
                        pinnedApiLinks = pinnedApiLinks + "<li><a href=\"openapi/?url=spec/" + s + "%3Fapi-version=latest\">" + s.replaceFirst("^.*/", "") + "</a></li>";
221
                    }
222
                    pinnedApiLinks = "<p>Pinned APIs:</p>\n" +
223
                                     "<ul>\n" +
224
                                     pinnedApiLinks +
225
                                     "</ul>\n";
226
                }
227
                return "<!DOCTYPE html>\n"
228
                     + "<html lang='en'>\n"
229
                     + "<head>\n"
230
                     + "<title>Nanopub Query</title>\n"
231
                     + "<meta charset='utf-8'>\n"
232
                     + "<link rel=\"stylesheet\" href=\"/style.css\">\n"
233
                     + "</head>\n"
234
                     + "<body>\n"
235
                     + "<h1>Nanopub Query</h1>"
236
                     + "<p>General repos:</p>"
237
                     + "<ul>" + repos + "</ul>"
238
                     + "<p>Specific repos:</p>"
239
                     + "<ul>"
240
                     + "<li><a href=\"/pubkeys\">Pubkey Repos</a></li>"
241
                     + "<li><a href=\"/types\">Type Repos</a></li>"
242
                     + "</ul>"
243
                     + (FeatureFlags.spacesEnabled()
244
                             ? "<p>Spaces:</p>"
245
                               + "<ul><li><a href=\"/spaces\">Spaces</a></li></ul>"
246
                             : "")
247
                     + pinnedApiLinks
248
                     + "</body>\n"
249
                     + "</html>";
250
            }, false).onSuccess(html -> {
251
                req.response().putHeader("content-type", "text/html").end(html);
252
            }).onFailure(ex -> {
253
                req.response().setStatusCode(500).end("Error: " + ex.getMessage());
254
            });
255
        });
256
        proxyRouter.route(HttpMethod.GET, "/pubkeys").handler(req -> {
257
            vertx.<String>executeBlocking(() -> {
258
                String repos = "";
259
                List<String> repoList = new ArrayList<>(TripleStore.get().getRepositoryNames());
260
                Collections.sort(repoList);
261
                for (String s : repoList) {
262
                    if (!s.startsWith("pubkey_")) continue;
263
                    String hash = s.replaceFirst("^([a-zA-Z0-9-]+)_([a-zA-Z0-9-_]+)$", "$2");
264
                    Value hashObj = Utils.getObjectForHash(hash);
265
                    String label;
266
                    if (hashObj == null) {
267
                        label = "";
268
                    } else {
269
                        label = " (" + Utils.getShortPubkeyName(hashObj.stringValue()) + ")";
270
                    }
271
                    s = s.replaceFirst("^([a-zA-Z0-9-]+)_([a-zA-Z0-9-_]+)$", "$1/$2");
272
                    repos += "<li><code><a href=\"/page/" + s + "\">" + s + "</a>" + label + "</code></li>";
273
                }
274
                return "<!DOCTYPE html>\n"
275
                     + "<html lang='en'>\n"
276
                     + "<head>\n"
277
                     + "<title>Nanopub Query: Pubkey Repos</title>\n"
278
                     + "<meta charset='utf-8'>\n"
279
                     + "<link rel=\"stylesheet\" href=\"/style.css\">\n"
280
                     + "</head>\n"
281
                     + "<body>\n"
282
                     + "<h3>Pubkey Repos</h3>"
283
                     + "<p>Repos:</p>"
284
                     + "<ul>" + repos + "</ul>"
285
                     + "</body>\n"
286
                     + "</html>";
287
            }, false).onSuccess(html -> {
288
                req.response().putHeader("content-type", "text/html").end(html);
289
            }).onFailure(ex -> {
290
                req.response().setStatusCode(500).end("Error: " + ex.getMessage());
291
            });
292
        });
293
        proxyRouter.route(HttpMethod.GET, "/types").handler(req -> {
294
            vertx.<String>executeBlocking(() -> {
295
                String repos = "";
296
                List<String> repoList = new ArrayList<>(TripleStore.get().getRepositoryNames());
297
                Collections.sort(repoList);
298
                for (String s : repoList) {
299
                    if (!s.startsWith("type_")) continue;
300
                    String hash = s.replaceFirst("^([a-zA-Z0-9-]+)_([a-zA-Z0-9-_]+)$", "$2");
301
                    Value hashObj = Utils.getObjectForHash(hash);
302
                    String label;
303
                    if (hashObj == null) {
304
                        label = "";
305
                    } else {
306
                        label = " (" + hashObj.stringValue() + ")";
307
                    }
308
                    s = s.replaceFirst("^([a-zA-Z0-9-]+)_([a-zA-Z0-9-_]+)$", "$1/$2");
309
                    repos += "<li><code><a href=\"/page/" + s + "\">" + s + "</a>" + label + "</code></li>";
310
                }
311
                return "<!DOCTYPE html>\n"
312
                     + "<html lang='en'>\n"
313
                     + "<head>\n"
314
                     + "<title>Nanopub Query: Type Repos</title>\n"
315
                     + "<meta charset='utf-8'>\n"
316
                     + "<link rel=\"stylesheet\" href=\"/style.css\">\n"
317
                     + "</head>\n"
318
                     + "<body>\n"
319
                     + "<h3>Type Repos</h3>"
320
                     + "<p>Repos:</p>"
321
                     + "<ul>" + repos + "</ul>"
322
                     + "</body>\n"
323
                     + "</html>";
324
            }, false).onSuccess(html -> {
325
                req.response().putHeader("content-type", "text/html").end(html);
326
            }).onFailure(ex -> {
327
                req.response().setStatusCode(500).end("Error: " + ex.getMessage());
328
            });
329
        });
330
        io.vertx.core.Handler<RoutingContext> spacesHandler = req -> {
331
            if (!FeatureFlags.spacesEnabled()) {
332
                req.response().setStatusCode(404)
333
                        .putHeader("content-type", "text/plain")
334
                        .end("Spaces feature is disabled");
335
                return;
336
            }
337
            // Path suffix wins over Accept header so /spaces.json is unambiguous.
338
            boolean wantJson = req.normalizedPath().endsWith(".json")
339
                    || "application/json".equalsIgnoreCase(req.request().getHeader("Accept"));
340
            vertx.<String>executeBlocking(() -> {
341
                var rows = SpacesListingRoute.fetchRows();
342
                return wantJson
343
                        ? SpacesListingRoute.renderJson(rows)
344
                        : SpacesListingRoute.renderHtml(rows);
345
            }, false).onSuccess(body -> {
346
                req.response().putHeader(
347
                        "content-type",
348
                        wantJson ? "application/json" : "text/html").end(body);
349
            }).onFailure(ex -> {
350
                req.response().setStatusCode(500).end("Error: " + ex.getMessage());
351
            });
352
        };
353
        proxyRouter.route(HttpMethod.GET, "/spaces").handler(spacesHandler);
354
        proxyRouter.route(HttpMethod.GET, "/spaces.json").handler(spacesHandler);
355
        proxyRouter.route(HttpMethod.GET, "/style.css").handler(req -> {
356
            if (css == null) {
357
                css = getResourceAsString("style.css");
358
            }
359
            req.response().end(css);
360
        });
361

362
        // TODO This is no longer needed and can be removed at some point:
363
        proxyRouter.route(HttpMethod.GET, "/grlc-spec/*").handler(req -> {
364
            vertx.<String>executeBlocking(() -> {
365
                GrlcSpec gsp = new GrlcSpec(req.normalizedPath(), req.queryParams());
366
                return gsp.getSpec();
367
            }, false).onSuccess(spec -> {
368
                req.response().putHeader("content-type", "text/yaml").end(spec);
369
            }).onFailure(ex -> {
370
                if (ex instanceof InvalidGrlcSpecException) {
371
                    req.response().setStatusCode(400).end(ex.getMessage());
372
                } else {
373
                    req.response().setStatusCode(500).end("Unexpected error: " + ex.getMessage());
374
                }
375
            });
376
        });
377

378
        proxyRouter.route(HttpMethod.GET, "/openapi/spec/*").handler(req -> {
379
            vertx.<String>executeBlocking(() -> {
380
                OpenApiSpecPage osp = new OpenApiSpecPage(req.normalizedPath(), req.queryParams());
381
                return osp.getSpec();
382
            }, false).onSuccess(spec -> {
383
                req.response().putHeader("content-type", "text/yaml").end(spec);
384
            }).onFailure(ex -> {
385
                if (ex instanceof InvalidGrlcSpecException) {
386
                    req.response().setStatusCode(400).end("Invalid grlc API definition: " + ex.getMessage());
387
                } else {
388
                    req.response().setStatusCode(500).end("Unexpected error: " + ex.getMessage());
389
                }
390
            });
391
        });
392

393
        proxyRouter.route("/openapi/*").handler(StaticHandler.create("com/knowledgepixels/query/swagger"));
394

395
        HttpProxy grlcxProxy = HttpProxy.reverseProxy(httpClient);
396
        grlcxProxy.origin(proxyPort, proxy);
397

398
        grlcxProxy.addInterceptor(new ProxyInterceptor() {
×
399

400
            @Override
401
            @GeneratedFlagForDependentElements
402
            public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
403
                final ProxyRequest req = context.request();
404
                final String apiPattern = "^/api/(RA[a-zA-Z0-9-_]{43})/([a-zA-Z0-9-_]+)([.]csv|[.]json|[.]srx)?([?].*)?$";
405
                if (req.getURI().matches(apiPattern)) {
406
                    try {
407
                        req.setMethod(HttpMethod.POST);
408
                        if (req.getURI().matches(".*[.]csv([?].*)?$")) {
409
                            req.putHeader("Accept", "text/csv");
410
                            req.setURI(req.getURI().replaceFirst("[.]csv([?].*)?$", "$1"));
411
                        } else if (req.getURI().matches(".*[.]json([?].*)?$")) {
412
                            req.putHeader("Accept", "application/json");
413
                            req.setURI(req.getURI().replaceFirst("[.]json([?].*)?$", "$1"));
414
                        } else if (req.getURI().matches(".*[.]srx([?].*)?$")) {
415
                            req.putHeader("Accept", "application/xml");
416
                            req.setURI(req.getURI().replaceFirst("[.]srx([?].*)?$", "$1"));
417
                        }
418
                        GrlcSpec grlcSpec = new GrlcSpec(req.getURI(), req.proxiedRequest().params());
419

420
                        // Variant 1:
421
                        req.putHeader("Content-Type", "application/sparql-query");
422
                        req.setBody(Body.body(Buffer.buffer(grlcSpec.expandQuery())));
423
                        // Variant 2:
424
                        //req.putHeader("Content-Type", "application/x-www-form-urlencoded");
425
                        //req.setBody(Body.body(Buffer.buffer("query=" + URLEncoder.encode(grlcSpec.getExpandedQueryContent(), Charsets.UTF_8))));
426

427
                        req.setURI("/rdf4j-server/repositories/" + grlcSpec.getRepoName());
428
                        log.info("Forwarding apix request to /rdf4j-server/repositories/", grlcSpec.getRepoName());
429
                    } catch (InvalidGrlcSpecException ex) {
430
                        return Future.succeededFuture(context.request()
431
                                .response()
432
                                .setStatusCode(400)
433
                                .putHeader("Content-Type", "text/plain")
434
                                .setBody(Body.body(Buffer.buffer("Bad request: " + ex.getMessage()))));
435
                    } catch (Exception ex) {
436
                        return Future.succeededFuture(context.request()
437
                                .response()
438
                                .setStatusCode(500)
439
                                .putHeader("Content-Type", "text/plain")
440
                                .setBody(Body.body(Buffer.buffer("Unexpected error: " + ex.getMessage()))));
441
                    }
442
                }
443
                return ProxyInterceptor.super.handleProxyRequest(context);
444
            }
445

446
            @Override
447
            @GeneratedFlagForDependentElements
448
            public Future<Void> handleProxyResponse(ProxyContext context) {
449
                log.info("Receiving api response");
450
                ProxyResponse resp = context.response();
451
                resp.putHeader("Access-Control-Allow-Origin", "*");
452
                resp.putHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
453
                resp.putHeader("Content-Disposition", "inline");
454
                return ProxyInterceptor.super.handleProxyResponse(context);
455
            }
456

457
        });
458
        proxyRouter.route(HttpMethod.GET, "/api/*").handler(ProxyHandler.create(grlcxProxy));
459

460
        // Handle HEAD requests for all paths not already covered (e.g. /repo/* has its own HEAD handler).
461
        // Global headers are applied before routing, so we just end the response with no body.
462
        proxyRouter.route(HttpMethod.HEAD, "/*").handler(req -> {
463
            req.response().setStatusCode(200).end();
464
        });
465

466
        proxyServer.requestHandler(req -> {
467
            applyGlobalHeaders(req.response());
468
            proxyRouter.handle(req);
469
        });
470
        proxyServer.listen(9393);
471

472
        // Periodic metrics update. Runs on a dedicated single-thread scheduled executor
473
        // (not on the Vert.x event loop) because `updateMetrics` can fall through to a
474
        // synchronous HTTP call in `TripleStore.getRepositoryNames()` when the cache has
475
        // been invalidated. `scheduleWithFixedDelay` naturally serialises ticks and cannot
476
        // pile up if the work occasionally runs long. Same pattern as `JellyNanopubLoader.loadUpdates`
477
        // below.
478
        Executors.newSingleThreadScheduledExecutor()
479
                .scheduleWithFixedDelay(collector::updateMetrics, 1, 1, TimeUnit.SECONDS);
480

481

482
        new Thread(() -> {
483
            try {
484
                var status = StatusController.get().initialize();
485
                log.info("Current state: {}, last committed counter: {}", status.state, status.loadCounter);
486
                // Restore or fetch the registry setup ID
487
                Long storedSetupId = StatusController.get().getRegistrySetupId();
488
                if (storedSetupId != null) {
489
                    JellyNanopubLoader.setLastKnownSetupId(storedSetupId);
490
                    log.info("Restored registry setupId: {}", storedSetupId);
491
                } else if (status.state == StatusController.State.LAUNCHING
492
                        || status.state == StatusController.State.LOADING_INITIAL) {
493
                    // Fresh start or crashed during initial load – safe to adopt the current setupId
494
                    try {
495
                        var metadata = JellyNanopubLoader.fetchRegistryMetadata();
496
                        JellyNanopubLoader.setLastKnownSetupId(metadata.setupId());
497
                        if (metadata.setupId() != null) {
498
                            StatusController.get().setRegistrySetupId(metadata.setupId());
499
                            log.info("Fetched initial registry setupId: {}", metadata.setupId());
500
                        }
501
                    } catch (Exception e) {
502
                        log.warn("Could not fetch initial registry setupId", e);
503
                    }
504
                } else {
505
                    // Upgrade from a version without setupId tracking. The DB has data but
506
                    // we can't verify it matches the current registry state. Leave lastKnownSetupId
507
                    // as null so that loadUpdates() will trigger a resync.
508
                    log.warn("No stored registry setupId but DB has data (state: {}, counter: {}). "
509
                            + "A resync will be triggered on the first update poll.",
510
                            status.state, status.loadCounter);
511
                }
512
                boolean forceResync = "true".equalsIgnoreCase(
513
                        Utils.getEnvString("FORCE_RESYNC", "false"));
514
                if (forceResync && status.state != StatusController.State.LAUNCHING) {
515
                    log.warn("FORCE_RESYNC is set. Forcing full re-load from registry.");
516
                    var metadata = JellyNanopubLoader.fetchRegistryMetadata();
517
                    JellyNanopubLoader.setLastKnownSetupId(metadata.setupId());
518
                    if (metadata.setupId() != null) {
519
                        StatusController.get().setRegistrySetupId(metadata.setupId());
520
                    }
521
                    StatusController.get().setResetting();
522
                    StatusController.get().setLoadingInitial(-1);
523
                    JellyNanopubLoader.loadInitial(-1);
524
                    StatusController.get().setReady();
525
                } else if (status.state == StatusController.State.LAUNCHING || status.state == StatusController.State.LOADING_INITIAL) {
526
                    // Do the initial nanopublication loading
527
                    StatusController.get().setLoadingInitial(status.loadCounter);
528
                    // Fall back to local nanopub loading if the local files are present
529
                    if (!LocalNanopubLoader.init()) {
530
                        JellyNanopubLoader.loadInitial(status.loadCounter);
531
                    } else {
532
                        log.info("Local nanopublication loading finished");
533
                    }
534
                    StatusController.get().setReady();
535
                } else {
536
                    log.info("Initial load is already done");
537
                    StatusController.get().setReady();
538
                }
539
            } catch (Exception ex) {
540
                log.info("Initial load failed, terminating...", ex);
541
                Runtime.getRuntime().exit(1);
542
            }
543

544
            // Seed the TrustStateRegistry from any persisted pointer before the
545
            // periodic poll begins, so the first tick doesn't re-materialize state
546
            // we already have.
547
            TrustStateLoader.bootstrap();
548

549
            // Drop any npass:* graph that isn't the current-pointer target —
550
            // leftovers from builds interrupted by a crash.
551
            if (FeatureFlags.spacesEnabled()) {
552
                AuthorityResolver.get().cleanOrphans();
553
            }
554

555
            // Start periodic nanopub loading
556
            log.info("Starting periodic nanopub loading...");
557
            var executor = Executors.newSingleThreadScheduledExecutor();
558
            executor.scheduleWithFixedDelay(
559
                    JellyNanopubLoader::loadUpdates,
560
                    JellyNanopubLoader.UPDATES_POLL_INTERVAL,
561
                    JellyNanopubLoader.UPDATES_POLL_INTERVAL,
562
                    TimeUnit.MILLISECONDS
563
            );
564

565
            // Periodic authority-resolver tick: detects trust-state flips and
566
            // advances the current space-state graph by an incremental cycle on
567
            // each load-number delta. Same cadence as the nanopub-loading poll.
568
            //
569
            // The same single-threaded executor also runs periodicRebuildTick
570
            // every 10 min; that's the from-scratch rebuild triggered when an
571
            // incremental cycle DELETEs a structural derivation and raises the
572
            // npa:needsFullRebuild flag. Sharing one executor serialises the
573
            // two ticks naturally — they never overlap.
574
            if (FeatureFlags.spacesEnabled()) {
575
                var spacesExecutor = Executors.newSingleThreadScheduledExecutor();
576
                spacesExecutor.scheduleWithFixedDelay(
577
                        () -> {
578
                            try {
579
                                AuthorityResolver.get().tick();
580
                            } catch (Exception ex) {
581
                                log.warn("AuthorityResolver tick failed", ex);
582
                            }
583
                        },
584
                        JellyNanopubLoader.UPDATES_POLL_INTERVAL,
585
                        JellyNanopubLoader.UPDATES_POLL_INTERVAL,
586
                        TimeUnit.MILLISECONDS
587
                );
588
                spacesExecutor.scheduleWithFixedDelay(
589
                        () -> {
590
                            try {
591
                                AuthorityResolver.get().periodicRebuildTick();
592
                            } catch (Exception ex) {
593
                                log.warn("AuthorityResolver periodic rebuild failed", ex);
594
                            }
595
                        },
596
                        10, 10, TimeUnit.MINUTES
597
                );
598
            }
599
        }).start();
600

601
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
602
            try {
603
                log.info("Gracefully shutting down...");
604
                TripleStore.get().shutdownRepositories();
605
                vertx.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
606
                log.info("Graceful shutdown completed");
607
            } catch (Exception ex) {
608
                log.info("Graceful shutdown failed", ex);
609
            }
610
        }));
611
    }
612

613
    private String getResourceAsString(String file) {
614
        InputStream is = getClass().getClassLoader().getResourceAsStream("com/knowledgepixels/query/" + file);
615
        try (Scanner s = new Scanner(is).useDelimiter("\\A")) {
616
            String fileContent = s.hasNext() ? s.next() : "";
617
            return fileContent;
618
        }
619
    }
620

621
    private static void handleRedirect(RoutingContext req, String path) {
622
        String queryString = "";
623
        if (!req.queryParam("query").isEmpty())
624
            queryString = "?query=" + URLEncoder.encode(req.queryParam("query").getFirst(), Charsets.UTF_8);
625
        if (req.queryParam("for-type").size() == 1) {
626
            String type = req.queryParam("for-type").getFirst();
627
            req.response().putHeader("location", path + "/type/" + Utils.createHash(type) + queryString);
628
            req.response().setStatusCode(301).end();
629
        } else if (req.queryParam("for-pubkey").size() == 1) {
630
            String type = req.queryParam("for-pubkey").getFirst();
631
            req.response().putHeader("location", path + "/pubkey/" + Utils.createHash(type) + queryString);
632
            req.response().setStatusCode(301).end();
633
        } else if (req.queryParam("for-user").size() == 1) {
634
            String type = req.queryParam("for-user").getFirst();
635
            req.response().putHeader("location", path + "/user/" + Utils.createHash(type) + queryString);
636
            req.response().setStatusCode(301).end();
637
        }
638
    }
639

640
    /**
641
     * Apply headers to the response that should be present for all requests.
642
     *
643
     * @param response The response to which the headers should be applied.
644
     */
645
    static void applyGlobalHeaders(HttpServerResponse response) {
646
        var state = StatusController.get().getState();
647
        response.putHeader("Nanopub-Query-Status", state.state.toString());
648
        response.putHeader("Nanopub-Query-Registry-Url", JellyNanopubLoader.registryUrl);
649
        Long setupId = StatusController.get().getRegistrySetupId();
650
        response.putHeader("Nanopub-Query-Registry-Setup-Id", setupId == null ? "" : setupId.toString());
651
        response.putHeader("Nanopub-Query-Load-Counter", String.valueOf(state.loadCounter));
652
        // Forward registry metadata headers
653
        String coverageTypes = JellyNanopubLoader.lastCoverageTypes;
654
        response.putHeader("Nanopub-Query-Registry-Coverage-Types", coverageTypes != null ? coverageTypes : "all");
655
        String coverageAgents = JellyNanopubLoader.lastCoverageAgents;
656
        response.putHeader("Nanopub-Query-Registry-Coverage-Agents", coverageAgents != null ? coverageAgents : "viaSetting");
657
        String testInstance = JellyNanopubLoader.lastTestInstance;
658
        if (testInstance != null) {
659
            response.putHeader("Nanopub-Query-Registry-Test-Instance", testInstance);
660
        }
661
        String nanopubCount = JellyNanopubLoader.lastNanopubCount;
662
        if (nanopubCount != null) {
663
            response.putHeader("Nanopub-Query-Registry-Nanopub-Count", nanopubCount);
664
        }
665
    }
666
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc