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

knowledgepixels / nanopub-registry / 23539426481

25 Mar 2026 11:47AM UTC coverage: 30.645% (+0.2%) from 30.443%
23539426481

push

github

web-flow
Merge pull request #88 from knowledgepixels/perf/eliminate-redundant-signature-verification

perf: eliminate redundant signature verification in POST and peer sync

190 of 700 branches covered (27.14%)

Branch coverage included in aggregate %.

646 of 2028 relevant lines covered (31.85%)

5.39 hits per line

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

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

3
import com.mongodb.client.ClientSession;
4
import io.micrometer.prometheus.PrometheusMeterRegistry;
5
import io.vertx.core.AbstractVerticle;
6
import io.vertx.core.Handler;
7
import io.vertx.core.Promise;
8
import io.vertx.core.http.HttpMethod;
9
import io.vertx.core.http.HttpServer;
10
import io.vertx.ext.web.Router;
11
import io.vertx.ext.web.RoutingContext;
12
import io.vertx.micrometer.PrometheusScrapingHandler;
13
import io.vertx.micrometer.backends.BackendRegistries;
14
import net.trustyuri.TrustyUriUtils;
15
import org.eclipse.rdf4j.rio.RDFFormat;
16
import org.eclipse.rdf4j.rio.Rio;
17
import org.nanopub.MalformedNanopubException;
18
import org.nanopub.Nanopub;
19
import org.nanopub.NanopubImpl;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

23
import java.util.concurrent.TimeUnit;
24

25
import static com.knowledgepixels.registry.RegistryDB.has;
26
import static com.knowledgepixels.registry.RegistryDB.isSet;
27

28
public class MainVerticle extends AbstractVerticle {
×
29

30
    private final Logger logger = LoggerFactory.getLogger(MainVerticle.class);
×
31

32
    @Override
33
    public void start(Promise<Void> startPromise) {
34
        HttpServer server = vertx.createHttpServer();
×
35
        Router router = Router.router(vertx);
×
36
        server.requestHandler(router);
×
37
        server.listen(9292);
×
38
        router.route(HttpMethod.GET, "/agent*").handler(c -> {
×
39
            // /agent/... | /agents | /agentAccounts
40
            ListPage.show(c);
×
41
        });
×
42
        router.route(HttpMethod.GET, "/nanopubs*").handler(c -> ListPage.show(c));
×
43
        router.route(HttpMethod.GET, "/list*").handler(c -> ListPage.show(c));
×
44
        router.route(HttpMethod.GET, "/pubkeys*").handler(c -> ListPage.show(c));
×
45
        router.route(HttpMethod.GET, "/np/").handler(c -> c.response().putHeader("Location", "/").setStatusCode(307).end());
×
46
        router.route(HttpMethod.GET, "/np/*").handler(c -> NanopubPage.show(c));
×
47
        router.route(HttpMethod.GET, "/get/").handler(c -> c.response().putHeader("Location", "/").setStatusCode(307).end());
×
48
        router.route(HttpMethod.GET, "/get/*").handler(c -> NanopubPage.show(c, true));
×
49
        router.route(HttpMethod.GET, "/debug/*").handler(c -> DebugPage.show(c));
×
50
        router.route(HttpMethod.GET, "/style.css").handler(c -> ResourcePage.show(c, "style.css", "text/css"));
×
51

52
        // Metrics
53
        final var metricsHttpServer = vertx.createHttpServer();
×
54
        final var metricsRouter = Router.router(vertx);
×
55
        metricsHttpServer.requestHandler(metricsRouter).listen(9293);
×
56

57
        final var metricsRegistry = (PrometheusMeterRegistry) BackendRegistries.getDefaultNow();
×
58
        final var collector = new MetricsCollector(metricsRegistry);
×
59
        metricsRouter.route("/metrics").handler(PrometheusScrapingHandler.create(metricsRegistry));
×
60

61
        router.route(HttpMethod.GET, "/*").handler(c -> MainPage.show(c));
×
62
        router.route(HttpMethod.HEAD, "/*").handler(c -> MainPage.show(c));
×
63

64
        Handler<RoutingContext> postHandler = c -> {
×
65
            c.request().bodyHandler(bh -> {
×
66
                vertx.<Void>executeBlocking(() -> {
×
67
                    String contentType = c.request().getHeader("Content-Type");
×
68
                    Nanopub np = null;
×
69
                    try {
70
                        np = new NanopubImpl(bh.toString(), Rio.getParserFormatForMIMEType(contentType).orElse(RDFFormat.TRIG));
×
71
                    } catch (MalformedNanopubException ex) {
×
72
                        ex.printStackTrace();
×
73
                    }
×
74
                    if (np != null) {
×
75
                        try (ClientSession s = RegistryDB.getClient().startSession()) {
×
76
                            String ac = TrustyUriUtils.getArtifactCode(np.getUri().toString());
×
77
                            if (has(s, Collection.NANOPUBS.toString(), ac)) {
×
78
                                logger.info("POST: known nanopub {}", ac);
×
79
                            } else {
80
                                logger.info("POST: new nanopub {}", ac);
×
81

82
                                // TODO Run checks here whether we want to register this nanopub (considering quotas etc.)
83

84
                                // Verify signature once, pass through to avoid redundant verification:
85
                                String pubkey = RegistryDB.getPubkey(np);
×
86
                                if (pubkey == null)
×
87
                                    throw new RuntimeException("Nanopublication not supported: " + np.getUri());
×
88
                                // Load to nanopub store:
89
                                boolean success = RegistryDB.loadNanopubVerified(s, np, pubkey, null);
×
90
                                if (!success)
×
91
                                    throw new RuntimeException("Nanopublication not supported: " + np.getUri());
×
92
                                // Load to lists, if applicable:
93
                                NanopubLoader.simpleLoad(s, np, pubkey);
×
94
                            }
95
                        }
96
                    }
97
                    return null;
×
98
                }).onComplete(ar -> {
×
99
                    if (ar.succeeded()) {
×
100
                        c.response().setStatusCode(201).end();
×
101
                    } else {
102
                        c.response().setStatusCode(400)
×
103
                                .setStatusMessage("Error processing nanopub: " + ar.cause().getMessage()).end();
×
104
                    }
105
                });
×
106
            });
×
107
        };
×
108
        router.route(HttpMethod.POST, "/").handler(postHandler);
×
109
        router.route(HttpMethod.POST, "/np/").handler(postHandler);
×
110

111
        // INIT
112
        vertx.executeBlocking(() -> {
×
113
            logger.info("Starting DB initialization...");
×
114
            RegistryDB.init();
×
115

116
            new Thread(Task::runTasks).start();
×
117

118
            return null;
×
119
        }).onComplete(res -> logger.info("DB initialization finished"));
×
120

121
        // Periodic metrics update
122
        vertx.setPeriodic(1000, id -> collector.updateMetrics());
×
123

124
        // SHUTDOWN
125
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
×
126
            try {
127
                logger.info("Gracefully shutting down...");
×
128
                RegistryDB.getClient().close();
×
129
                vertx.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS);
×
130
                logger.info("Graceful shutdown completed");
×
131
            } catch (Exception ex) {
×
132
                logger.error("Graceful shutdown failed", ex);
×
133
            }
×
134
        }));
×
135

136
    }
×
137

138
}
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