Added TemplateSystem

This commit is contained in:
HorizonCode 2021-05-10 11:19:18 +02:00
parent 9eb17cd05f
commit 1f984efc3d
5 changed files with 77 additions and 5 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -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);
}

View File

@ -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<Object, Object> placeHolderList = Maps.newConcurrentMap();
Template template = NekoRIP.getInstance().getTemplates().getTemplate(templateName);
Writer compiledTemplate = new StringWriter();
template.process(placeHolderList, compiledTemplate);
return compiledTemplate.toString();
}
}

View File

@ -0,0 +1 @@
Yes.

View File

@ -1,5 +1,6 @@
[web]
port = 62011
host = "0.0.0.0"
port = 8000
[mysql]
host = ""