From aa55c21f0426b8b28a9a80e54034ad39cd4c389d Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sat, 15 Sep 2018 00:47:18 +0200 Subject: [PATCH] Move more stuff, start config site development --- build.gradle | 2 +- conf.json | 1 + src/main/kotlin/nl/voidcorp/yeetbot/Config.kt | 4 +- src/main/kotlin/nl/voidcorp/yeetbot/Util.kt | 12 ++++++ src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt | 38 ++++++++++++------- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index a2c9d4f..af226c0 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ dependencies { compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1' compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.1' compile 'com.github.salomonbrys.kotson:kotson:2.5.0' - compile "com.sparkjava:spark-kotlin:1.0.0-alpha" + compile 'io.javalin:javalin:2.1.1' } diff --git a/conf.json b/conf.json index 0df84a6..eb8fcc0 100644 --- a/conf.json +++ b/conf.json @@ -1,4 +1,5 @@ { + "httpPort": 7222, "guildWords": { "174602395630829568": { "list": [ diff --git a/src/main/kotlin/nl/voidcorp/yeetbot/Config.kt b/src/main/kotlin/nl/voidcorp/yeetbot/Config.kt index 7e9eb15..dfdb774 100644 --- a/src/main/kotlin/nl/voidcorp/yeetbot/Config.kt +++ b/src/main/kotlin/nl/voidcorp/yeetbot/Config.kt @@ -1,6 +1,6 @@ package nl.voidcorp.yeetbot -data class Config(val guildWords: MutableMap = mutableMapOf()) +data class Config(var httpPort: Int = 7222, val guildWords: MutableMap = mutableMapOf()) data class GuildInfo(val list: MutableList = mutableListOf()) @@ -11,4 +11,4 @@ enum class MatchType { RAW } -fun MutableList.contains(match: String):Boolean = this.any { it.match == match } \ No newline at end of file +fun MutableList.contains(match: String): Boolean = this.any { it.match == match } \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/yeetbot/Util.kt b/src/main/kotlin/nl/voidcorp/yeetbot/Util.kt index 1c5fd7f..cd68804 100644 --- a/src/main/kotlin/nl/voidcorp/yeetbot/Util.kt +++ b/src/main/kotlin/nl/voidcorp/yeetbot/Util.kt @@ -1,8 +1,20 @@ package nl.voidcorp.yeetbot import com.google.gson.GsonBuilder +import java.util.regex.Pattern +import java.util.regex.PatternSyntaxException val gson = GsonBuilder().setPrettyPrinting().create() +fun Any.toJson():String = gson.toJson(this) +fun checkRegex(pattern: String): Boolean { + var exc: PatternSyntaxException? = null + try { + Pattern.compile(pattern) + } catch (e: PatternSyntaxException) { + exc = e + } + return exc == null +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt b/src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt index 0e2c00e..83a859f 100644 --- a/src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt +++ b/src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt @@ -1,15 +1,19 @@ package nl.voidcorp.yeetbot import com.github.salomonbrys.kotson.fromJson -import net.dv8tion.jda.core.* +import io.javalin.Javalin +import io.javalin.json.FromJsonMapper +import io.javalin.json.JavalinJson +import io.javalin.json.ToJsonMapper +import net.dv8tion.jda.core.AccountType +import net.dv8tion.jda.core.JDABuilder +import net.dv8tion.jda.core.OnlineStatus import net.dv8tion.jda.core.entities.Game import org.apache.logging.log4j.LogManager import java.io.File import java.io.FileReader import java.io.FileWriter import java.util.* -import java.util.regex.Pattern -import java.util.regex.PatternSyntaxException import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.schedule @@ -82,16 +86,24 @@ fun main(args: Array) { } } + + JavalinJson.toJsonMapper = object : ToJsonMapper { + override fun map(obj: Any): String = obj.toJson() + } + + JavalinJson.fromJsonMapper = object : FromJsonMapper { + override fun map(json: String, targetClass: Class): T = gson.fromJson(json, targetClass) + + } + + val app = Javalin.create().start(config.httpPort) + app.get("/") { + it.result("memes!") + } + + app.get("/test") { + it.json(config) + } } -fun checkRegex(pattern: String): Boolean { - var exc: PatternSyntaxException? = null - try { - Pattern.compile(pattern) - } catch (e: PatternSyntaxException) { - exc = e - } - - return exc == null -} \ No newline at end of file