|
| 1 | +/* |
| 2 | + * DolphinBot - https://github.com/NeonAngelThreads/DolphinBot |
| 3 | + * Copyright (C) 2025 NeonAngelThreads (https://github.com/NeonAngelThreads) |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public |
| 6 | + * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any |
| 7 | + * later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 10 | + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 11 | + * License for more details. You should have received a copy of the GNU General Public License along with this |
| 12 | + * program. If not, see <https://www.gnu.org/licenses/>. |
| 13 | + * |
| 14 | + * https://space.bilibili.com/386644641 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.angellock.impl.api; |
| 18 | + |
| 19 | +import com.google.gson.Gson; |
| 20 | +import com.google.gson.GsonBuilder; |
| 21 | +import com.sun.net.httpserver.HttpExchange; |
| 22 | +import com.sun.net.httpserver.HttpHandler; |
| 23 | +import com.sun.net.httpserver.HttpServer; |
| 24 | +import lombok.Getter; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
| 26 | +import org.angellock.impl.RobotPlayer; |
| 27 | +import org.angellock.impl.api.websocket.LogWebSocketHandler; |
| 28 | +import org.angellock.impl.api.websocket.handlers.*; |
| 29 | +import org.angellock.impl.managers.BotManager; |
| 30 | +import org.angellock.impl.util.ConsoleTokens; |
| 31 | +import org.angellock.impl.util.reason.KickReason; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStreamReader; |
| 37 | +import java.io.OutputStream; |
| 38 | +import java.net.InetSocketAddress; |
| 39 | +import java.nio.charset.StandardCharsets; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.concurrent.Executors; |
| 45 | + |
| 46 | +@Slf4j |
| 47 | +public class HttpAPIServer { |
| 48 | + @Getter |
| 49 | + private static HttpAPIServer instance; |
| 50 | + private final HttpServer server; |
| 51 | + private final LogWebSocketHandler logWebSocketServer; |
| 52 | + private final int wsPort; |
| 53 | + |
| 54 | + public HttpAPIServer(int port) throws IOException { |
| 55 | + instance = this; |
| 56 | + this.wsPort = port + 1; |
| 57 | + this.server = HttpServer.create(new InetSocketAddress(port), 0); |
| 58 | + this.server.setExecutor(Executors.newCachedThreadPool()); |
| 59 | + |
| 60 | + registerRoutes(); |
| 61 | + |
| 62 | + // 启动日志WebSocket服务 |
| 63 | + this.logWebSocketServer = new LogWebSocketHandler(wsPort); |
| 64 | + this.logWebSocketServer.start(); |
| 65 | + |
| 66 | + this.server.start(); |
| 67 | + log.info(ConsoleTokens.colorizeText("&aHTTP API Server started on port &b{}&a, ready to accept requests"), port); |
| 68 | + log.info(ConsoleTokens.colorizeText("&aLog WebSocket Server started on port &b{}&a"), wsPort); |
| 69 | + } |
| 70 | + |
| 71 | + private void registerRoutes() { |
| 72 | + server.createContext("/api/health", new HeartBeatHandler()); |
| 73 | + server.createContext("/api/bots", new BotsHandler("GET")); |
| 74 | + server.createContext("/api/bots/start", new BotStartHandler("POST")); |
| 75 | + server.createContext("/api/bots/stop", new BotStopHandler("POST")); |
| 76 | + server.createContext("/api/bots/send-command", new CommandHandler("POST")); |
| 77 | + server.createContext("/api/config", new ConfigHandler()); |
| 78 | + server.createContext("/api/bot/create", new AddedBotHandler("POST")); |
| 79 | + } |
| 80 | + |
| 81 | + public void stop() { |
| 82 | + server.stop(0); |
| 83 | + try { |
| 84 | + logWebSocketServer.stop(); |
| 85 | + } catch (InterruptedException e) { |
| 86 | + log.error("Failed to stop WebSocket server", e); |
| 87 | + } |
| 88 | + log.info(ConsoleTokens.colorizeText("&eHTTP API Server stopped")); |
| 89 | + log.info(ConsoleTokens.colorizeText("&eLog WebSocket Server stopped")); |
| 90 | + } |
| 91 | +} |
0 commit comments