Skip to content

Commit

Permalink
release: 1.0.0
Browse files Browse the repository at this point in the history
This version has been used in production and has been thoroughly tested. It used to be part of https://github.com/CCBlueX/LiquidBounce, but since I will be rewriting parts of it soon, I decided to release it properly as a library.
  • Loading branch information
1zun4 committed Sep 3, 2024
0 parents commit d8c458e
Show file tree
Hide file tree
Showing 34 changed files with 2,574 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Jetbrains IDE ###
.idea

### Mac OS ###
.DS_Store
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Netty Http Server

Netty HttpServer is a Kotlin-based library for building web REST APIs on top of Netty. It provides a simple way to handle HTTP requests and responses, with built-in support for WebSockets and file serving. This library has been used in production as part of the [LiquidBounce](https://github.com/CCBlueX/LiquidBounce) project and is now available as a standalone library.

## Getting Started

### Installation

To include Netty HttpServer in your project, add the following dependency to your `build.gradle` file:

```gradle
implementation 'com.github.CCBlueX:netty-httpserver:1.0.0'
```

### Basic Usage

Here is an example of how to use the library to create a simple "Hello, World!" REST API:

```kotlin
import com.google.gson.JsonObject
import net.ccbluex.netty.http.HttpServer
import net.ccbluex.netty.http.util.httpOk

fun main() {
val server = HttpServer()

server.routeController.apply {
get("/hello") {
httpOk(JsonObject().apply {
addProperty("message", "Hello, World!")
})
}
}

server.start(8080) // Start the server on port 8080
}
```

In this example, the server listens on port `8080` and responds with a JSON message `"Hello, World!"` when accessing the `/hello` endpoint.

### Examples

You can find additional examples in the `/examples` folder of the repository. These include:

1. **Hello World Example**: A basic server that responds with "Hello, World!".
2. **Echo Server**: A server that echoes back any JSON data sent to it.
3. **File Server**: A server that serves files from a specified directory.

### Running the Examples

To run the examples, you can use Gradle. In the root of the repository, execute the following command:

```bash
./gradlew run -Pexample=<example-name>
```

Replace `<example-name>` with the name of the example you want to run, such as `hello-world`, `echo-server`, or `file-server`.

For instance, to run the Hello World example, use:

```bash
./gradlew run -Pexample=hello-world
```

## License

Netty HttpServer is licensed under the GNU General Public License v3.0. See the [LICENSE](LICENSE) file for more details.

## Contributing

Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.

## Author

Netty HttpServer is developed and maintained by CCBlueX. It was originally part of the LiquidBounce project.

---

Feel free to explore the examples provided and adapt them to your specific needs. Happy coding!
101 changes: 101 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
plugins {
kotlin("jvm") version "2.0.0"
`maven-publish`
}

val projectName = "Netty-HttpServer"
val projectDescription = "A Kotlin library for web REST APIs built on top of Netty."
val licenseName = "GNU General Public License v3.0"
val licenseUrl = "https://www.gnu.org/licenses/gpl-3.0.en.html"
val authorName = "ccbluex"
val projectUrl = "https://github.com/ccbluex/netty-httpserver"

group = "net.ccbluex"
version = "1.0.0"

repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

kotlin {
jvmToolchain(21)
}

dependencies {
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
// https://mvnrepository.com/artifact/io.netty/netty-all
implementation("io.netty:netty-all:4.1.82.Final")
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation("com.google.code.gson:gson:2.10.1")
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
implementation("org.apache.tika:tika-core:2.9.2")

testImplementation(kotlin("test"))
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation("org.mockito:mockito-core:5.13.0")
}

tasks.test {
useJUnitPlatform()
}

tasks.withType<JavaCompile> {
options.release.set(21)
}

tasks.withType<Jar> {
manifest {
attributes(
"Implementation-Title" to projectName,
"Implementation-Version" to version,
"Implementation-Vendor" to authorName,
"License" to licenseName,
"License-Url" to licenseUrl
)
}

// Include LICENSE file in the JAR
from("LICENSE") {
into("META-INF/")
}
}

publishing {
publications {
create<MavenPublication>("pub") {
from(components["java"])

pom {
name.set(projectName)
description.set(projectDescription)
url.set(projectUrl)

licenses {
license {
name.set(licenseName)
url.set(licenseUrl)
}
}

developers {
developer {
id.set("ccbluex")
name.set(authorName)
}
}

scm {
connection.set("scm:git:git://github.com/ccbluex/netty-httpserver.git")
developerConnection.set("scm:git:ssh://github.com:ccbluex/netty-httpserver.git")
url.set(projectUrl)
}
}
}
}
}
21 changes: 21 additions & 0 deletions examples/echo-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.gradle
**/build/
!src/**/build/

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Avoid ignore Gradle wrappper properties
!gradle-wrapper.properties

# Cache of project
.gradletasknamecache

# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
55 changes: 55 additions & 0 deletions examples/echo-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
kotlin("jvm") version "2.0.0"
application
}
group = "net.ccbluex"
version = "1.0.0"

repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

kotlin {
jvmToolchain(21)
}

dependencies {
implementation(project(":"))

// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
// https://mvnrepository.com/artifact/io.netty/netty-all
implementation("io.netty:netty-all:4.1.82.Final")
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation("com.google.code.gson:gson:2.10.1")
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
implementation("org.apache.tika:tika-core:2.9.2")
}

application {
mainClass.set("EchoServerExampleKt") // Specify the main class to run the example
}

tasks.test {
useJUnitPlatform()
}

tasks.withType<JavaCompile> {
options.release.set(21)
}

tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "EchoServerExampleKt"
}

// Include runtime dependencies in the JAR
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
// Prevent duplicate files from being added to the JAR
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
15 changes: 15 additions & 0 deletions examples/echo-server/src/main/kotlin/EchoServerExample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import com.google.gson.JsonObject
import net.ccbluex.netty.http.HttpServer
import net.ccbluex.netty.http.util.httpOk

fun main() {
val server = HttpServer()

server.routeController.apply {
post("/echo") { request ->
httpOk(request.asJson<JsonObject>())
}
}

server.start(8080) // Start the server on port 8080
}
21 changes: 21 additions & 0 deletions examples/file-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.gradle
**/build/
!src/**/build/

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Avoid ignore Gradle wrappper properties
!gradle-wrapper.properties

# Cache of project
.gradletasknamecache

# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
Loading

0 comments on commit d8c458e

Please sign in to comment.