Skip to content

Commit d8c458e

Browse files
committed
release: 1.0.0
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.
0 parents  commit d8c458e

34 files changed

+2574
-0
lines changed

.gitignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Jetbrains IDE ###
42+
.idea
43+
44+
### Mac OS ###
45+
.DS_Store

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Netty Http Server
2+
3+
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.
4+
5+
## Getting Started
6+
7+
### Installation
8+
9+
To include Netty HttpServer in your project, add the following dependency to your `build.gradle` file:
10+
11+
```gradle
12+
implementation 'com.github.CCBlueX:netty-httpserver:1.0.0'
13+
```
14+
15+
### Basic Usage
16+
17+
Here is an example of how to use the library to create a simple "Hello, World!" REST API:
18+
19+
```kotlin
20+
import com.google.gson.JsonObject
21+
import net.ccbluex.netty.http.HttpServer
22+
import net.ccbluex.netty.http.util.httpOk
23+
24+
fun main() {
25+
val server = HttpServer()
26+
27+
server.routeController.apply {
28+
get("/hello") {
29+
httpOk(JsonObject().apply {
30+
addProperty("message", "Hello, World!")
31+
})
32+
}
33+
}
34+
35+
server.start(8080) // Start the server on port 8080
36+
}
37+
```
38+
39+
In this example, the server listens on port `8080` and responds with a JSON message `"Hello, World!"` when accessing the `/hello` endpoint.
40+
41+
### Examples
42+
43+
You can find additional examples in the `/examples` folder of the repository. These include:
44+
45+
1. **Hello World Example**: A basic server that responds with "Hello, World!".
46+
2. **Echo Server**: A server that echoes back any JSON data sent to it.
47+
3. **File Server**: A server that serves files from a specified directory.
48+
49+
### Running the Examples
50+
51+
To run the examples, you can use Gradle. In the root of the repository, execute the following command:
52+
53+
```bash
54+
./gradlew run -Pexample=<example-name>
55+
```
56+
57+
Replace `<example-name>` with the name of the example you want to run, such as `hello-world`, `echo-server`, or `file-server`.
58+
59+
For instance, to run the Hello World example, use:
60+
61+
```bash
62+
./gradlew run -Pexample=hello-world
63+
```
64+
65+
## License
66+
67+
Netty HttpServer is licensed under the GNU General Public License v3.0. See the [LICENSE](LICENSE) file for more details.
68+
69+
## Contributing
70+
71+
Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.
72+
73+
## Author
74+
75+
Netty HttpServer is developed and maintained by CCBlueX. It was originally part of the LiquidBounce project.
76+
77+
---
78+
79+
Feel free to explore the examples provided and adapt them to your specific needs. Happy coding!

build.gradle.kts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.0"
3+
`maven-publish`
4+
}
5+
6+
val projectName = "Netty-HttpServer"
7+
val projectDescription = "A Kotlin library for web REST APIs built on top of Netty."
8+
val licenseName = "GNU General Public License v3.0"
9+
val licenseUrl = "https://www.gnu.org/licenses/gpl-3.0.en.html"
10+
val authorName = "ccbluex"
11+
val projectUrl = "https://github.com/ccbluex/netty-httpserver"
12+
13+
group = "net.ccbluex"
14+
version = "1.0.0"
15+
16+
repositories {
17+
mavenCentral()
18+
}
19+
20+
java {
21+
sourceCompatibility = JavaVersion.VERSION_21
22+
targetCompatibility = JavaVersion.VERSION_21
23+
}
24+
25+
kotlin {
26+
jvmToolchain(21)
27+
}
28+
29+
dependencies {
30+
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
31+
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
32+
// https://mvnrepository.com/artifact/io.netty/netty-all
33+
implementation("io.netty:netty-all:4.1.82.Final")
34+
// https://mvnrepository.com/artifact/com.google.code.gson/gson
35+
implementation("com.google.code.gson:gson:2.10.1")
36+
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
37+
implementation("org.apache.tika:tika-core:2.9.2")
38+
39+
testImplementation(kotlin("test"))
40+
// https://mvnrepository.com/artifact/org.mockito/mockito-core
41+
testImplementation("org.mockito:mockito-core:5.13.0")
42+
}
43+
44+
tasks.test {
45+
useJUnitPlatform()
46+
}
47+
48+
tasks.withType<JavaCompile> {
49+
options.release.set(21)
50+
}
51+
52+
tasks.withType<Jar> {
53+
manifest {
54+
attributes(
55+
"Implementation-Title" to projectName,
56+
"Implementation-Version" to version,
57+
"Implementation-Vendor" to authorName,
58+
"License" to licenseName,
59+
"License-Url" to licenseUrl
60+
)
61+
}
62+
63+
// Include LICENSE file in the JAR
64+
from("LICENSE") {
65+
into("META-INF/")
66+
}
67+
}
68+
69+
publishing {
70+
publications {
71+
create<MavenPublication>("pub") {
72+
from(components["java"])
73+
74+
pom {
75+
name.set(projectName)
76+
description.set(projectDescription)
77+
url.set(projectUrl)
78+
79+
licenses {
80+
license {
81+
name.set(licenseName)
82+
url.set(licenseUrl)
83+
}
84+
}
85+
86+
developers {
87+
developer {
88+
id.set("ccbluex")
89+
name.set(authorName)
90+
}
91+
}
92+
93+
scm {
94+
connection.set("scm:git:git://github.com/ccbluex/netty-httpserver.git")
95+
developerConnection.set("scm:git:ssh://github.com:ccbluex/netty-httpserver.git")
96+
url.set(projectUrl)
97+
}
98+
}
99+
}
100+
}
101+
}

