Temporary shutdown http endpoint
This commit is contained in:
1 parent
8f5c6b8cd5
commit
8652c72cfa
3 files changed
+49
-6
No files matched your search
@@ -25,6 +25,7 @@ public class MusicBot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("log4j.skipJansi", "false");
|
||||
|
||||
DBController.init();
|
||||
|
||||
try {
|
||||
@@ -41,7 +42,7 @@ public class MusicBot {
|
||||
jda.getPresence().setActivity(Activity.watching("me load..."));
|
||||
jda.addEventListener(new NewCommandListener(), new EditHandler());
|
||||
|
||||
// WebApi webserver = new WebApi();
|
||||
WebApi webserver = new WebApi();
|
||||
jda.getPresence().setActivity(Activity.watching("you"));
|
||||
|
||||
|
||||
@@ -71,7 +72,7 @@ public class MusicBot {
|
||||
return jda;
|
||||
}
|
||||
|
||||
public static void disconnect() {
|
||||
private static void disconnect() {
|
||||
if (jda != null) {
|
||||
jda.shutdownNow();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package netneutrality.http.server;/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
* https://xtremcommunity.com/
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import netneutrality.MusicBot;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ActionHandler implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
String path = exchange.getRequestURI().getRawPath();
|
||||
|
||||
if (path.equals("/actions/shutdown")) {
|
||||
LogManager.getLogger().info("Request to shutdown received");
|
||||
exchange.sendResponseHeaders(200, "Shutting down..".length());
|
||||
OutputStream os = exchange.getResponseBody();
|
||||
os.write("Shutting down..".getBytes());
|
||||
os.close();
|
||||
MusicBot.shutdown();
|
||||
}
|
||||
|
||||
exchange.sendResponseHeaders(200, path.length());
|
||||
OutputStream os = exchange.getResponseBody();
|
||||
os.write(path.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
@@ -7,31 +7,38 @@ package netneutrality.http.server;
|
||||
*/
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import netneutrality.ThreadManager;
|
||||
import netneutrality.thread.BaseThread;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class WebApi {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WebApi.class);
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(10, new BaseThread("web-api"));
|
||||
|
||||
private int port = 9000;
|
||||
private final int port = 9000;
|
||||
private HttpServer server;
|
||||
|
||||
public WebApi() {
|
||||
logger.debug("Starting web server...");
|
||||
try {
|
||||
server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), port), 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
server.createContext("/", new RootHandler());
|
||||
server.createContext("/actions", new ActionHandler());
|
||||
server.createContext("/guild", new GuildBrowser());
|
||||
server.setExecutor(null);
|
||||
server.setExecutor(executor);
|
||||
server.start();
|
||||
logger.debug("Web server started at [" + server.getAddress().getAddress().toString() + "]");
|
||||
logger.info("Web server started at [" + server.getAddress() + "]");
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user