Skip to content

Commit

Permalink
Initial Kotlin/JS code
Browse files Browse the repository at this point in the history
  • Loading branch information
joreilly committed Sep 22, 2019
1 parent a591383 commit 390f1e3
Show file tree
Hide file tree
Showing 31 changed files with 574 additions and 85 deletions.
21 changes: 21 additions & 0 deletions SharedCode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ kotlin {
}

fromPreset(presets.android, 'android')

}

js {
compileKotlinJs {
kotlinOptions.metaInfo = true
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = "commonjs"
kotlinOptions.main = "call"
}
}


cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
Expand Down Expand Up @@ -138,6 +149,16 @@ kotlin {
// SQL Delight
implementation "com.squareup.sqldelight:sqlite-driver:${Versions.sqlDelight}"
}

jsMain.dependencies {
api Kotlin.stdlibJs
api Kotlin.serializationRuntimeJs
api Ktor.clientJs
api Ktor.clientJsonJs
api Ktor.clientLoggingJs
api Ktor.clientSerializationJs
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.surrus.galwaybus.db.MyDatabase

lateinit var appContext: Context

actual fun createDb(): MyDatabase {
actual fun createDb(): MyDatabase? {
val driver = AndroidSqliteDriver(MyDatabase.Schema, appContext, "galwaybus.db")
return MyDatabase(driver)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,42 @@ import kotlinx.coroutines.launch



expect fun createDb() : MyDatabase
expect fun createDb() : MyDatabase?

class GalwayBusRepository {

private val galwayBusApi = GalwayBusApi()
private val galwayBusDb = createDb()
private val galwayBusQueries = galwayBusDb.galwayBusQueries
private val galwayBusQueries = galwayBusDb?.galwayBusQueries

init {
GlobalScope.launch (ApplicationDispatcher) {
fetchAndStoreBusStops()
//fetchAndStoreBusStops()
}
}


suspend fun fetchAndStoreBusStops() {
val busStops = galwayBusApi.fetchBusStops()

busStops.forEach {
galwayBusQueries.insertItem(it.stop_id.toLong(), it.short_name, it.irish_short_name)
galwayBusQueries?.insertItem(it.stop_id.toLong(), it.short_name, it.irish_short_name)
}
}

@ExperimentalCoroutinesApi
suspend fun getBusStopsFlow() = galwayBusQueries.selectAll(mapper = { stop_id, short_name, irish_short_name ->
suspend fun getBusStopsFlow() = galwayBusQueries?.selectAll(mapper = { stop_id, short_name, irish_short_name ->
BusStop(stop_id.toInt(), short_name, irish_short_name)
}).asFlow().mapToList()
})?.asFlow()?.mapToList()


suspend fun getBusStops() = galwayBusQueries.selectAll(mapper = { stop_id, short_name, irish_short_name ->
BusStop(stop_id.toInt(), short_name, irish_short_name)
}).executeAsList()
suspend fun getBusStops(): List<BusStop> {
return galwayBusQueries?.let {
it.selectAll(mapper = { stop_id, short_name, irish_short_name ->
BusStop(stop_id.toInt(), short_name, irish_short_name)
}).executeAsList()
} ?: emptyList<BusStop>()
}


fun getBusStops(success: (List<BusStop>) -> Unit) {
Expand All @@ -51,8 +56,6 @@ class GalwayBusRepository {
}
}



suspend fun fetchBusRoutes(): List<BusRoute> {
val busRoutes = galwayBusApi.fetchBusRoutes()
return transformBusRouteMapToList(busRoutes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlinx.serialization.list
import kotlinx.serialization.map


class GalwayBusApi() {
class GalwayBusApi(val baseUrl: String = "https://galwaybus.herokuapp.com") {

private val busRouteMapSerializer: KSerializer<Map<String, BusRoute>> = (StringSerializer to BusRoute.serializer()).map

Expand Down Expand Up @@ -57,7 +57,8 @@ class GalwayBusApi() {
}


companion object {
private const val baseUrl = "https://galwaybus.herokuapp.com"
}
// companion object {
// //private const val baseUrl = "https://galwaybus.herokuapp.com"
// private const val baseUrl = "https://localhost:8080"
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.surrus.galwaybus.common
import com.squareup.sqldelight.drivers.ios.NativeSqliteDriver
import com.surrus.galwaybus.db.MyDatabase

actual fun createDb(): MyDatabase {
actual fun createDb(): MyDatabase? {
val driver = NativeSqliteDriver(MyDatabase.Schema, "galwaybus.db")
return MyDatabase(driver)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.surrus.galwaybus.common

import com.surrus.galwaybus.db.MyDatabase



actual fun createDb(): MyDatabase? {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.surrus.galwaybus.common

import kotlinx.coroutines.*

internal actual val ApplicationDispatcher: CoroutineDispatcher = Dispatchers.Default
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.surrus.galwaybus.common
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.surrus.galwaybus.db.MyDatabase

actual fun createDb(): MyDatabase {
actual fun createDb(): MyDatabase? {
val driver = JdbcSqliteDriver("test")
return MyDatabase(driver)
}
8 changes: 8 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
apply plugin: 'kotlin-platform-jvm'
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.appengine'
apply plugin: 'application'

appengine {
deploy {
Expand All @@ -11,16 +12,23 @@ appengine {
}


mainClassName = "io.ktor.server.netty.EngineMain"

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$Versions.kotlin"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}"


implementation "io.ktor:ktor-server-servlet:$Versions.ktor"
implementation "io.ktor:ktor-html-builder:$Versions.ktor"
implementation "io.ktor:ktor-gson:$Versions.ktor"
implementation "io.ktor:ktor-client-apache:$Versions.ktor"
implementation "io.ktor:ktor-server-netty:$Versions.ktor"

implementation Ktor.locations
implementation Ktor.freemaker


implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinxSerialization}"

implementation "com.google.cloud:google-cloud-logging-logback:0.60.0-alpha"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,68 @@
package com.surrus.galwaybus.backend



import com.surrus.galwaybus.common.model.BusStop
import com.surrus.galwaybus.common.remote.GalwayBusApi
import io.ktor.application.*
import io.ktor.features.*
import freemarker.cache.ClassTemplateLoader
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.CallLogging
import io.ktor.features.Compression
import io.ktor.features.ConditionalHeaders
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.features.PartialContent
import io.ktor.features.StatusPages
import io.ktor.freemarker.FreeMarker
import io.ktor.gson.gson
import io.ktor.html.*
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.locations.Locations
import io.ktor.response.respond
import io.ktor.routing.*
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.html.*

// Entry Point of the application as defined in resources/application.conf.
// @see https://ktor.io/servers/configuration.html#hocon-file
import io.ktor.routing.Routing
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.util.error
import kotlinx.html.body
import kotlinx.html.head
import kotlinx.html.p
import kotlinx.html.title
import org.example.kotlin.multiplatform.backend.serializable

fun Application.main() {
// This adds Date and Server headers to each response, and allows custom additional headers

install(DefaultHeaders)
// This uses use the logger to log every call (request/response)
install(CallLogging)
// install(ConditionalHeaders)
// install(PartialContent)
// install(Compression)
// //install(Locations)
// install(StatusPages) {
// exception<Throwable> { cause ->
// environment.log.error(cause)
// call.respond(HttpStatusCode.InternalServerError)
// }
// }

install(ContentNegotiation) {
//serializable { }
gson {
setPrettyPrinting()
}
}

//val busStop = BusStop(1, "name", "irish name")

routing {
// Here we use a DSL for building HTML on the route "/"
// @see https://github.com/Kotlin/kotlinx.html
get("/") {
call.respondHtml {
head {
title { +"Ktor on Google App Engine Standard" }
}
body {
p {
+"hi there! This is Ktor running on Google Appengine Standard"
}
}
}
}
get("/bus") {
install(FreeMarker) {
templateLoader = ClassTemplateLoader(javaClass.classLoader, "")
}

install(Routing) {
index()

get("/stops.json") {
val galwayBusApi = GalwayBusApi()
val busStops = galwayBusApi.fetchBusStops()
call.respond(busStops)
}
}
}

fun main() {
embeddedServer(Netty, port = 8090) { main() }.start(wait = true)
}
}
18 changes: 18 additions & 0 deletions backend/src/main/java/com/surrus/galwaybus/backend/IndexRoute.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.surrus.galwaybus.backend

import io.ktor.http.ContentType
import io.ktor.http.content.defaultResource
import io.ktor.http.content.resource
import io.ktor.http.content.static
import io.ktor.routing.Route
import io.ktor.routing.accept

fun Route.index() {
static("frontend") {
resource("web.bundle.js")
}

accept(ContentType.Text.Html) {
defaultResource("index.html")
}
}
Loading

0 comments on commit 390f1e3

Please sign in to comment.