Search in sources :

Example 1 with Router

use of io.micronaut.web.router.Router in project micronaut-core by micronaut-projects.

the class NettyHttpServer method start.

@Override
public synchronized NettyEmbeddedServer start() {
    if (!isRunning()) {
        // suppress unused
        // done here to prevent a blocking service loader in the event loop
        EventLoopGroupConfiguration workerConfig = resolveWorkerConfiguration();
        workerGroup = createWorkerEventLoopGroup(workerConfig);
        parentGroup = createParentEventLoopGroup();
        ServerBootstrap serverBootstrap = createServerBootstrap();
        serverBootstrap.channelFactory(() -> nettyEmbeddedServices.getServerSocketChannelInstance(workerConfig));
        processOptions(serverConfiguration.getOptions(), serverBootstrap::option);
        processOptions(serverConfiguration.getChildOptions(), serverBootstrap::childOption);
        childHandler = new NettyHttpServerInitializer();
        serverBootstrap = serverBootstrap.group(parentGroup, workerGroup).childHandler(childHandler);
        Optional<String> host = serverConfiguration.getHost();
        final String definedHost = host.orElse(null);
        serverPort = bindServerToHost(serverBootstrap, definedHost, serverPort);
        if (isDefault) {
            List<Integer> defaultPorts = new ArrayList<>(2);
            defaultPorts.add(serverPort);
            if (serverConfiguration.isDualProtocol()) {
                defaultPorts.add(bindServerToHost(serverBootstrap, definedHost, getHttpPort(serverConfiguration)));
            }
            final Router router = this.nettyEmbeddedServices.getRouter();
            final Set<Integer> exposedPorts = router.getExposedPorts();
            this.boundPorts.addAll(defaultPorts);
            if (CollectionUtils.isNotEmpty(exposedPorts)) {
                router.applyDefaultPorts(defaultPorts);
                for (Integer exposedPort : exposedPorts) {
                    if (!defaultPorts.contains(exposedPort)) {
                        try {
                            if (definedHost != null) {
                                serverBootstrap.bind(definedHost, exposedPort).sync();
                            } else {
                                serverBootstrap.bind(exposedPort).sync();
                            }
                            this.boundPorts.add(exposedPort);
                        } catch (Throwable e) {
                            final boolean isBindError = e instanceof BindException;
                            if (LOG.isErrorEnabled()) {
                                if (isBindError) {
                                    LOG.error("Unable to start server. Additional specified server port {} already in use.", exposedPort);
                                } else {
                                    LOG.error("Error starting Micronaut server: " + e.getMessage(), e);
                                }
                            }
                            throw new ServerStartupException("Unable to start Micronaut server on port: " + serverPort, e);
                        }
                    }
                }
            }
        }
        fireStartupEvents();
        running.set(true);
    }
    return this;
}
Also used : DefaultEventLoopGroupConfiguration(io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration) EventLoopGroupConfiguration(io.micronaut.http.netty.channel.EventLoopGroupConfiguration) ArrayList(java.util.ArrayList) Router(io.micronaut.web.router.Router) BindException(java.net.BindException) AsciiString(io.netty.util.AsciiString) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException)

Aggregations

DefaultEventLoopGroupConfiguration (io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration)1 EventLoopGroupConfiguration (io.micronaut.http.netty.channel.EventLoopGroupConfiguration)1 ServerStartupException (io.micronaut.http.server.exceptions.ServerStartupException)1 Router (io.micronaut.web.router.Router)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 AsciiString (io.netty.util.AsciiString)1 BindException (java.net.BindException)1 ArrayList (java.util.ArrayList)1