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

import io.netty.bootstrap.ServerBootstrap
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, Runnable {
    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())
            }
        })

    override fun run() {
        logInfo("Loading Www")
        mainBootstrap.bind(7301).channel().closeFuture()
            .addListener { logInfo("Closed Www") }
    }
}