examples/echo-server/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.gradle
2+
**/build/
3+
!src/**/build/
4+
5+
# Ignore Gradle GUI config
6+
gradle-app.setting
7+
8+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
9+
!gradle-wrapper.jar
10+
11+
# Avoid ignore Gradle wrappper properties
12+
!gradle-wrapper.properties
13+
14+
# Cache of project
15+
.gradletasknamecache
16+
17+
# Eclipse Gradle plugin generated files
18+
# Eclipse Core
19+
.project
20+
# JDT-specific (Eclipse Java Development Tools)
21+
.classpath
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.0"
3+
application
4+
}
5+
group = "net.ccbluex"
6+
version = "1.0.0"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
java {
13+
sourceCompatibility = JavaVersion.VERSION_21
14+
targetCompatibility = JavaVersion.VERSION_21
15+
}
16+
17+
kotlin {
18+
jvmToolchain(21)
19+
}
20+
21+
dependencies {
22+
implementation(project(":"))
23+
24+
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
25+
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
26+
// https://mvnrepository.com/artifact/io.netty/netty-all
27+
implementation("io.netty:netty-all:4.1.82.Final")
28+
// https://mvnrepository.com/artifact/com.google.code.gson/gson
29+
implementation("com.google.code.gson:gson:2.10.1")
30+
// https://mvnrepository.com/artifact/org.apache.tika/tika-core
31+
implementation("org.apache.tika:tika-core:2.9.2")
32+
}
33+
34+
application {
35+
mainClass.set("EchoServerExampleKt") // Specify the main class to run the example
36+
}
37+
38+
tasks.test {
39+
useJUnitPlatform()
40+
}
41+
42+
tasks.withType<JavaCompile> {
43+
options.release.set(21)
44+
}
45+
46+
tasks.withType<Jar> {
47+
manifest {
48+
attributes["Main-Class"] = "EchoServerExampleKt"
49+
}
50+
51+
// Include runtime dependencies in the JAR
52+
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
53+
// Prevent duplicate files from being added to the JAR
54+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import com.google.gson.JsonObject
2+
import net.ccbluex.netty.http.HttpServer
3+
import net.ccbluex.netty.http.util.httpOk
4+
5+
fun main() {
6+
val server = HttpServer()
7+
8+
server.routeController.apply {
9+
post("/echo") { request ->
10+
httpOk(request.asJson<JsonObject>())
11+
}
12+
}
13+
14+
server.start(8080) // Start the server on port 8080
15+
}

examples/file-server/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.gradle
2+
**/build/
3+
!src/**/build/
4+
5+
# Ignore Gradle GUI config
6+
gradle-app.setting
7+
8+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
9+
!gradle-wrapper.jar
10+
11+
# Avoid ignore Gradle wrappper properties
12+
!gradle-wrapper.properties
13+
14+
# Cache of project
15+
.gradletasknamecache
16+
17+
# Eclipse Gradle plugin generated files
18+
# Eclipse Core
19+
.project
20+
# JDT-specific (Eclipse Java Development Tools)
21+
.classpath

0 commit comments

Comments
 (0)