[aimedb] Allow bind to specific address

This commit is contained in:
samnyan
2020-03-30 17:23:00 +09:00
parent 0cf510fb02
commit cd7201d57a
2 changed files with 20 additions and 2 deletions

View File

@@ -8,12 +8,15 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
/**
* @author samnyan (privateamusement@protonmail.com)
@@ -23,13 +26,16 @@ public class AimeDbServer {
private static final Logger logger = LoggerFactory.getLogger(AimeDbServer.class);
private final AimeDbServerInitializer aimeDbServerInitializer;
private final String address;
private final int port;
private final boolean enableAimeDb;
public AimeDbServer(AimeDbServerInitializer aimeDbServerInitializer,
@Value("${aimedb.server.address}") String address,
@Value("${aimedb.server.port}") int port,
@Value("${aimedb.server.enable}") boolean enableAimeDb) {
this.aimeDbServerInitializer = aimeDbServerInitializer;
this.address = address;
this.port = port;
this.enableAimeDb = enableAimeDb;
}
@@ -46,8 +52,19 @@ public class AimeDbServer {
.childHandler(aimeDbServerInitializer)
.option(ChannelOption.SO_BACKLOG, 128);
ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
logger.info("Aime DB start up on port : " + port);
InetSocketAddress socket;
if(StringUtils.isNotBlank(this.address)) {
try {
socket = new InetSocketAddress(InetAddress.getByName(this.address), this.port);
} catch (UnknownHostException e) {
logger.error("UnknownHostException, please check you have set a correct aimedb.server.address.");
socket = new InetSocketAddress(this.port);
}
} else {
socket = new InetSocketAddress(this.port);
}
ChannelFuture f = bootstrap.bind(socket).sync();
logger.info("Aime DB start up on " + socket.toString());
f.channel().closeFuture();
}
}

View File

@@ -1,5 +1,6 @@
## AimeDb server setting
aimedb.server.enable=true
aimedb.server.address=0.0.0.0
aimedb.server.port=22345
## Server host return to client when boot up.
## Please notice DIVA won't work with localhost or 127.0.0.1