use of io.vertx.ext.web.Route in project vertx-swagger by bobxwang.
the class SwaggerApp method Run.
public static AnnotationConfigApplicationContext Run(Class<?> clasz) {
final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(clasz);
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(ApplicationContextHolder.class);
applicationContext.registerBeanDefinition("applicationContextHolder", bdb.getBeanDefinition());
// Just For Init
applicationContext.getBean(ApplicationContextHolder.class);
final Environment environment = applicationContext.getBean(Environment.class);
final Vertx vertx = applicationContext.getBean(Vertx.class);
final SpringVerticleFactory verticleFactory = new SpringVerticleFactory();
vertx.registerVerticleFactory(verticleFactory);
try {
applicationContext.getBean(Router.class);
} catch (BeansException be) {
if (be instanceof NoSuchBeanDefinitionException) {
Router rr = new RouterImpl(vertx);
applicationContext.getBeanFactory().registerSingleton("router", rr);
}
}
final Router router = applicationContext.getBean(Router.class);
initSwagger(environment);
configRouter(vertx, router, environment);
Map<String, Verticle> maps = applicationContext.getBeansOfType(Verticle.class);
DeploymentOptions options = new DeploymentOptions().setInstances(1).setWorker(true);
for (Map.Entry<String, Verticle> temp : maps.entrySet()) {
Verticle verticle = temp.getValue();
String name = verticle.getClass().getSimpleName().substring(0, 1).toLowerCase() + verticle.getClass().getSimpleName().substring(1);
vertx.deployVerticle(verticleFactory.prefix() + ":" + name, options);
for (Method method : verticle.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(Override.class)) {
continue;
}
if (method.getName().contains("lambda")) {
continue;
}
if (io.swagger.util.ReflectionUtils.isOverriddenMethod(method, verticle.getClass())) {
continue;
}
if (method.isAnnotationPresent(BBRouter.class)) {
BBRouter bbRouter = io.swagger.util.ReflectionUtils.getAnnotation(method, BBRouter.class);
Route route = router.route(bbRouter.httpMethod(), bbRouter.path());
route.handler(ctx -> {
ReflectionUtils.makeAccessible(method);
ReflectionUtils.invokeMethod(method, verticle, ctx);
});
}
}
}
HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setSsl(false).setKeyStoreOptions(new JksOptions().setPath("server-keystore.jks").setPassword("secret")));
httpServer.requestHandler(router::accept);
int port;
try {
port = Integer.valueOf(environment.getProperty("server.port", "8080"));
} catch (Exception e) {
throw new RuntimeException("请配置有效端口号");
}
httpServer.listen(port, ar -> {
if (ar.succeeded()) {
logger.info("Server started on port " + ar.result().actualPort());
} else {
logger.error("Cannot start the server: " + ar.cause());
}
});
return applicationContext;
}
use of io.vertx.ext.web.Route in project Summer by yale8848.
the class SummerRouter method registerResource.
public void registerResource(Class clazz) {
if (isRegister(clazz)) {
return;
}
MethodsProcessor.get(classInfos, clazz);
for (ClassInfo classInfo : classInfos) {
for (MethodInfo methodInfo : classInfo.getMethodInfoList()) {
String p = classInfo.getClassPath() + methodInfo.getMethodPath();
p = PathParamConverter.converter(p);
p = addContextPath(p);
Route route = null;
if (methodInfo.getHttpMethod() == null) {
route = router.route(p);
} else if (methodInfo.getHttpMethod() == GET.class) {
route = router.get(p);
} else if (methodInfo.getHttpMethod() == POST.class) {
route = router.post(p);
} else if (methodInfo.getHttpMethod() == PUT.class) {
route = router.put(p);
} else if (methodInfo.getHttpMethod() == DELETE.class) {
route = router.delete(p);
} else if (methodInfo.getHttpMethod() == OPTIONS.class) {
route = router.options(p);
} else if (methodInfo.getHttpMethod() == HEAD.class) {
route = router.head(p);
}
if (methodInfo.isBlocking()) {
route.blockingHandler(getHandler(classInfo, methodInfo));
} else {
route.handler(getHandler(classInfo, methodInfo));
}
}
}
}
use of io.vertx.ext.web.Route in project vertx-web by vert-x3.
the class TemplateTest method testRelativeToRoutePath.
private void testRelativeToRoutePath(String pathPrefix) throws Exception {
TemplateEngine engine = new TestEngine(false);
router.route().handler(context -> {
context.put("foo", "badger");
context.put("bar", "fox");
context.next();
});
Route route = router.route();
if (pathPrefix != null) {
route.path(pathPrefix + "*");
}
route.handler(TemplateHandler.create(engine, "somedir", "text/html"));
String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>";
testRequest(HttpMethod.GET, pathPrefix != null ? pathPrefix + "/test-template.html" : "/test-template.html", 200, "OK", expected);
}
use of io.vertx.ext.web.Route in project vertx-web by vert-x3.
the class TemplateHandlerImplTest method testTurnOffIndex.
@Test
public void testTurnOffIndex() {
TemplateEngine templateEngine = mock(TemplateEngine.class);
RoutingContext routingContext = mock(RoutingContext.class);
when(routingContext.normalisedPath()).thenReturn("/");
Route currentRoute = mock(Route.class);
when(currentRoute.getPath()).thenReturn("/");
when(routingContext.currentRoute()).thenReturn(currentRoute);
TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
templateHandler.setIndexTemplate(null);
templateHandler.handle(routingContext);
verify(templateEngine).render(any(), eq("templates"), eq("/"), any());
}
use of io.vertx.ext.web.Route in project vertx-web by vert-x3.
the class TemplateHandlerImplTest method testSimpleTemplate.
@Test
public void testSimpleTemplate() {
TemplateEngine templateEngine = mock(TemplateEngine.class);
RoutingContext routingContext = mock(RoutingContext.class);
when(routingContext.normalisedPath()).thenReturn("/about");
Route currentRoute = mock(Route.class);
when(currentRoute.getPath()).thenReturn("/");
when(routingContext.currentRoute()).thenReturn(currentRoute);
TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
templateHandler.handle(routingContext);
verify(templateEngine).render(any(), eq("templates"), eq("/about"), any());
}
Aggregations