|
| 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! |
0 commit comments