ButtonWebsiteModule/src/buttondevteam/website/ButtonWebsiteModule.java

39 lines
1.1 KiB
Java
Raw Normal View History

package buttondevteam.website;
2016-10-22 19:20:28 +00:00
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import com.sun.net.httpserver.HttpServer;
2016-10-22 19:20:28 +00:00
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.website.page.*;
public class ButtonWebsiteModule extends JavaPlugin {
private static HttpServer server;
@Override
public void onEnable() {
try {
server = HttpServer.create(new InetSocketAddress((InetAddress) null, 8080), 10);
addPage(new IndexPage());
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
this.getLogger().info("Starting webserver...");
((Runnable) server::start).run(); // Totally normal way of calling a method
this.getLogger().info("Webserver started");
});
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while starting the webserver!", e);
}
}
/**
* Adds a new page/endpoint to the website. This method needs to be called before the server finishes loading (onEnable).
*/
public static void addPage(Page page) {
server.createContext("/" + page.GetName(), page);
}
2016-10-22 19:20:28 +00:00
}