From ce8596f52ae0c60f7efc99ef4c575327ee5a15b3 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 10 May 2021 09:38:04 +0200 Subject: [PATCH] Init code push. --- NekoRIP.iml | 91 +++++++++++ pom.xml | 143 ++++++++++++++++++ .../java/net/horizoncode/nekorip/NekoRIP.java | 54 +++++++ .../horizoncode/nekorip/config/Config.java | 70 +++++++++ .../horizoncode/nekorip/mysql/MySQLAPI.java | 116 ++++++++++++++ src/main/resources/config.toml | 9 ++ 6 files changed, 483 insertions(+) create mode 100644 NekoRIP.iml create mode 100644 pom.xml create mode 100644 src/main/java/net/horizoncode/nekorip/NekoRIP.java create mode 100644 src/main/java/net/horizoncode/nekorip/config/Config.java create mode 100644 src/main/java/net/horizoncode/nekorip/mysql/MySQLAPI.java create mode 100644 src/main/resources/config.toml diff --git a/NekoRIP.iml b/NekoRIP.iml new file mode 100644 index 0000000..8796700 --- /dev/null +++ b/NekoRIP.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b24517b --- /dev/null +++ b/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + + net.horizoncode.nekorip + NekoRIP + 0.0.1 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + jar + + + com.google.code.gson + gson + 2.8.6 + + + com.google.guava + guava + 30.1.1-jre + + + com.sparkjava + spark-core + 2.9.3 + + + commons-codec + commons-codec + 1.15 + + + commons-io + commons-io + 2.8.0 + + + commons-logging + commons-logging + 1.2 + + + mysql + mysql-connector-java + 8.0.23 + + + org.json + json + 20201115 + + + org.apache.clerezza.ext + org.json.simple + 0.4 + + + org.jsoup + jsoup + 1.13.1 + + + org.yaml + snakeyaml + 1.28 + + + org.realityforge.org.jetbrains.annotations + org.jetbrains.annotations + 1.7.0 + + + org.projectlombok + lombok + 1.18.18 + + + com.diogonunes + JColor + 5.0.1 + + + org.tomlj + tomlj + 1.0.0 + + + org.freemarker + freemarker + 2.3.31 + + + redis.clients + jedis + 3.5.2 + + + commons-validator + commons-validator + 1.7 + + + org.slf4j + slf4j-simple + 1.7.30 + + + org.apache.commons + commons-text + 1.9 + + + com.inversoft + prime-transformer + 2.1.5 + + + com.paypal.sdk + merchantsdk + 2.15.122 + + + net.sargue + mailgun + 1.10.0 + + + club.minnced + discord-webhooks + 0.5.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 new file mode 100644 index 0000000..9671e1f --- /dev/null +++ b/src/main/java/net/horizoncode/nekorip/NekoRIP.java @@ -0,0 +1,54 @@ +package net.horizoncode.nekorip; + +import lombok.Getter; +import net.horizoncode.nekorip.config.Config; +import net.horizoncode.nekorip.mysql.MySQLAPI; +import org.apache.commons.lang3.Validate; + +import java.io.File; + +/** + * Created by HorizonCode at 10/05/2021 + * + * @author HorizonCode + * @since 10/05/2021 + **/ +@Getter +public class NekoRIP { + + private static NekoRIP instance; + + private Config config; + private MySQLAPI mySQL; + + public static void main(String[] args) { + new NekoRIP().init(); + } + + private void init() { + Validate.isTrue(instance == null, "Instance is already initialized."); + instance = this; + + File config = new File("config.toml"); + this.config = new Config(config); + + if(this.config.isJustCreated()){ + System.out.println("No config found, created one. Please edit the config!"); + System.gc(); + System.exit(0); + return; + } + + String host = this.config.getStringOrDefault("mysql.host", ""); + int port = this.config.getIntOrDefault("mysql.port", 3306); + String database = this.config.getStringOrDefault("mysql.database", "nekorip"); + String username = this.config.getStringOrDefault("mysql.username", "root"); + 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."); + + } + +} diff --git a/src/main/java/net/horizoncode/nekorip/config/Config.java b/src/main/java/net/horizoncode/nekorip/config/Config.java new file mode 100644 index 0000000..3c9c379 --- /dev/null +++ b/src/main/java/net/horizoncode/nekorip/config/Config.java @@ -0,0 +1,70 @@ +package net.horizoncode.nekorip.config; + +import lombok.AccessLevel; +import lombok.Getter; +import net.horizoncode.nekorip.NekoRIP; +import org.apache.commons.io.FileUtils; +import org.tomlj.Toml; +import org.tomlj.TomlParseResult; + +import java.io.File; +import java.io.IOException; + +@Getter +public class Config { + + @Getter(AccessLevel.NONE) + private final File configFile; + + @Getter(AccessLevel.PACKAGE) + private TomlParseResult toml; + + private boolean justCreated; + + + public Config(File cfg) { + configFile = cfg; + if (!configFile.exists()) { + try { + FileUtils.copyInputStreamToFile(NekoRIP.class.getResourceAsStream("/" + cfg.getName()), configFile); + justCreated = true; + } catch (IOException e) { + e.printStackTrace(); + } + } else { + if (configFile.isDirectory()) { + try { + FileUtils.copyInputStreamToFile(NekoRIP.class.getResourceAsStream("/" + cfg.getName()), configFile); + justCreated = true; + } catch (IOException e) { + e.printStackTrace(); + } + } + } + if (justCreated) + return; + + try { + toml = Toml.parse(configFile.toPath()); + if (toml.hasErrors()) { + toml.errors().forEach(error -> System.err.println(error.toString())); + System.exit(-1); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public int getIntOrDefault(String key, int defaultValue) { + return getToml().contains(key) && getToml().get(key) instanceof Integer ? (int) getToml().get(key) : defaultValue; + } + + public boolean getBooleanOrDefault(String key, boolean defaultValue) { + return getToml().contains(key) && getToml().get(key) instanceof Boolean ? getToml().getBoolean(key) : defaultValue; + } + + public String getStringOrDefault(String key, String defaultValue) { + return getToml().contains(key) ? getToml().getString(key) : defaultValue; + } + +} diff --git a/src/main/java/net/horizoncode/nekorip/mysql/MySQLAPI.java b/src/main/java/net/horizoncode/nekorip/mysql/MySQLAPI.java new file mode 100644 index 0000000..ede2fa2 --- /dev/null +++ b/src/main/java/net/horizoncode/nekorip/mysql/MySQLAPI.java @@ -0,0 +1,116 @@ +package net.horizoncode.nekorip.mysql; + + +import java.sql.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class MySQLAPI { + private static final MySQLAPI INSTANCE; + + static { + INSTANCE = new MySQLAPI(); + } + + private String host; + private int port; + private String user; + private String password; + private String database; + private Connection connection; + + private MySQLAPI() { + } + + public static MySQLAPI getInstance() { + return MySQLAPI.INSTANCE; + } + + public boolean connect(final String host, final int port, final String user, final String password, final String database) { + this.host = host; + this.port = port; + this.user = user; + this.password = password; + this.database = database; + return this.openConnection(); + } + + private boolean openConnection() { + try { + if (this.connection != null) { + this.connection.close(); + } + Class.forName("com.mysql.cj.jdbc.Driver"); + this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password); + establishConnection(); + } catch (Exception e) { + System.out.println("MySQL error while trying connecting to " + this.host + ":" + this.port + "/" + this.database + ": " + e.getMessage()); + return false; + } + System.out.println("MySQL connected to " + this.host + ":" + this.port + "/" + this.database); + return true; + } + + private void establishConnection() { + ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5); + + scheduleTaskExecutor.scheduleAtFixedRate(() -> { + try { + Statement s = getConnection().createStatement(); + s.executeQuery("SELECT 1+1"); + s.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + }, 0, 10, TimeUnit.MINUTES); + } + + public void closeConnection() { + try { + System.out.println("MySQL connection closed!"); + connection.close(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void closeRessources(final ResultSet rs, final PreparedStatement st, final Statement s) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException ex) { + } + } + if (st != null) { + try { + st.close(); + } catch (SQLException ex2) { + } + } + if (s != null) { + try { + s.close(); + } catch (SQLException ex2) { + } + } + } + + public void closeRessources(final ResultSet rs) { + this.closeRessources(rs, null, null); + } + + public void closeRessources(final PreparedStatement st) { + this.closeRessources(null, st, null); + } + + public void closeRessources(final Statement s) { + this.closeRessources(null, null, s); + } + + public Connection getConnection() { + return connection; + } + +} diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml new file mode 100644 index 0000000..f307d01 --- /dev/null +++ b/src/main/resources/config.toml @@ -0,0 +1,9 @@ +[web] +port = 62011 + +[mysql] +host = "" +port = 3306 +database = "" +username = "" +password = "" \ No newline at end of file