did/did-server/src/main/java/io/github/ehlxr/did/server/http/HttpServer.java

98 lines
2.9 KiB
Java
Raw Normal View History

2021-01-22 07:40:02 +00:00
package io.github.ehlxr.did.server.http;
2018-08-14 07:21:56 +00:00
2021-01-22 07:40:02 +00:00
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.core.SnowFlake;
import io.github.ehlxr.did.server.BaseServer;
2018-08-14 07:21:56 +00:00
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
2021-01-19 10:50:50 +00:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-08-14 07:21:56 +00:00
2021-01-27 03:47:09 +00:00
import java.util.Objects;
2018-08-14 07:21:56 +00:00
/**
2021-01-20 07:25:58 +00:00
* Http 使 Netty Http
2018-08-14 07:21:56 +00:00
*
* @author ehlxr
*/
public class HttpServer extends BaseServer {
2021-01-19 10:50:50 +00:00
protected Logger logger = LoggerFactory.getLogger(HttpServer.class);
2021-01-27 03:47:09 +00:00
private int port = "".equals(Constants.getEnv("HTTP_PORT")) ? Constants.HTTP_PORT : Integer.parseInt(Constants.getEnv("HTTP_PORT"));
2021-01-19 10:50:50 +00:00
2021-01-20 07:25:58 +00:00
public HttpServer(SnowFlake snowFlake, int port) {
2021-01-27 03:47:09 +00:00
Objects.requireNonNull(snowFlake, "snowflake instance not allow null");
2021-01-20 07:25:58 +00:00
this.snowFlake = snowFlake;
2021-01-27 03:47:09 +00:00
this.port = port == 0 ? this.port : port;
2021-01-20 07:25:58 +00:00
}
2018-08-14 07:21:56 +00:00
public HttpServer(SnowFlake snowFlake) {
2021-01-27 03:47:09 +00:00
this(snowFlake, 0);
2018-08-14 07:21:56 +00:00
}
2021-01-27 03:47:09 +00:00
public static HttpServerBuilder newBuilder() {
return new HttpServerBuilder();
}
2018-08-14 07:21:56 +00:00
@Override
2021-01-27 03:47:09 +00:00
public void shutdown() {
logger.info("HttpServer shutdowning......");
super.shutdown();
logger.info("HttpServer shutdown finish!");
2018-08-14 07:21:56 +00:00
}
@Override
public void start() {
try {
2021-01-27 03:47:09 +00:00
init();
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(defLoopGroup,
new HttpRequestDecoder(),
new HttpObjectAggregator(65536),
new HttpResponseEncoder(),
new HttpServerHandler(snowFlake)
);
}
});
2021-01-19 10:39:42 +00:00
channelFuture = serverBootstrap.bind(port).sync();
logger.info("HttpServer start success, port is:{}", port);
2018-08-14 07:21:56 +00:00
} catch (InterruptedException e) {
logger.error("HttpServer start fail,", e);
}
}
2021-01-27 03:47:09 +00:00
public static final class HttpServerBuilder {
protected SnowFlake snowFlake;
private int port;
private HttpServerBuilder() {
}
public static HttpServerBuilder aHttpServer() {
return new HttpServerBuilder();
}
public HttpServerBuilder snowFlake(SnowFlake snowFlake) {
this.snowFlake = snowFlake;
return this;
}
public HttpServerBuilder port(int port) {
this.port = port;
return this;
}
public HttpServer build() {
return new HttpServer(snowFlake, port);
}
2021-01-19 12:28:04 +00:00
}
2018-08-14 07:21:56 +00:00
}