Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / www / WwwBoot.kt
@Taehui Taehui on 27 Jun 1 KB v1.0-SNAPSHOT
package net.taehui.twilight.www

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.channel.EventLoopGroup
import io.netty.channel.socket.ServerSocketChannel
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.HttpClientCodec
import io.netty.handler.codec.http.HttpObjectAggregator
import io.netty.handler.codec.http.HttpServerCodec
import net.taehui.twilight.Logger

class WwwBoot(eventLoopGroup: EventLoopGroup, eventChannel: Class<out ServerSocketChannel>) : Logger, AutoCloseable {
    private val mainBootstrap: ServerBootstrap = ServerBootstrap().group(eventLoopGroup).channel(eventChannel)
        .childHandler(object : ChannelInitializer<SocketChannel>() {
            public override fun initChannel(ch: SocketChannel) {
                ch.pipeline().addLast(HttpServerCodec()).addLast(HttpObjectAggregator(Int.MAX_VALUE))
                    .addLast(HttpClientCodec()).addLast(WwwAvatar())
            }
        })
    private val mainChannel: Channel

    init {
        logInfo("Loading Www")
        mainChannel = mainBootstrap.bind(7301).channel().closeFuture().addListener {
                logInfo("Closed Www")
            }.channel()
    }

    override fun close() {
        mainChannel.close()
    }
}