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

knowledgepixels / nanopub-registry / 23243177209

18 Mar 2026 11:47AM UTC coverage: 30.21% (+4.3%) from 25.927%
23243177209

push

github

web-flow
Merge pull request #83 from knowledgepixels/feature/get-path-nanodash-forward

Add /get/ path that forwards to Nanodash for HTML requests

185 of 680 branches covered (27.21%)

Branch coverage included in aggregate %.

621 of 1988 relevant lines covered (31.24%)

5.27 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, "/get/").handler(c -> c.response().putHeader("Location", "/").setStatusCode(307).end());
×
49
        router.route(HttpMethod.GET, "/get/*").handler(c -> NanopubPage.show(c, true));
×
50
        router.route(HttpMethod.GET, "/debug/*").handler(c -> DebugPage.show(c));
×
51
        router.route(HttpMethod.GET, "/style.css").handler(c -> ResourcePage.show(c, "style.css", "text/css"));
×
52

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

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

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

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

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

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

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

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

120
            new Thread(Task::runTasks).start();
×
121

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

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

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

140
    }
×
141

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