Skip to content

Commit 144eb3d

Browse files
committed
HTTP API endpoint on port 8070
1 parent 9214d1e commit 144eb3d

6 files changed

Lines changed: 113 additions & 98 deletions

File tree

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1+
# Fish API
2+
13
### Setup
24

35
Clone this repository first.
46
To edit the plugin display name and other data, take a look at `plugin.json`.
57
Edit the name of the project itself by going into `settings.gradle`.
68

7-
### Basic Usage
8-
9-
See `src/example/ExamplePlugin.java` for some basic commands and event handlers.
10-
Every main plugin class must extend `Plugin`. Make sure that `plugin.json` points to the correct main plugin class.
11-
12-
Please note that the plugin system is in beta, and as such is subject to changes.
13-
149
### Building a Jar
1510

1611
`gradlew jar` / `./gradlew jar`
1712

1813
Output jar should be in `build/libs`.
1914

20-
2115
### Installing
2216

2317
Simply place the output jar from the step above in your server's `config/mods` directory and restart the server.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repositories{
1717

1818
ext{
1919
//the build number that this plugin is made for
20-
mindustryVersion = 'v145'
20+
mindustryVersion = 'v146'
2121
jabelVersion = "93fde537c7"
2222
}
2323

plugin.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "ExamplePlugin",
3-
"author": "Anuke",
4-
"main": "example.ExamplePlugin",
5-
"description": "A plugin to demonstrate some basic features.",
2+
"name": "Fish API",
3+
"author": "BalaM314",
4+
"main": "fish.api.FishApiPlugin",
5+
"description": "Contains an HTTP API, and exposes certain functions to JS.",
66
"version": 1.0
7-
}
7+
}

src/example/ExamplePlugin.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/fish/api/FishApiPlugin.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fish.api;
2+
3+
import arc.util.*;
4+
import fish.api.http.Api;
5+
import mindustry.mod.*;
6+
7+
import java.io.IOException;
8+
9+
public class FishApiPlugin extends Plugin {
10+
11+
//called when game initializes
12+
@Override
13+
public void init(){
14+
try {
15+
Api.setupServer(8070);
16+
} catch(IOException e){
17+
Log.err("Failed to setup the API server");
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
}

src/fish/api/http/Api.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package fish.api.http;
2+
3+
import arc.ApplicationListener;
4+
import arc.Core;
5+
import arc.util.Log;
6+
import arc.util.Threads;
7+
import arc.util.serialization.*;
8+
import com.sun.net.httpserver.*;
9+
import java.io.*;
10+
import java.net.*;
11+
import mindustry.*;
12+
import mindustry.core.*;
13+
import mindustry.gen.*;
14+
import mindustry.net.Administration.*;
15+
16+
17+
public class Api {
18+
static HttpServer server;
19+
20+
public static void respondString(HttpExchange t, int status, String data) throws IOException {
21+
var bytes = data.getBytes();
22+
t.getResponseHeaders().add("Content-Type", "application/json");
23+
t.sendResponseHeaders(status, bytes.length);
24+
OutputStream os = t.getResponseBody();
25+
os.write(bytes);
26+
os.close();
27+
Log.info("Done");
28+
}
29+
30+
public static void respondBlank(HttpExchange t, int status) throws IOException {
31+
t.sendResponseHeaders(status, 0);
32+
t.getResponseBody().close();
33+
}
34+
35+
public static void writeStatus(JsonWriter json) throws IOException {
36+
String description = !Config.desc.string().equals("off") ? Config.desc.string() : "";
37+
38+
json.object()
39+
.name("name").value(Config.serverName.string())
40+
.name("description").value(description)
41+
.name("mapName").value(Vars.state.map.name())
42+
.name("playerCount").value(Groups.player.size())
43+
.name("limit").value(Vars.netServer.admins.getPlayerLimit())
44+
.name("wave").value(Vars.state.wave)
45+
.name("version").value(Version.build)
46+
.name("gamemode").value(Vars.state.rules.mode());
47+
}
48+
49+
public static void setupServer(int port) throws IOException {
50+
server = HttpServer.create(new InetSocketAddress(port), 0);
51+
server.createContext("/api", t -> {
52+
try {
53+
switch(t.getRequestMethod()){
54+
case "GET" -> {
55+
switch(t.getRequestURI().getPath()){
56+
case "/api/v1/status" -> {
57+
var buffer = new StringWriter();
58+
var writer = new JsonWriter(buffer);
59+
writeStatus(writer);
60+
writer.close();
61+
Log.info(buffer.toString());
62+
respondString(t, 200, buffer.toString());
63+
}
64+
default -> respondBlank(t, 404);
65+
}
66+
}
67+
default -> respondBlank(t, 405);
68+
}
69+
} catch(Exception e){
70+
e.printStackTrace();
71+
respondBlank(t, 500);
72+
}
73+
});
74+
server.setExecutor(Threads.executor("http", 1));
75+
server.start();
76+
Core.app.addListener(new ApplicationListener() {
77+
@Override
78+
public void dispose() {
79+
server.stop(0);
80+
}
81+
});
82+
}
83+
}

0 commit comments

Comments
 (0)