Skip to content

Commit

Permalink
No longer bundle Ktor with UWP
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 28, 2023
1 parent c2d09af commit 9748f85
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ kotlin {
}
val nonWindowsMain by creating {
dependsOn(commonMain.get())
dependencies {
implementation(libs.ktor.server.netty)
implementation(libs.ktor.server.cors)
}
}

named("jvmMain") {
Expand All @@ -34,8 +38,6 @@ kotlin {
implementation(project.dependencies.compose.materialIconsExtended)
implementation(project.dependencies.compose.material3)
implementation(libs.logback)
implementation(libs.ktor.server.netty)
implementation(libs.ktor.server.cors)
}
}
}
Expand Down
17 changes: 3 additions & 14 deletions app/desktop/src/commonMain/kotlin/AuthorizationScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,14 @@ import dev.schlaubi.tonbrett.app.api.getUrl
import dev.schlaubi.tonbrett.client.href
import dev.schlaubi.tonbrett.common.Route
import io.ktor.http.*
import kotlin.system.exitProcess

@Composable
fun AuthorizationScreen(uwp: Boolean, alreadyWaiting: Boolean, onAuth: () -> Unit) {
fun AuthorizationScreen(alreadyWaiting: Boolean, onAuth: () -> Unit) {
var waiting by remember { mutableStateOf(alreadyWaiting) }
if (waiting) {
SideEffect {
val protocol = if (uwp) {
Route.Auth.Type.PROTOCOL
} else {
Route.Auth.Type.APP
}

launchUri(href(Route.Auth(protocol), URLBuilder(getUrl())).build().toURI())
if (uwp) {
exitProcess(0)
} else {
startAuthorizationServer(onAuth)
}
launchUri(href(Route.Auth(loginType), URLBuilder(getUrl())).build().toURI())
prepareAuthentication(onAuth)
}
}
Column(
Expand Down
38 changes: 4 additions & 34 deletions app/desktop/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,16 @@ import androidx.compose.ui.window.*
import cafe.adriel.lyricist.LocalStrings
import dev.schlaubi.tonbrett.app.*
import dev.schlaubi.tonbrett.app.api.ProvideContext
import io.ktor.http.*
import io.ktor.serialization.*
import mu.KotlinLogging
import java.net.URI
import kotlin.io.path.absolutePathString
import java.awt.Window as AWTWindow

private val LOG = KotlinLogging.logger { }

fun main(args: Array<String>) {
val argsString = args.joinToString(" ")
if (argsString.startsWith("tonbrett://login")) {
try {
LOG.info { "Launched App with $argsString saving token now" }
val token = Url(argsString).parameters["token"]!!
setToken(token)
} catch (e: Exception) {
e.printStackTrace()
Thread.sleep(50000)
}
startApplication(isUwp)
} else {
main(reAuthorize = false, uwp = isUwp) { startApplication(isUwp) }
}
}
fun main(array: Array<String>) = start(array)

fun main(reAuthorize: Boolean, uwp: Boolean = false, onAuth: () -> Unit) {
val token = runCatching { getToken() }.getOrNull()
if (reAuthorize || token == null) {
startApplication(uwp, true)
} else {
startApplication(uwp)
}
}

fun startApplication(uwp: Boolean, forAuth: Boolean = false) = application {
fun startApplication(forAuth: Boolean = false) = application {
val sessionExpired = remember { mutableStateOf(false) }
var needsUpdate by remember { mutableStateOf(false) }
val exceptionHandler = ExceptionHandlerFactory {
Expand All @@ -84,14 +58,13 @@ fun startApplication(uwp: Boolean, forAuth: Boolean = false) = application {
}
}
} else {
startActualApplication(uwp, forAuth, exceptionHandler, sessionExpired)
startActualApplication(forAuth, exceptionHandler, sessionExpired)
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun ApplicationScope.startActualApplication(
uwp: Boolean,
forAuth: Boolean,
exceptionHandler: ExceptionHandlerFactory, sessionExpired: MutableState<Boolean>
) {
Expand All @@ -110,10 +83,7 @@ private fun ApplicationScope.startActualApplication(
}

if (needsAuth) {
AuthorizationScreen(uwp, !firstAuth) {
context.resetApi()
needsAuth = false
}
AuthorizationScreen(!firstAuth) { needsAuth = false }
} else {
context.resetApi()
ProvideContext(context) {
Expand Down
15 changes: 0 additions & 15 deletions app/desktop/src/commonMain/kotlin/NativeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,3 @@ package dev.schlaubi.tonbrett.app.desktop
import java.net.URI
import java.nio.file.Path

/**
* Whether the current platform is UWP.
*/
expect val isUwp: Boolean

/**
* Tries to launch the URI using the UWP `Launcher`.
*
* @param uri the [URI] to launch
*/
expect fun launchUri(uri: URI)

expect fun getToken(): String

expect fun setToken(token: String)
25 changes: 25 additions & 0 deletions app/desktop/src/commonMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@file:JvmName("Platform")
package dev.schlaubi.tonbrett.app.desktop

import dev.schlaubi.tonbrett.common.Route
import java.net.URI

expect val loginType: Route.Auth.Type

/**
* Runs any preparation steps for authentication
*/
expect fun prepareAuthentication(onAuth: () -> Unit)

expect fun start(args: Array<String>)

/**
* Tries to launch the URI using the UWP `Launcher`.
*
* @param uri the [URI] to launch
*/
expect fun launchUri(uri: URI)

expect fun getToken(): String

expect fun setToken(token: String)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun Application.authModule(onAuth: () -> Unit, scope: CoroutineScope) {
post("login") {
val token = call.parameters["token"] ?: throw BadRequestException("Missing token")

//saveConfig(Config(token))
saveConfig(Config(token))
call.respond(HttpStatusCode.Accepted)
// This stops the server, see stopServerOnCancellation above
scope.cancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package dev.schlaubi.tonbrett.app.desktop

import dev.schlaubi.tonbrett.common.Route
import java.awt.Desktop
import java.net.URI
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.div

actual val isUwp = false
actual val loginType = Route.Auth.Type.APP

actual fun start(args: Array<String>) {
val needsAuth = runCatching { getToken() }.isFailure
startApplication(needsAuth)
}

actual fun prepareAuthentication(onAuth: () -> Unit) = startAuthorizationServer(onAuth)

actual fun launchUri(uri: URI) {
val desktop = Desktop.getDesktop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@ package dev.schlaubi.tonbrett.app.desktop

import dev.schlaubi.tonbrett.app.desktop.uwp_helper.StringResult
import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.*
import dev.schlaubi.tonbrett.common.Route
import io.ktor.http.*
import mu.KotlinLogging
import java.lang.foreign.Arena
import java.lang.foreign.MemorySegment
import java.lang.foreign.SegmentAllocator
import java.net.URI
import kotlin.system.exitProcess

actual val isUwp = true
private val LOG = KotlinLogging.logger { }

actual val loginType = Route.Auth.Type.PROTOCOL

actual fun prepareAuthentication(onAuth: () -> Unit): Unit = exitProcess(0)

actual fun start(args: Array<String>) {
val argsString = args.joinToString(" ")
if (argsString.startsWith("tonbrett://login")) {
try {
LOG.info { "Launched App with $argsString saving token now" }
val token = Url(argsString).parameters["token"]!!
setToken(token)
} catch (e: Exception) {
e.printStackTrace()
Thread.sleep(50000)
}
startApplication()
} else {
val needsAuth = runCatching { getToken() }.isFailure
startApplication(needsAuth)
}
}

actual fun launchUri(uri: URI): Unit = Arena.openConfined().use { arena ->
val url = arena.allocateUtf8String(uri.toString())
Expand Down
2 changes: 1 addition & 1 deletion app/desktop/uwp_helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tasks {
rootProject.file("jextract-20/bin/jextract.bat").absolutePath
} else {
if (OSUtils.IS_WINDOWS) {
"C:\\Users\\micha\\.jdks\\jextract-20\\bin\\jextract.bat"
"jextract.bat"
} else {
"jextract"
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

allprojects {
group = "dev.schlaubi.tonbrett"
version = "1.14.25"
version = "1.14.26"

repositories {
mavenCentral()
Expand Down

0 comments on commit 9748f85

Please sign in to comment.