diff --git a/.env b/.env new file mode 100644 index 0000000000000000000000000000000000000000..097104c0bce0c91217be0becc4bb50de97736b49 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +API_ROOT=ewngu43gbipbowboi +WORKING_DIR=/Users/tobias/Documents/Programmieren/Projects/PlayWall/PlayWallServer \ No newline at end of file diff --git a/.gitignore b/.gitignore index c7c103277a1af52911cd80028e198ed3f24bacff..7ef55bd0d113ba40d2d4b5cba731e55b75f09eb2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ /settings.properties /target /deploy +/build/ +/deploy_docker/ +/storage/ diff --git a/Dockerfile b/Dockerfile index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1c602308c1e958b8a49926922890ea564f6a9616 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM openjdk:8 + +USER root + +COPY build/1.0-SNAPSHOT/playwall-server.jar /root/playwall-server.jar +WORKDIR /root + +CMD ["java", "-jar", "playwall-server.jar"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..9448130c40d49230d259406153989e9461f73fe1 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,31 @@ +version: '3.3' +services: + mysql: + image: mysql:8 + restart: always + environment: + MYSQL_ROOT_PASSWORD: ${API_ROOT} + MYSQL_DATABASE: api + MYSQL_ROOT_HOST: '%' + volumes: + - "${WORKING_DIR}/storage/mysql:/var/lib/mysql" + networks: + - netMySQL + ports: + - "3306:3306" + playwall: + image: jtobisan/playwall-server:latest + restart: always + volumes: + - "${WORKING_DIR}/deploy_docker:/root/deploy" + expose: + - "8090" + ports: + - "8080:8080" + networks: + - netMySQL + depends_on: + - mysql +networks: + netMySQL: + driver: bridge diff --git a/pom.xml b/pom.xml index f09eca63bee116503aa16677b24cd41aa354879c..ebf1d796919c12e819c858fb2380cfedea53daf0 100644 --- a/pom.xml +++ b/pom.xml @@ -9,8 +9,14 @@ <version>1.0-SNAPSHOT</version> <properties> - <java-version>1.8</java-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <project.main>de.tobias.playpad.server.PlayPadServer</project.main> + <java.version>1.8</java.version> + + <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version> + <scala-maven-plugin.version>3.4.4</scala-maven-plugin.version> + <maven-jar-plugin.version>2.3.1</maven-jar-plugin.version> <scala-library.version>[2.13.0,)</scala-library.version> @@ -86,22 +92,50 @@ </dependencies> <build> + <!-- <finalName>PlayWallServer</finalName>--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> - <version>3.8.1</version> + <version>${maven-compiler-plugin.version}</version> <configuration> - <source>${java-version}</source> - <target>${java-version}</target> + <source>${java.version}</source> + <target>${java.version}</target> </configuration> </plugin> + + <plugin> + <groupId>net.alchim31.maven</groupId> + <artifactId>scala-maven-plugin</artifactId> + <version>${scala-maven-plugin.version}</version> + <executions> + <execution> + <id>scala-compile-first</id> + <phase>process-resources</phase> + <goals> + <goal>add-source</goal> + <goal>compile</goal> + </goals> + </execution> + <execution> + <id>scala-test-compile</id> + <phase>process-test-resources</phase> + <goals> + <goal>testCompile</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> + <outputDirectory>build/${project.version}</outputDirectory> + <finalName>${project.artifactId}</finalName> + <appendAssemblyId>false</appendAssemblyId> <archive> <manifest> - <mainClass>de.tobias.playpad.server.PlayPadServer</mainClass> + <mainClass>${project.main}</mainClass> </manifest> </archive> <descriptorRefs> @@ -118,22 +152,22 @@ </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> + <distributionManagement> + <repository> + <id>release</id> + <name>TheCodeLabs-releases</name> + <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-release</url> + </repository> + <snapshotRepository> + <id>snapshots</id> + <name>TheCodeLabs-snapshots</name> + <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-snapshots</url> + </snapshotRepository> + </distributionManagement> + <pluginRepositories> <pluginRepository> <id>scala</id> @@ -146,8 +180,29 @@ <enabled>false</enabled> </snapshots> </pluginRepository> + <pluginRepository> + <id>release</id> + <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-plugins-release</url> + <releases> + <enabled>true</enabled> + </releases> + <snapshots> + <enabled>false</enabled> + </snapshots> + </pluginRepository> + <pluginRepository> + <id>snapshots</id> + <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-plugins-snapshots</url> + <releases> + <enabled>false</enabled> + </releases> + <snapshots> + <enabled>true</enabled> + </snapshots> + </pluginRepository> </pluginRepositories> + <repositories> <repository> <id>scala</id> @@ -163,7 +218,7 @@ <repository> <id>release</id> - <url>https://maven.thecodelabs.de/artifactory/TheCodeLabs-release</url> + <url>https://maven.thecodelabs.de/artifactory/Releases</url> <releases> <enabled>true</enabled> </releases> diff --git a/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala b/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala index 97933915fc8a441768685239c3a90d9378dc3e13..32c3405948b3859cbbe205f8401dbd5eec7ec6a8 100644 --- a/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala +++ b/src/main/scala/de/tobias/playpad/server/PlayPadServer.scala @@ -1,7 +1,7 @@ package de.tobias.playpad.server -import java.nio.file.{Files, Paths} -import java.sql.DriverManager +import java.nio.file.{Files, Path, Paths} +import java.sql.{Connection, DriverManager} import com.j256.ormlite.dao.{Dao, DaoManager} import com.j256.ormlite.jdbc.JdbcConnectionSource @@ -16,7 +16,7 @@ import de.tobias.playpad.server.server.account._ import de.tobias.playpad.server.server.plugin.{PluginGet, PluginList} import de.tobias.playpad.server.server.project._ import de.tobias.playpad.server.server.project.sync.ProjectSyncHandler -import de.tobias.playpad.server.settings.SettingsHandler +import de.tobias.playpad.server.settings.{Settings, SettingsHandler, SettingsLoader} import de.tobias.playpad.server.transformer.JsonTransformer import spark.Spark._ @@ -25,80 +25,92 @@ import spark.Spark._ */ object PlayPadServer extends App { - val app = ApplicationUtils.registerMainApplication(getClass) - ApplicationUtils.addAppListener(_ => { - Logger.init(app.getPath(PathType.LOG)) - Slf4JLoggerAdapter.disableSlf4jDebugPrints() - Logger.setLevelFilter(LogLevelFilter.DEBUG) - Logger.setFileOutput(FileOutputOption.COMBINED) - }) - app.start(args) - - // Load Config - private val settingsLoader = SettingsHandler.loader - private val settingsPath = Paths.get("settings.properties") - - if (Files.notExists(settingsPath)) { - SettingsHandler.saver.default(settingsPath) + private var settingsLoader: SettingsLoader = _ + private var settingsPath: Path = _ + private var settings: Settings = _ + + private var databaseUrl: String = _ + var connectionSource: JdbcConnectionSource = _ + private var databaseConnection: Connection = _ + + try { + val app = ApplicationUtils.registerMainApplication(getClass) + ApplicationUtils.addAppListener(_ => { + Logger.init(app.getPath(PathType.LOG)) + Slf4JLoggerAdapter.disableSlf4jDebugPrints() + Logger.setLevelFilter(LogLevelFilter.DEBUG) + Logger.setFileOutput(FileOutputOption.COMBINED) + }) + app.start(args) + + // Load Config + settingsLoader = SettingsHandler.loader + settingsPath = Paths.get("deploy/settings.properties") + + if (Files.notExists(settingsPath)) { + SettingsHandler.saver.default(settingsPath) + } + + settings = settingsLoader.load(settingsPath) + + databaseUrl = s"jdbc:mysql://${settings.db_host}:${settings.db_port}/${settings.db_database}?" + + s"autoReconnect=true&wait_timeout=86400&serverTimezone=Europe/Berlin" + connectionSource = new JdbcConnectionSource(databaseUrl) + connectionSource.setUsername(settings.db_username) + connectionSource.setPassword(settings.db_password) + + databaseConnection = DriverManager.getConnection(databaseUrl, settings.db_username, settings.db_password) + + val pluginDao: Dao[Plugin, Int] = DaoManager.createDao(connectionSource, classOf[Plugin]) + val accountDao: Dao[Account, Int] = DaoManager.createDao(connectionSource, classOf[Account]) + val sessionDao: Dao[Session, Int] = DaoManager.createDao(connectionSource, classOf[Session]) + + // Management Tables + TableUtils.createTableIfNotExists(connectionSource, classOf[Plugin]) + TableUtils.createTableIfNotExists(connectionSource, classOf[Account]) + TableUtils.createTableIfNotExists(connectionSource, classOf[Session]) + SqlHelper.createTables(databaseConnection) + + // Setup Http Server + port(8090) + threadPool(8, 2, 60 * 60 * 1000) + + val externalPath = Paths.get(settings.download_folder).toAbsolutePath.toString + externalStaticFileLocation(externalPath) + + //secure("deploy/keystore.jks", settings.keystorePassword, null, null) + + // PlayWall Cloud + webSocket("/project", new ProjectSyncHandler(sessionDao, databaseConnection)) + + // Project + val transformer = new JsonTransformer + + get("/projects/:id", new ProjectGet(databaseConnection, sessionDao), transformer) + get("/projects", new ProjectList(databaseConnection, sessionDao), transformer) + post("/projects", new ProjectPost(databaseConnection, sessionDao), transformer) + delete("/projects", new ProjectDelete(databaseConnection, sessionDao), transformer) + + // Plugins + get("/plugins/:id", new PluginGet(pluginDao), transformer) + get("/plugins", new PluginList(pluginDao), transformer) + + // Account + post("/accounts", new AccountPost(accountDao), transformer) + put("/accounts", new AccountPut(accountDao), transformer) + + post("/sessions", new SessionPost(accountDao), transformer) + delete("/sessions", new SessionDelete(accountDao), transformer) + get("/sessions", new SessionGet(accountDao), transformer) + + SettingsHandler.saver.save(settings, settingsPath) + + Runtime.getRuntime.addShutdownHook(new Thread(() => { + databaseConnection.close() + connectionSource.close() + stop() + })) + } catch { + case e: Exception => e.printStackTrace() } - - private val settings = settingsLoader.load(settingsPath) - - private val databaseUrl = s"jdbc:mysql://${settings.db_host}:${settings.db_port}/${settings.db_database}?" + - s"autoReconnect=true&wait_timeout=86400&serverTimezone=Europe/Berlin" - var connectionSource = new JdbcConnectionSource(databaseUrl) - connectionSource.setUsername(settings.db_username) - connectionSource.setPassword(settings.db_password) - - private val databaseConnection = DriverManager.getConnection(databaseUrl, settings.db_username, settings.db_password) - - val pluginDao: Dao[Plugin, Int] = DaoManager.createDao(connectionSource, classOf[Plugin]) - val accountDao: Dao[Account, Int] = DaoManager.createDao(connectionSource, classOf[Account]) - val sessionDao: Dao[Session, Int] = DaoManager.createDao(connectionSource, classOf[Session]) - - // Management Tables - TableUtils.createTableIfNotExists(connectionSource, classOf[Plugin]) - TableUtils.createTableIfNotExists(connectionSource, classOf[Account]) - TableUtils.createTableIfNotExists(connectionSource, classOf[Session]) - SqlHelper.createTables(databaseConnection) - - // Setup Http Server - port(8090) - threadPool(8, 2, 60 * 60 * 1000) - - private val externalPath = Paths.get(settings.download_folder).toAbsolutePath.toString - externalStaticFileLocation(externalPath) - - //secure("deploy/keystore.jks", settings.keystorePassword, null, null) - - // PlayWall Cloud - webSocket("/project", new ProjectSyncHandler(sessionDao, databaseConnection)) - - // Project - private val transformer = new JsonTransformer - - get("/projects/:id", new ProjectGet(databaseConnection, sessionDao), transformer) - get("/projects", new ProjectList(databaseConnection, sessionDao), transformer) - post("/projects", new ProjectPost(databaseConnection, sessionDao), transformer) - delete("/projects", new ProjectDelete(databaseConnection, sessionDao), transformer) - - // Plugins - get("/plugins/:id", new PluginGet(pluginDao), transformer) - get("/plugins", new PluginList(pluginDao), transformer) - - // Account - post("/accounts", new AccountPost(accountDao), transformer) - put("/accounts", new AccountPut(accountDao), transformer) - - post("/sessions", new SessionPost(accountDao), transformer) - delete("/sessions", new SessionDelete(accountDao), transformer) - get("/sessions", new SessionGet(accountDao), transformer) - - SettingsHandler.saver.save(settings, settingsPath) - - Runtime.getRuntime.addShutdownHook(new Thread(() => { - databaseConnection.close() - connectionSource.close() - stop() - })) }