Skip to content

Commit

Permalink
feat(#3): http restructure, routing & parsing work
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Oct 30, 2024
1 parent f8db32c commit 02da077
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 45 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.dokka.gradle.DokkaPlugin

plugins {
kotlin("jvm") version "2.0.0"
id("org.jetbrains.dokka") version "1.9.20"
Expand All @@ -11,6 +9,7 @@ version = "0.0.1"

allprojects {
repositories {
mavenLocal()
mavenCentral()
}

Expand Down
1 change: 1 addition & 0 deletions kraftx/http/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation(libs.slf4j.api)

implementation("io.netty:netty-all:4.1.114.Final") // todo: add to version catalog
implementation("com.oxyggen.net:urilib:1.0.11") // todo: add to version catalog

testImplementation(kotlin("test"))
testImplementation(libs.kotlinx.coroutines.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ enum class HttpMethod(val protocolValue: String) {
PATCH("PATCH"),
DELETE("DELETE"),
OPTIONS("OPTIONS"),
HEAD("HEAD"),
HEAD("HEAD");

companion object {
fun getFromProtocolValue(protocolValue: String) =
entries.first { it.protocolValue == protocolValue }
}
}
45 changes: 45 additions & 0 deletions kraftx/http/src/main/kotlin/sh/illumi/kraft/x/http/HttpServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package sh.illumi.kraft.x.http

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelInitializer
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.HttpRequestDecoder
import io.netty.handler.codec.http.HttpResponseEncoder
import io.netty.handler.logging.LogLevel
import io.netty.handler.logging.LoggingHandler
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import sh.illumi.kraft.layer.RootLayer
import sh.illumi.kraft.x.http.routing.Route

/**
* A service that provides an HTTP server. It must be run on the [RootLayer] of
* the application.
*/
class HttpServer(
private val layer: RootLayer<*>,
) {
private lateinit var serverJob: Job
val rootRoute = Route()

private val eventLoopGroup = NioEventLoopGroup()
private val workerGroup = NioEventLoopGroup()
private val nettyServerBootstrap = ServerBootstrap()
.group(eventLoopGroup, workerGroup)
.handler(LoggingHandler(LogLevel.INFO))
.childHandler(object : ChannelInitializer<SocketChannel>() {
override fun initChannel(ch: SocketChannel) {
val p = ch.pipeline();
p.addLast(HttpRequestDecoder());
p.addLast(HttpResponseEncoder());
// p.addLast(new CustomHttpServerHandler());
}
});

fun startServer() {
serverJob = layer.coroutineScope.launch {

}
}
}
25 changes: 0 additions & 25 deletions kraftx/http/src/main/kotlin/sh/illumi/kraft/x/http/HttpService.kt

This file was deleted.

35 changes: 18 additions & 17 deletions kraftx/http/src/main/kotlin/sh/illumi/kraft/x/http/routing/Route.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package sh.illumi.kraft.x.http.routing

import com.oxyggen.io.Path
import sh.illumi.kraft.x.http.HttpMethod

open class Route(
val path: String = "/",
val path: Path = Path.parse("/"),
val childRoutes: MutableList<Route> = mutableListOf(),
val handlers: MutableMap<HttpMethod, suspend () -> Unit> = mutableMapOf()
) {
fun route(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun route(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
for (httpMethod in HttpMethod.entries) route.handlers[httpMethod] = handler
childRoutes += route
}
Expand Down Expand Up @@ -41,44 +42,44 @@ open class Route(
handlers[HttpMethod.HEAD] = handler
}

fun get(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun get(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.GET] = handler
childRoutes += route
}

fun post(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun post(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.POST] = handler
childRoutes += route
}

fun put(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun put(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.PUT] = handler
childRoutes += route
}

fun patch(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun patch(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.PATCH] = handler
childRoutes += route
}

fun delete(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun delete(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.DELETE] = handler
childRoutes += route
}

fun options(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun options(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.OPTIONS] = handler
childRoutes += route
}

fun head(path: String, handler: suspend () -> Unit) {
val route = Route(path, mutableListOf())
fun head(routePath: String, handler: suspend () -> Unit) {
val route = Route(path.resolve(routePath), mutableListOf())
route.handlers[HttpMethod.HEAD] = handler
childRoutes += route
}
Expand Down

0 comments on commit 02da077

Please sign in to comment.