|
| 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