Last active
November 26, 2017 10:43
-
-
Save netfeed/8cebca17864c51b13ac83aaad5842a84 to your computer and use it in GitHub Desktop.
vertx auto routing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package reflection.magic; | |
| import io.vertx.core.Vertx; | |
| import io.vertx.core.VertxOptions; | |
| import io.vertx.core.http.HttpMethod; | |
| import io.vertx.core.http.HttpServer; | |
| import io.vertx.core.http.HttpServerOptions; | |
| import io.vertx.ext.web.Route; | |
| import io.vertx.ext.web.Router; | |
| import org.reflections.Reflections; | |
| import org.reflections.scanners.SubTypesScanner; | |
| import org.reflections.scanners.TypeAnnotationsScanner; | |
| import java.util.Set; | |
| public class Application { | |
| public static void main(final String[] args) { | |
| new Application().run(); | |
| } | |
| /** | |
| * Finds all {@link reflection.magic.Router} and instantiates them with required extra fluff | |
| * | |
| * @return new {@link Router} with all routes setup | |
| */ | |
| private Router createRouter(Vertx vertx) { | |
| Router router = Router.router(vertx); | |
| Reflections reflections = new Reflections(getClass().getPackage().getName(), | |
| new SubTypesScanner(), | |
| new TypeAnnotationsScanner()); | |
| Set<Class<?>> routerAnnotations = reflections.getTypesAnnotatedWith(reflection.magic.Router.class); | |
| for (Class<?> annotatedClass : routerAnnotations) { | |
| try { | |
| reflection.magic.Router annotation = annotatedClass.getAnnotation(reflection.magic.Router.class); | |
| Object routerInstance = annotatedClass.newInstance(); | |
| RouteHandler routeHandler = (RouteHandler) routerInstance; | |
| HttpMethod verb = annotation.httpMethod(); | |
| Route wertxRoute; | |
| if (annotation.isRegex()) { | |
| if (annotation.requiresAuth()) { | |
| router.routeWithRegex(verb, annotation.path()).handler(new AuthHandler()); | |
| } | |
| wertxRoute = router.routeWithRegex(verb, annotation.path()); | |
| } else { | |
| if (annotation.requiresAuth()) { | |
| router.route(verb, annotation.path()).handler(new AuthHandler()); | |
| } | |
| wertxRoute = router.route(verb, annotation.path()); | |
| } | |
| wertxRoute.handler(routeHandler); | |
| System.out.println(String.format("%s %s (isRegex: %b, requiresAuth: %b)", verb, annotation.path(), annotation.isRegex(), annotation.requiresAuth())); | |
| } catch (InstantiationException | IllegalAccessException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| return router; | |
| } | |
| private void run() { | |
| VertxOptions vertxOptions = new VertxOptions(); | |
| Vertx vertx = Vertx.vertx(vertxOptions); | |
| HttpServerOptions options = new HttpServerOptions().setLogActivity(true); | |
| HttpServer server = vertx.createHttpServer(options); | |
| Router router = createRouter(vertx); | |
| server.requestHandler(router::accept).listen(8080, "localhost", res -> { | |
| if (res.succeeded()) { | |
| System.out.println("Server is now listening!"); | |
| } else { | |
| System.out.println("Failed to bind!"); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package reflection.magic; | |
| import io.vertx.ext.web.RoutingContext; | |
| public class AuthHandler extends RouteHandler { | |
| @Override | |
| public void handle(RoutingContext routingContext) { | |
| // do auth stuff here | |
| routingContext.next(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package reflection.magic; | |
| import io.vertx.core.Handler; | |
| import io.vertx.ext.web.RoutingContext; | |
| public abstract class RouteHandler implements Handler<RoutingContext> { | |
| @Override | |
| public abstract void handle(RoutingContext routingContext); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package reflection.magic; | |
| import io.vertx.core.http.HttpMethod; | |
| import java.lang.annotation.ElementType; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| import java.lang.annotation.Target; | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Target(ElementType.TYPE) | |
| public @interface Router { | |
| /** | |
| * @return the given path for this router, can be a regex | |
| */ | |
| String path(); | |
| /** | |
| * @return if true, then this route will be setup with {@link io.vertx.ext.web.Router#routeWithRegex(String)} | |
| */ | |
| boolean isRegex() default false; | |
| /** | |
| * @return if true, register {@link apa.handler.AuthHandler} for route | |
| */ | |
| boolean requiresAuth() default false; | |
| /** | |
| * @return the {@link HttpMethod} verb that this router will work on | |
| */ | |
| HttpMethod httpMethod() default HttpMethod.GET; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment