> pages = rf.getSubTypesOf(Page.class); for (Class extends Page> page : pages) { try { if (Modifier.isAbstract(page.getModifiers())) continue; Page p = page.newInstance();
+ * addPage(server, p); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
+ */ //^^ This code would load the pages dynamically - But we'll only have like, one page...
+ addPage(server, new IndexPage());
+ server.start();
+ this.getLogger().info("Webserver started");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void addPage(HttpServer server, Page page) {
+ server.createContext("/" + page.GetName(), page);
+ }
}
diff --git a/src/buttondevteam/website/data/Stats.java b/src/buttondevteam/website/data/Stats.java
new file mode 100644
index 0000000..d051729
--- /dev/null
+++ b/src/buttondevteam/website/data/Stats.java
@@ -0,0 +1,5 @@
+package buttondevteam.website.data;
+
+public class Stats {
+
+}
diff --git a/src/buttondevteam/website/page/IndexPage.java b/src/buttondevteam/website/page/IndexPage.java
new file mode 100644
index 0000000..8a687c7
--- /dev/null
+++ b/src/buttondevteam/website/page/IndexPage.java
@@ -0,0 +1,26 @@
+package buttondevteam.website.page;
+
+import java.io.IOException;
+
+import com.google.gson.Gson;
+import com.sun.net.httpserver.HttpExchange;
+
+import buttondevteam.io.IOHelper;
+import buttondevteam.website.data.Stats;
+
+public class IndexPage extends Page {
+
+ @Override
+ public void handlePage(HttpExchange exchange) throws IOException {
+ Gson gson = new Gson();
+ Stats request = gson.fromJson(IOHelper.GetPOSTJSON(exchange), Stats.class); // TODO: Change to a request class
+ Stats response = new Stats();
+ IOHelper.SendResponse(200, gson.toJson(response), exchange);
+ }
+
+ @Override
+ public String GetName() {
+ return "";
+ }
+
+}
diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java
new file mode 100644
index 0000000..ffe21c3
--- /dev/null
+++ b/src/buttondevteam/website/page/Page.java
@@ -0,0 +1,41 @@
+package buttondevteam.website.page;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+
+import com.sun.net.httpserver.*;
+
+import buttondevteam.io.IOHelper;
+
+/**
+ * Add to {@link Main}.Pages
+ */
+public abstract class Page implements HttpHandler {
+ public abstract String GetName();
+
+ @Override
+ public void handle(HttpExchange exchange) {
+ try {
+ if (exchange.getRequestURI().getPath().equals("/" + GetName()))
+ handlePage(exchange);
+ else {
+ IOHelper.SendResponse(404, "404 Not found", exchange);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PrintStream str = new PrintStream(baos);
+ str.print("500 Internal Server Error
");
+ e.printStackTrace(str);
+ str.print("
");
+ IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange);
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+ }
+
+ public abstract void handlePage(HttpExchange exchange) throws IOException;
+}