From 1f984efc3d0f3fcb35fa0f7de02a588cd0e79fed Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 10 May 2021 11:19:18 +0200 Subject: [PATCH] Added TemplateSystem --- .idea/vcs.xml | 6 ++++ .../java/net/horizoncode/nekorip/NekoRIP.java | 36 ++++++++++++++++--- .../nekorip/template/BaseTemplateRoute.java | 36 +++++++++++++++++++ src/main/java/static/home.html | 1 + src/main/resources/config.toml | 3 +- 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/net/horizoncode/nekorip/template/BaseTemplateRoute.java create mode 100644 src/main/java/static/home.html diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/net/horizoncode/nekorip/NekoRIP.java b/src/main/java/net/horizoncode/nekorip/NekoRIP.java index 9671e1f..bf2134f 100644 --- a/src/main/java/net/horizoncode/nekorip/NekoRIP.java +++ b/src/main/java/net/horizoncode/nekorip/NekoRIP.java @@ -1,9 +1,13 @@ package net.horizoncode.nekorip; +import freemarker.template.Configuration; +import freemarker.template.TemplateExceptionHandler; import lombok.Getter; import net.horizoncode.nekorip.config.Config; import net.horizoncode.nekorip.mysql.MySQLAPI; +import net.horizoncode.nekorip.template.BaseTemplateRoute; import org.apache.commons.lang3.Validate; +import spark.Spark; import java.io.File; @@ -16,10 +20,14 @@ import java.io.File; @Getter public class NekoRIP { + @Getter private static NekoRIP instance; + private final Configuration templates = new Configuration(Configuration.VERSION_2_3_27); + private Config config; private MySQLAPI mySQL; + private BaseTemplateRoute baseTemplateRoute; public static void main(String[] args) { new NekoRIP().init(); @@ -32,13 +40,13 @@ public class NekoRIP { File config = new File("config.toml"); this.config = new Config(config); - if(this.config.isJustCreated()){ + if (this.config.isJustCreated()) { System.out.println("No config found, created one. Please edit the config!"); System.gc(); System.exit(0); return; } - + System.out.println("Connecting to MySQL-Server..."); String host = this.config.getStringOrDefault("mysql.host", ""); int port = this.config.getIntOrDefault("mysql.port", 3306); String database = this.config.getStringOrDefault("mysql.database", "nekorip"); @@ -46,8 +54,28 @@ public class NekoRIP { String password = this.config.getStringOrDefault("mysql.password", ""); mySQL = MySQLAPI.getInstance(); - if(!mySQL.connect(host, port, username, password, database)) - System.out.println("Failed to connect to mysql."); + if (!mySQL.connect(host, port, username, password, database)) + System.out.println("Failed to connect to the MySQL-Server."); + else + System.out.println("Successfully connected to the MySQL-Server."); + + templates.setDefaultEncoding("UTF-8"); + templates.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + templates.setLogTemplateExceptions(false); + templates.setWrapUncheckedExceptions(true); + templates.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX); + + String web_host = this.config.getStringOrDefault("web.host", "0.0.0.0"); + int web_port = this.config.getIntOrDefault("web.port", 8000); + + Spark.ipAddress(web_host); + Spark.port(web_port); + Spark.staticFileLocation("/static/"); + + System.out.println("Setting up Routes..."); + Spark.get("/", baseTemplateRoute = new BaseTemplateRoute("home.html")); + + System.out.printf("Listening on %s:%d...%n", web_host, web_port); } diff --git a/src/main/java/net/horizoncode/nekorip/template/BaseTemplateRoute.java b/src/main/java/net/horizoncode/nekorip/template/BaseTemplateRoute.java new file mode 100644 index 0000000..8ba69d8 --- /dev/null +++ b/src/main/java/net/horizoncode/nekorip/template/BaseTemplateRoute.java @@ -0,0 +1,36 @@ +package net.horizoncode.nekorip.template; + +import com.google.common.collect.Maps; +import freemarker.template.Template; +import net.horizoncode.nekorip.NekoRIP; +import spark.Request; +import spark.Response; +import spark.Route; + +import java.io.StringWriter; +import java.io.Writer; +import java.util.concurrent.ConcurrentMap; + +/** + * Created by HorizonCode at 10/05/2021 + * + * @author HorizonCode + * @since 10/05/2021 + **/ +public class BaseTemplateRoute implements Route { + + private final String templateName; + + public BaseTemplateRoute(String templateName) { + this.templateName = templateName; + } + + @Override + public Object handle(Request request, Response response) throws Exception { + ConcurrentMap placeHolderList = Maps.newConcurrentMap(); + Template template = NekoRIP.getInstance().getTemplates().getTemplate(templateName); + Writer compiledTemplate = new StringWriter(); + template.process(placeHolderList, compiledTemplate); + return compiledTemplate.toString(); + } +} diff --git a/src/main/java/static/home.html b/src/main/java/static/home.html new file mode 100644 index 0000000..fa15cab --- /dev/null +++ b/src/main/java/static/home.html @@ -0,0 +1 @@ +Yes. \ No newline at end of file diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml index f307d01..3a3e7de 100644 --- a/src/main/resources/config.toml +++ b/src/main/resources/config.toml @@ -1,5 +1,6 @@ [web] -port = 62011 +host = "0.0.0.0" +port = 8000 [mysql] host = ""