Skip to content

Commit

Permalink
#7 Adding routes overview
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed May 2, 2020
1 parent ac3b23b commit e6ce1f2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.example.layrry.links.core.internal;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -29,19 +28,20 @@
import org.moditect.layrry.platform.PluginLifecycleListener;

import com.example.layrry.links.core.spi.RouterContributor;
import com.example.layrry.links.core.spi.RouterContributor.RouterContributions;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

public class LayrryLinksVerticle extends AbstractVerticle {

private static final Logger LOGGER = LogManager.getLogger(LayrryLinksVerticle.class);

private static Map<String, ModuleLayer> moduleLayers = new ConcurrentHashMap<>();
private static Map<ModuleLayer, Set<String>> routesByLayer = new HashMap<>();
private static Map<ModuleLayer, Set<String>> routesByLayer = new ConcurrentHashMap<>();

private static volatile Router mainRouter;
private static volatile Vertx vertx;
Expand All @@ -53,6 +53,8 @@ public void start() {
mainRouter = Router.router(vertx);
mainRouter.route().handler(BodyHandler.create());

registerContributedRoutes(LayrryLinksVerticle.class.getModule().getLayer());

for(Entry<String, ModuleLayer> layer : moduleLayers.entrySet()) {
registerContributedRoutes(layer.getValue());
}
Expand All @@ -67,16 +69,14 @@ private static void registerContributedRoutes(ModuleLayer layer) {
Set<String> routes = new HashSet<>();

contributors.forEach(contributor -> {
contributor.install(vertx, new RouterContributions() {

@Override
public void add(String path, Router router) {
LOGGER.info("Added router for path: " + path);
if (contributor.getClass().getModule().getLayer() == layer) {
contributor.install(vertx, (path, router) -> {
LOGGER.info("Adding router for path: " + path);

mainRouter.mountSubRouter(path, router);
routes.add(path);
}
});
});
}
});

routesByLayer.put(layer, routes);
Expand All @@ -90,6 +90,8 @@ private static void unregisterContributedRoutes(ModuleLayer layer) {
route.remove();
LOGGER.info("Removed router for path: " + route.getPath());
});

routesByLayer.remove(layer);
}

private static boolean startsWithAny(String path, Set<String> paths) {
Expand All @@ -110,15 +112,45 @@ public void pluginAdded(PluginDescriptor plugin) {

@Override
public void pluginRemoved(PluginDescriptor plugin) {
LOGGER.info("Removing plug-in: " + plugin);

unregisterContributedRoutes(plugin.getModuleLayer());
moduleLayers.remove(plugin.getName());
routesByLayer.remove(plugin.getModuleLayer());
LOGGER.info("Removed plug-in: " + plugin);
}
}

public static class RoutesOverviewRouterContributor implements RouterContributor {

@Override
public void install(Vertx vertx, RouterContributions contributions) {
Router router = Router.router(vertx);

router.get("/").handler(this::handleGetRoutesOverview);

contributions.add("/routes", router);
}

public static Map<String, ModuleLayer> getModuleLayers() {
return moduleLayers;
private void handleGetRoutesOverview(RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();

StringBuilder overview = new StringBuilder()
.append("<!DOCTYPE html>")
.append("<html>")
.append(" <head>")
.append(" <title>Layrry Links -- Routes</title>")
.append(" </head>")
.append(" <body>")
.append(" <h1>Layrry Links -- Routes</h1>");

for (Entry<ModuleLayer, Set<String>> layerAndRoutes : routesByLayer.entrySet()) {
for (String route : layerAndRoutes.getValue()) {
overview.append(" <p><a href=\"" + route + "\">" + route + "</a></p>");
}
}

overview.append(" </body>")
.append("</html>");

response.putHeader("content-type", "text/html").end(overview.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;

/**
* Service interface to be implemented by plug-ins of the Layrry Links
* application for contributing routes to the web application.
*/
public interface RouterContributor {

/**
* Invoked when adding a plug-in with a contributor implementation.
*
* @param vertx The Vertx instance
* @param contributions Callback for registering one or more routes
*/
void install(Vertx vertx, RouterContributions contributions);

interface RouterContributions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import org.moditect.layrry.platform.PluginLifecycleListener;

import com.example.layrry.links.core.internal.LayrryLinksVerticle.RoutesOverviewRouterContributor;
import com.example.layrry.links.core.internal.LayrryLinksVerticle.RoutesPluginLifecycleListener;
import com.example.layrry.links.core.spi.RouterContributor;

module com.example.layrry.links.core {
requires org.moditect.layrry.platform;
Expand All @@ -27,6 +29,7 @@
exports com.example.layrry.links.core;
exports com.example.layrry.links.core.spi;

uses com.example.layrry.links.core.spi.RouterContributor;
uses RouterContributor;
provides PluginLifecycleListener with RoutesPluginLifecycleListener;
provides RouterContributor with RoutesOverviewRouterContributor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

public class LayrryLinksTest {


private String layersConfig;
private Path pluginDir;
private Path preparedPluginDir;
Expand Down

0 comments on commit e6ce1f2

Please sign in to comment.