From 5cb1d9a4bc8dae4973ebf35b4a0b8c0def3266fa Mon Sep 17 00:00:00 2001 From: Kevin Belisle Date: Fri, 25 Jun 2021 15:56:20 -0400 Subject: [PATCH] Include Javalin and add a periodic function --- app/build.gradle.kts | 6 ++++ app/src/main/kotlin/xyz/etztech/stonks/App.kt | 31 ++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 13a2b2d..afd468c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -34,6 +34,12 @@ dependencies { // Use the Kotlin JUnit integration. testImplementation("org.jetbrains.kotlin:kotlin-test-junit") + + compile("org.slf4j:slf4j-simple:1.7.30") + + compile("io.javalin:javalin:3.13.8") + + compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0") } application { diff --git a/app/src/main/kotlin/xyz/etztech/stonks/App.kt b/app/src/main/kotlin/xyz/etztech/stonks/App.kt index c121fbe..84a426d 100644 --- a/app/src/main/kotlin/xyz/etztech/stonks/App.kt +++ b/app/src/main/kotlin/xyz/etztech/stonks/App.kt @@ -3,13 +3,30 @@ */ package xyz.etztech.stonks -class App { - val greeting: String - get() { - return "Hello World!" - } -} +import io.javalin.Javalin +import kotlinx.coroutines.* fun main() { - println(App().greeting) + println("Starting Stonks...") + + var apiServerPort = 7000 + initApiServer(apiServerPort) + + var periodicFetchingInterval = 1000L + initPeriodicFetching(periodicFetchingInterval) } + +fun initApiServer(apiServerPort: Int) { + val app = Javalin.create().start(apiServerPort) + app.get("/") { ctx -> ctx.result("Hello World") } +} + +fun initPeriodicFetching(interval: Long) = + runBlocking { + launch { + while (true) { + println("Stonks!") + delay(interval) + } + } + }