Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/offsetmods538/mesh-lib
Easy to use library for hosting an HTTP server on the Minecraft server's port
https://github.com/offsetmods538/mesh-lib
http http-server minecraft minecraft-fabric minecraft-library minecraft-mod netty netty-http netty-server
Last synced: 17 days ago
JSON representation
Easy to use library for hosting an HTTP server on the Minecraft server's port
- Host: GitHub
- URL: https://github.com/offsetmods538/mesh-lib
- Owner: OffsetMods538
- License: mit
- Created: 2024-10-12T13:54:53.000Z (24 days ago)
- Default Branch: master
- Last Pushed: 2024-10-17T11:34:34.000Z (19 days ago)
- Last Synced: 2024-10-19T15:24:58.209Z (17 days ago)
- Topics: http, http-server, minecraft, minecraft-fabric, minecraft-library, minecraft-mod, netty, netty-http, netty-server
- Language: Java
- Homepage:
- Size: 88.9 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MESH Lib
[![discord-singular](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy/social/discord-singular_vector.svg)](https://discord.offsetmonkey538.top/)
[![modrinth](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/cozy/available/modrinth_vector.svg)](https://modrinth.com/mod/mesh-lib)Easy to use library for running your HTTP server on the same port as the Minecraft server.
Javadoc is available [here](https://maven.offsetmonkey538.top/javadoc/releases/top/offsetmonkey538/meshlib/mesh-lib/latest)
## Version Support
I am hoping to keep this mod supported for 1.19 up to latest.## Usage
### Players
Players shouldn't ever need to install this mod.
It should be JIJ-ed by mod devs### Mod Devs
#### Gradle
Add my maven repo to your repositories block:
```groovy
repositories {
// Others
maven {
name = "OffsetMods538"
url = "https://maven.offsetmonkey538.top/releases"
content {
includeGroup "top.offsetmonkey538.meshlib"
}
}
}
```Then include the library in your dependencies block:
```groovy
dependencies {
// Others
include modImplementation("top.offsetmonkey538.meshlib:mesh-lib:1.0.0+1.21")
}
```
Make sure to use the latest version.#### Using
Let's write a simple http server that will live at `http://server.com:25565/simple-server` and just serve `"Hello, World!"` in plain text!
First we'll need the actual `HttpHandler`, for that we'll create a new class, let's call it `MyHttpHandler`.
This class has to implement the `HttpHandler` interface, this will look something like this:
```java
public class MyHttpHandler implements HttpHandler {@Override
public void handleRequest(@NotNull ChannelHandlerContext ctx, @NotNull FullHttpRequest request) throws Exception {
// Logic will go here
}
}
```Now we'll need to actually implement the handler. You can google "HTTP Netty" for more info on how to handle HTTP requests with Netty.
```java
public void handleRequest(@NotNull ChannelHandlerContext ctx, @NotNull FullHttpRequest request) throws Exception {
// Write "Hello, World!" to a buffer, encoded in UTF-8
final ByteBuf content = Unpooled.copiedBuffer("Hello, World!", StandardCharsets.UTF_8);
// Create a response with said buffer
final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);// Set the "CONTENT_TYPE" header to tell the browser that this is plain text encoded in UTF-8
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");// Send the response and close the connection
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
```Finally, you'll need to actually register your handler. Put this in your mod initializer:
```java
@Override
public void onInitializeServer() {
// Others
HttpHandlerRegistry.INSTANCE.register("simple-server", new MyHttpHandler());
}
```
The id is the path that your handler will be able to listen on, in this case `server:port/simple-server`. For compatibility reasons, no mod is allowed to occupy the root path.
If you need to listen to multiple paths (`server:port/simple-server/test` and `server:port/simple-server/test2`), then use `request.uri()` inside your handler, do **not** use `simple-server/test` as the id, it **will not** work.Now, if you launch the server and try visiting `localhost:25565/simple-server` in your browser of choice, you should be greeted with a nice welcome message :D
If not, then come yell at me on my [discord](http://discord.offsetmonkey538.top).