This repository was archived by the owner on Mar 3, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetty.java
More file actions
90 lines (79 loc) · 2.9 KB
/
Netty.java
File metadata and controls
90 lines (79 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package io.jooby.netty;
import io.jooby.WebServer;
import io.jooby.Router;
import io.jooby.internal.netty.NettyConfigurer;
import io.jooby.internal.netty.NettyHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import org.jooby.funzy.Throwing;
public class Netty implements WebServer {
public static class Pipeline extends ChannelInitializer<SocketChannel> {
private final SslContext sslCtx;
private final NettyHandler handler;
public Pipeline(SslContext sslCtx, DefaultEventExecutorGroup executor, Router router) {
this.sslCtx = sslCtx;
this.handler = new NettyHandler(executor, router);
}
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new HttpServerCodec());
p.addLast(new HttpServerExpectContinueHandler());
p.addLast(handler);
}
}
private static boolean SSL = false;
private EventLoopGroup accept;
private EventLoopGroup worker;
public void start(int port, Router router, boolean join) {
// Configure SSL.
final SslContext sslCtx;
NettyConfigurer provider = NettyConfigurer.get();
// Configure the server.
this.accept = provider.group(1);
this.worker = provider.group(0);
try {
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
DefaultEventExecutorGroup executor = new DefaultEventExecutorGroup(32);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.group(accept, worker)
.channel(provider.channel())
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new Pipeline(sslCtx, executor, router));
Channel ch = bootstrap.bind(port).sync().channel();
ChannelFuture future = ch.closeFuture();
if (join) {
future.sync();
}
} catch (Throwable x) {
throw Throwing.sneakyThrow(x);
}
}
@Override public void stop() {
worker.shutdownGracefully();
accept.shutdownGracefully();
}
}