mirror of
https://gitlab.com/mangadex-pub/mangadex_at_home.git
synced 2024-01-19 02:48:37 +00:00
77 lines
3.3 KiB
Kotlin
77 lines
3.3 KiB
Kotlin
/*
|
|
Mangadex@Home
|
|
Copyright (c) 2020, MangaDex Network
|
|
This file is part of MangaDex@Home.
|
|
|
|
MangaDex@Home is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
MangaDex@Home is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this MangaDex@Home. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package mdnet.base.netty
|
|
|
|
import io.netty.bootstrap.ServerBootstrap
|
|
import io.netty.channel.ChannelFactory
|
|
import io.netty.channel.ChannelFuture
|
|
import io.netty.channel.ChannelInitializer
|
|
import io.netty.channel.ChannelOption
|
|
import io.netty.channel.ServerChannel
|
|
import io.netty.channel.nio.NioEventLoopGroup
|
|
import io.netty.channel.socket.SocketChannel
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel
|
|
import io.netty.handler.codec.http.HttpObjectAggregator
|
|
import io.netty.handler.codec.http.HttpServerCodec
|
|
import io.netty.handler.codec.http.HttpServerKeepAliveHandler
|
|
import io.netty.handler.stream.ChunkedWriteHandler
|
|
import java.net.InetSocketAddress
|
|
import org.http4k.core.HttpHandler
|
|
import org.http4k.server.Http4kChannelHandler
|
|
import org.http4k.server.Http4kServer
|
|
import org.http4k.server.ServerConfig
|
|
|
|
class WebUiNetty(private val hostname: String, private val port: Int) : ServerConfig {
|
|
override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer {
|
|
private val masterGroup = NioEventLoopGroup()
|
|
private val workerGroup = NioEventLoopGroup()
|
|
private lateinit var closeFuture: ChannelFuture
|
|
private lateinit var address: InetSocketAddress
|
|
|
|
override fun start(): Http4kServer = apply {
|
|
val bootstrap = ServerBootstrap()
|
|
bootstrap.group(masterGroup, workerGroup)
|
|
.channelFactory(ChannelFactory<ServerChannel> { NioServerSocketChannel() })
|
|
.childHandler(object : ChannelInitializer<SocketChannel>() {
|
|
public override fun initChannel(ch: SocketChannel) {
|
|
ch.pipeline().addLast("codec", HttpServerCodec())
|
|
ch.pipeline().addLast("keepAlive", HttpServerKeepAliveHandler())
|
|
ch.pipeline().addLast("aggregator", HttpObjectAggregator(Int.MAX_VALUE))
|
|
ch.pipeline().addLast("streamer", ChunkedWriteHandler())
|
|
ch.pipeline().addLast("handler", Http4kChannelHandler(httpHandler))
|
|
}
|
|
})
|
|
.option(ChannelOption.SO_BACKLOG, 1000)
|
|
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
|
val channel = bootstrap.bind(InetSocketAddress(hostname, port)).sync().channel()
|
|
address = channel.localAddress() as InetSocketAddress
|
|
closeFuture = channel.closeFuture()
|
|
}
|
|
|
|
override fun stop() = apply {
|
|
closeFuture.cancel(false)
|
|
workerGroup.shutdownGracefully()
|
|
masterGroup.shutdownGracefully()
|
|
}
|
|
|
|
override fun port(): Int = address.port
|
|
}
|
|
}
|