Added TemplateSystem
This commit is contained in:
parent
9eb17cd05f
commit
1f984efc3d
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal 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>
|
@ -1,9 +1,13 @@
|
|||||||
package net.horizoncode.nekorip;
|
package net.horizoncode.nekorip;
|
||||||
|
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.TemplateExceptionHandler;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.horizoncode.nekorip.config.Config;
|
import net.horizoncode.nekorip.config.Config;
|
||||||
import net.horizoncode.nekorip.mysql.MySQLAPI;
|
import net.horizoncode.nekorip.mysql.MySQLAPI;
|
||||||
|
import net.horizoncode.nekorip.template.BaseTemplateRoute;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
|
import spark.Spark;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@ -16,10 +20,14 @@ import java.io.File;
|
|||||||
@Getter
|
@Getter
|
||||||
public class NekoRIP {
|
public class NekoRIP {
|
||||||
|
|
||||||
|
@Getter
|
||||||
private static NekoRIP instance;
|
private static NekoRIP instance;
|
||||||
|
|
||||||
|
private final Configuration templates = new Configuration(Configuration.VERSION_2_3_27);
|
||||||
|
|
||||||
private Config config;
|
private Config config;
|
||||||
private MySQLAPI mySQL;
|
private MySQLAPI mySQL;
|
||||||
|
private BaseTemplateRoute baseTemplateRoute;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new NekoRIP().init();
|
new NekoRIP().init();
|
||||||
@ -38,7 +46,7 @@ public class NekoRIP {
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
System.out.println("Connecting to MySQL-Server...");
|
||||||
String host = this.config.getStringOrDefault("mysql.host", "");
|
String host = this.config.getStringOrDefault("mysql.host", "");
|
||||||
int port = this.config.getIntOrDefault("mysql.port", 3306);
|
int port = this.config.getIntOrDefault("mysql.port", 3306);
|
||||||
String database = this.config.getStringOrDefault("mysql.database", "nekorip");
|
String database = this.config.getStringOrDefault("mysql.database", "nekorip");
|
||||||
@ -47,7 +55,27 @@ public class NekoRIP {
|
|||||||
|
|
||||||
mySQL = MySQLAPI.getInstance();
|
mySQL = MySQLAPI.getInstance();
|
||||||
if (!mySQL.connect(host, port, username, password, database))
|
if (!mySQL.connect(host, port, username, password, database))
|
||||||
System.out.println("Failed to connect to mysql.");
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
1
src/main/java/static/home.html
Normal file
1
src/main/java/static/home.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
Yes.
|
@ -1,5 +1,6 @@
|
|||||||
[web]
|
[web]
|
||||||
port = 62011
|
host = "0.0.0.0"
|
||||||
|
port = 8000
|
||||||
|
|
||||||
[mysql]
|
[mysql]
|
||||||
host = ""
|
host = ""
|
||||||
|
Reference in New Issue
Block a user