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

knowledgepixels / nanopub-registry / 21170335183

20 Jan 2026 11:47AM UTC coverage: 18.999% (+0.2%) from 18.824%
21170335183

push

github

ashleycaselli
refactor(MainVerticle): streamline route handlers and improve logging

96 of 586 branches covered (16.38%)

Branch coverage included in aggregate %.

352 of 1772 relevant lines covered (19.86%)

3.63 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.nanopub.extra.server.PublishNanopub;
21
import org.slf4j.Logger;
22
import org.slf4j.LoggerFactory;
23

24
import java.util.concurrent.TimeUnit;
25

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

29
public class MainVerticle extends AbstractVerticle {
×
30

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

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

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

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

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

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

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

83
                                // Load to nanopub store:
84
                                boolean success = RegistryDB.loadNanopub(s, np);
×
85
                                if (!success)
×
86
                                    throw new RuntimeException("Nanopublication not supported: " + np.getUri());
×
87
                                // Load to lists, if applicable:
88
                                NanopubLoader.simpleLoad(s, np);
×
89

90
                                if (!isSet(s, Collection.SERVER_INFO.toString(), "testInstance")) {
×
91
                                    // Here we publish it also to the first-generation services, so they know about it too:
92
                                    // TODO Remove this at some point
93
                                    try {
94
                                        new PublishNanopub().publishNanopub(np, "https://np.knowledgepixels.com/");
×
95
                                    } catch (Exception ex) {
×
96
                                        ex.printStackTrace();
×
97
                                    }
×
98
                                }
99
                            }
100
                        }
101
                    }
102
                    c.response().setStatusCode(201);
×
103
                } catch (Exception ex) {
×
104
                    c.response().setStatusCode(400).setStatusMessage("Error processing nanopub: " + ex.getMessage());
×
105
                } finally {
106
                    c.response().end();
×
107
                }
108
            });
×
109
        };
×
110
        router.route(HttpMethod.POST, "/").handler(postHandler);
×
111
        router.route(HttpMethod.POST, "/np/").handler(postHandler);
×
112

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

118
            new Thread(Task::runTasks).start();
×
119

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

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

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

138
    }
×
139

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