From 30e42f730ad96ee35ee13c76fbe93ec70c2b1314 Mon Sep 17 00:00:00 2001 From: tobias <thinkdifferent055@gmail.com> Date: Wed, 1 Feb 2017 15:05:45 +0100 Subject: [PATCH] Add maven project and start basic server --- pom.xml | 123 ++++++++++++++++++ .../resources/settings/settings.properties | 6 + .../tobias/playpad/server/PlayPadServer.scala | 56 ++++++++ .../tobias/playpad/server/plugin/Plugin.scala | 27 ++++ src/test/java/ClientTest.java | 47 +++++++ 5 files changed, 259 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/resources/settings/settings.properties create mode 100644 src/main/scala/de/tobias/playpad/server/PlayPadServer.scala create mode 100644 src/main/scala/de/tobias/playpad/server/plugin/Plugin.scala create mode 100644 src/test/java/ClientTest.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2bd0e7a --- /dev/null +++ b/pom.xml @@ -0,0 +1,123 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>de.tobias</groupId> + <artifactId>playwall-server</artifactId> + <version>1.0-SNAPSHOT</version> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>de.tobias.playpad.server.PlayPadServer</mainClass> + </manifest> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + <executions> + <execution> + <id>make-assembly</id> <!-- this is used for inheritance merges --> + <phase>package</phase> <!-- bind to the packaging phase --> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.scala-tools</groupId> + <artifactId>maven-scala-plugin</artifactId> + <version>2.15.2</version> + <executions> + <execution> + <goals> + <goal>compile</goal> + <goal>testCompile</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <pluginRepositories> + <pluginRepository> + <id>scala</id> + <name>Scala Tools</name> + <url>http://scala-tools.org/repo-releases/</url> + <releases> + <enabled>true</enabled> + </releases> + <snapshots> + <enabled>false</enabled> + </snapshots> + </pluginRepository> + </pluginRepositories> + + <repositories> + <repository> + <id>scala</id> + <name>Scala Tools</name> + <url>http://scala-tools.org/repo-releases/</url> + <releases> + <enabled>true</enabled> + </releases> + <snapshots> + <enabled>false</enabled> + </snapshots> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.scala-lang</groupId> + <artifactId>scala-library</artifactId> + <version>2.12.1</version> + </dependency> + <dependency> + <groupId>com.sparkjava</groupId> + <artifactId>spark-core</artifactId> + <version>2.5.4</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <version>1.7.21</version> + </dependency> + <dependency> + <groupId>com.j256.ormlite</groupId> + <artifactId>ormlite-core</artifactId> + <version>5.0</version> + </dependency> + <dependency> + <groupId>com.j256.ormlite</groupId> + <artifactId>ormlite-jdbc</artifactId> + <version>5.0</version> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>6.0.5</version> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.0</version> + </dependency> + </dependencies> +</project> \ No newline at end of file diff --git a/src/main/resources/settings/settings.properties b/src/main/resources/settings/settings.properties new file mode 100644 index 0000000..b0278a5 --- /dev/null +++ b/src/main/resources/settings/settings.properties @@ -0,0 +1,6 @@ +# Database +host=localhost +port=3306 +username=root +password=password +database=PlayWall \ No newline at end of file diff --git a/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala b/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala new file mode 100644 index 0000000..238371a --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala @@ -0,0 +1,56 @@ +package de.tobias.playpad.server + +import java.nio.file.{Files, Paths} +import java.util.Properties + +import com.google.gson.Gson +import com.j256.ormlite.dao.{Dao, DaoManager} +import com.j256.ormlite.jdbc.JdbcConnectionSource +import com.j256.ormlite.table.TableUtils +import de.tobias.playpad.server.plugin.Plugin +import spark.Spark +import spark.Spark._ +import spark.route.RouteOverview + +/** + * Created by tobias on 29.01.17. + */ +object PlayPadServer extends App { + + // Load Config + val properties = new Properties() + private val path = Paths.get("settings.properties") + if (Files.notExists(path)) { + val loader = Thread.currentThread.getContextClassLoader + val input = loader.getResourceAsStream("settings/settings.properties") + Files.copy(input, path) + } + properties.load(Files.newBufferedReader(path)) + + // Setup Database + private val host = properties.getProperty("host") + private val port = properties.getProperty("port") + private val username = properties.getProperty("username") + private val password = properties.getProperty("password") + private val database = properties.getProperty("database") + + private val databaseUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + var connectionSource = new JdbcConnectionSource(databaseUrl) + connectionSource.setUsername(username) + connectionSource.setPassword(password) + + val dao: Dao[Plugin, String] = DaoManager.createDao(connectionSource, classOf[Plugin]) + TableUtils.createTableIfNotExists(connectionSource, classOf[Plugin]) + + // Setup Http Server + Spark.port(8090) + get("/", (req, res) => "Hallo World") + + get("/plugin/list", (req, res) => { + val plugins = dao.queryForAll() + val gson = new Gson() + gson.toJson(plugins) + }) + + RouteOverview.enableRouteOverview() +} diff --git a/src/main/scala/de/tobias/playpad/server/plugin/Plugin.scala b/src/main/scala/de/tobias/playpad/server/plugin/Plugin.scala new file mode 100644 index 0000000..893de90 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/plugin/Plugin.scala @@ -0,0 +1,27 @@ +package de.tobias.playpad.server.plugin + +import com.j256.ormlite.dao.ForeignCollection +import com.j256.ormlite.field.{DatabaseField, ForeignCollectionField} +import com.j256.ormlite.table.DatabaseTable +import com.sun.xml.internal.xsom.impl.ForeignAttributesImpl + +/** + * Created by tobias on 31.01.17. + */ +@DatabaseTable(tableName = "Plugin") class Plugin() { + @DatabaseField(generatedId = true) private val id: Int = 0 + @DatabaseField var name: String = _ + @DatabaseField var description: String = _ + @DatabaseField var version: String = _ + @DatabaseField var build: Int = _ + + def this(name: String, description: String, version: String, build: Int) { + this() + this.name = name + this.description = description + this.version = version + this.build = build + } + + def getId: Int = id +} \ No newline at end of file diff --git a/src/test/java/ClientTest.java b/src/test/java/ClientTest.java new file mode 100644 index 0000000..77e384e --- /dev/null +++ b/src/test/java/ClientTest.java @@ -0,0 +1,47 @@ +import javax.net.ssl.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +/** + * Created by tobias on 26.01.17. + */ +public class ClientTest { + public static void main(String[] args) throws IOException, KeyManagementException, NoSuchAlgorithmException { + // Create a trust manager that does not validate certificate chains + TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + }}; + + // Install the all-trusting trust manager + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> true); + + URL url = new URL("https://localhost:6789"); + URLConnection conn = url.openConnection(); + + InputStream stream = conn.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } +} -- GitLab