Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Nov 3, 2023
1 parent b058d9b commit 60700bf
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 85 deletions.
13 changes: 0 additions & 13 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,6 @@ public abstract interface class com/hexagonkt/core/logging/LoggingPort {
public abstract fun setLoggerLevel (Ljava/lang/String;Lcom/hexagonkt/core/logging/LoggingLevel;)V
}

public final class com/hexagonkt/core/logging/SystemLogger : com/hexagonkt/core/logging/LoggerPort {
public fun <init> (Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Lcom/hexagonkt/core/logging/SystemLogger;
public static synthetic fun copy$default (Lcom/hexagonkt/core/logging/SystemLogger;Ljava/lang/String;ILjava/lang/Object;)Lcom/hexagonkt/core/logging/SystemLogger;
public fun equals (Ljava/lang/Object;)Z
public final fun getName ()Ljava/lang/String;
public fun hashCode ()I
public fun log (Lcom/hexagonkt/core/logging/LoggingLevel;Ljava/lang/Throwable;Lkotlin/jvm/functions/Function1;)V
public fun log (Lcom/hexagonkt/core/logging/LoggingLevel;Lkotlin/jvm/functions/Function0;)V
public fun toString ()Ljava/lang/String;
}

public final class com/hexagonkt/core/logging/SystemLoggingAdapter : com/hexagonkt/core/logging/LoggingPort {
public fun <init> ()V
public fun <init> (Lcom/hexagonkt/core/logging/LoggingLevel;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.hexagonkt.core.logging

import com.hexagonkt.core.logging.LoggingLevel.*

data class SystemLogger(val name: String) : LoggerPort {
internal data class SystemLogger(val name: String) : LoggerPort {

private val logger: System.Logger = System.getLogger(name)

Expand Down
10 changes: 0 additions & 10 deletions http/http_server_netty/api/http_server_netty.api
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,3 @@ public class com/hexagonkt/http/server/netty/NettyServerAdapter : com/hexagonkt/
public fun supportedProtocols ()Ljava/util/Set;
}

public final class com/hexagonkt/http/server/netty/NettyServerAdapter$HttpChannelInitializer : io/netty/channel/ChannelInitializer {
public fun <init> (Ljava/util/Map;Lio/netty/util/concurrent/EventExecutorGroup;Lcom/hexagonkt/http/server/HttpServerSettings;)V
public synthetic fun initChannel (Lio/netty/channel/Channel;)V
}

public final class com/hexagonkt/http/server/netty/NettyServerAdapter$HttpsChannelInitializer : io/netty/channel/ChannelInitializer {
public fun <init> (Ljava/util/Map;Lio/netty/handler/ssl/SslContext;Lcom/hexagonkt/http/SslSettings;Lio/netty/util/concurrent/EventExecutorGroup;Lcom/hexagonkt/http/server/HttpServerSettings;)V
public synthetic fun initChannel (Lio/netty/channel/Channel;)V
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hexagonkt.http.server.netty

import com.hexagonkt.http.handlers.HttpHandler
import com.hexagonkt.http.server.HttpServerSettings
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.*
import io.netty.handler.stream.ChunkedWriteHandler
import io.netty.util.concurrent.EventExecutorGroup

internal class HttpChannelInitializer(
private val handlers: Map<HttpMethod, HttpHandler>,
private val executorGroup: EventExecutorGroup?,
private val settings: HttpServerSettings,
) : ChannelInitializer<SocketChannel>() {

override fun initChannel(channel: SocketChannel) {
val pipeline = channel.pipeline()

pipeline.addLast(HttpServerCodec())
// pipeline.addLast(HttpServerKeepAliveHandler())
pipeline.addLast(HttpObjectAggregator(Int.MAX_VALUE))
// pipeline.addLast(ChunkedWriteHandler())

if (settings.zip)
pipeline.addLast(HttpContentCompressor())

val nettyServerHandler = NettyServerHandler(handlers, null)
if (executorGroup == null)
pipeline.addLast(nettyServerHandler)
else
pipeline.addLast(executorGroup, nettyServerHandler)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hexagonkt.http.server.netty

import com.hexagonkt.http.SslSettings
import com.hexagonkt.http.handlers.HttpHandler
import com.hexagonkt.http.server.HttpServerSettings
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.*
import io.netty.handler.ssl.SslContext
import io.netty.util.concurrent.EventExecutorGroup

internal class HttpsChannelInitializer(
private val handlers: Map<HttpMethod, HttpHandler>,
private val sslContext: SslContext,
private val sslSettings: SslSettings,
private val executorGroup: EventExecutorGroup?,
private val settings: HttpServerSettings,
) : ChannelInitializer<SocketChannel>() {

override fun initChannel(channel: SocketChannel) {
val pipeline = channel.pipeline()
val sslHandler = sslContext.newHandler(channel.alloc())
val handlerSsl = if (sslSettings.clientAuth) sslHandler else null

pipeline.addLast(sslHandler)
pipeline.addLast(HttpServerCodec())
// pipeline.addLast(HttpServerKeepAliveHandler())
pipeline.addLast(HttpObjectAggregator(Int.MAX_VALUE))
// pipeline.addLast(ChunkedWriteHandler())

if (settings.zip)
pipeline.addLast(HttpContentCompressor())

val serverHandler = NettyServerHandler(handlers, handlerSsl)
if (executorGroup == null)
pipeline.addLast(serverHandler)
else
pipeline.addLast(executorGroup, serverHandler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ import com.hexagonkt.http.handlers.HttpHandler
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.*
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.*
import io.netty.handler.ssl.ClientAuth.OPTIONAL
import io.netty.handler.ssl.ClientAuth.REQUIRE
import io.netty.handler.ssl.SslContext
import io.netty.handler.ssl.SslContextBuilder
import io.netty.handler.stream.ChunkedWriteHandler
import io.netty.util.concurrent.DefaultEventExecutorGroup
import io.netty.util.concurrent.EventExecutorGroup
import java.net.InetSocketAddress
import java.util.concurrent.TimeUnit.SECONDS
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.TrustManagerFactory
import kotlin.Int.Companion.MAX_VALUE

/**
* Implements [HttpServerPort] using Netty [Channel].
Expand Down Expand Up @@ -196,57 +192,4 @@ open class NettyServerAdapter(
NettyServerAdapter::shutdownQuietSeconds to shutdownQuietSeconds,
NettyServerAdapter::shutdownTimeoutSeconds to shutdownTimeoutSeconds,
)

class HttpChannelInitializer(
private val handlers: Map<HttpMethod, HttpHandler>,
private val executorGroup: EventExecutorGroup?,
private val settings: HttpServerSettings,
) : ChannelInitializer<SocketChannel>() {

override fun initChannel(channel: SocketChannel) {
val pipeline = channel.pipeline()

pipeline.addLast(HttpServerCodec())
pipeline.addLast(HttpServerKeepAliveHandler())
pipeline.addLast(HttpObjectAggregator(MAX_VALUE))
pipeline.addLast(ChunkedWriteHandler())

if (settings.zip)
pipeline.addLast(HttpContentCompressor())

if (executorGroup == null)
pipeline.addLast(NettyServerHandler(handlers, null))
else
pipeline.addLast(executorGroup, NettyServerHandler(handlers, null))
}
}

class HttpsChannelInitializer(
private val handlers: Map<HttpMethod, HttpHandler>,
private val sslContext: SslContext,
private val sslSettings: SslSettings,
private val executorGroup: EventExecutorGroup?,
private val settings: HttpServerSettings,
) : ChannelInitializer<SocketChannel>() {

override fun initChannel(channel: SocketChannel) {
val pipeline = channel.pipeline()
val sslHandler = sslContext.newHandler(channel.alloc())
val handlerSsl = if (sslSettings.clientAuth) sslHandler else null

pipeline.addLast(sslHandler)
pipeline.addLast(HttpServerCodec())
pipeline.addLast(HttpServerKeepAliveHandler())
pipeline.addLast(HttpObjectAggregator(MAX_VALUE))
pipeline.addLast(ChunkedWriteHandler())

if (settings.zip)
pipeline.addLast(HttpContentCompressor())

if (executorGroup == null)
pipeline.addLast(NettyServerHandler(handlers, handlerSsl))
else
pipeline.addLast(executorGroup, NettyServerHandler(handlers, handlerSsl))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ internal class NettyServerHandler(
}

private fun readHttpRequest(context: ChannelHandlerContext, nettyRequest: HttpRequest) {
val result = nettyRequest.decoderResult()

if (result.isFailure)
throw IllegalStateException(result.cause())
// val result = nettyRequest.decoderResult()
//
// if (result.isFailure)
// throw IllegalStateException(result.cause())

val channel = context.channel()
val address = channel.remoteAddress() as InetSocketAddress
Expand Down

0 comments on commit 60700bf

Please sign in to comment.