Search in sources :

Example 1 with RouterImpl

use of io.vertx.ext.web.impl.RouterImpl 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;
}
Also used : BBRouter(com.bob.vertx.swagger.BBRouter) HttpServerOptions(io.vertx.core.http.HttpServerOptions) RouterImpl(io.vertx.ext.web.impl.RouterImpl) HttpServer(io.vertx.core.http.HttpServer) Route(io.vertx.ext.web.Route) BeansException(org.springframework.beans.BeansException) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Router(io.vertx.ext.web.Router) BBRouter(com.bob.vertx.swagger.BBRouter) Method(java.lang.reflect.Method) HttpMethod(io.vertx.core.http.HttpMethod) Vertx(io.vertx.core.Vertx) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Verticle(io.vertx.core.Verticle) DeploymentOptions(io.vertx.core.DeploymentOptions) JksOptions(io.vertx.core.net.JksOptions) Environment(org.springframework.core.env.Environment) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Map(java.util.Map)

Aggregations

BBRouter (com.bob.vertx.swagger.BBRouter)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Verticle (io.vertx.core.Verticle)1 Vertx (io.vertx.core.Vertx)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 JksOptions (io.vertx.core.net.JksOptions)1 Route (io.vertx.ext.web.Route)1 Router (io.vertx.ext.web.Router)1 RouterImpl (io.vertx.ext.web.impl.RouterImpl)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 BeansException (org.springframework.beans.BeansException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Environment (org.springframework.core.env.Environment)1