add basic updater implementation in gigawear
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
/build
|
||||
/bin
|
||||
@@ -0,0 +1,14 @@
|
||||
plugins {
|
||||
alias(libs.plugins.convention.jvm)
|
||||
alias(libs.plugins.convention.ktor.client)
|
||||
alias(libs.plugins.convention.koin)
|
||||
alias(libs.plugins.convention.serialization)
|
||||
alias(libs.plugins.ktor)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.ktor.server.core.jvm)
|
||||
implementation(project(":libs:backend-base"))
|
||||
implementation(project(":libs:http-client"))
|
||||
implementation(project(":feature:updater:updater-common"))
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.shadowsparky.updater.backend
|
||||
|
||||
import org.koin.core.annotation.ComponentScan
|
||||
import org.koin.core.annotation.Configuration
|
||||
import org.koin.core.annotation.Module
|
||||
|
||||
@Module
|
||||
@ComponentScan
|
||||
@Configuration
|
||||
class BackendUpdaterModule
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package ru.shadowsparky.updater.backend.data
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.util.cio.readChannel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.backend.data.EnvFetcher
|
||||
import ru.shadowsparky.http.domain.HttpException
|
||||
import ru.shadowsparky.updater.domain.UpdateFetcher
|
||||
import ru.shadowsparky.updater.domain.UpdateInfo
|
||||
import java.io.File
|
||||
|
||||
@Factory
|
||||
class BackendUpdateFetcherFactory(
|
||||
private val envFetcher: EnvFetcher,
|
||||
private val json: Json
|
||||
) {
|
||||
fun create(userId: Long): BackendUpdateFetcher {
|
||||
return BackendUpdateFetcher(envFetcher, json, userId)
|
||||
}
|
||||
}
|
||||
|
||||
class BackendUpdateFetcher(
|
||||
private val envFetcher: EnvFetcher,
|
||||
private val json: Json,
|
||||
private val userId: Long
|
||||
) : UpdateFetcher {
|
||||
private val fetchMutex = Mutex()
|
||||
private val updateFileMutex = Mutex()
|
||||
|
||||
override suspend fun fetch(versionCode: Long): UpdateInfo {
|
||||
updateInfo?.let { return it }
|
||||
fetchMutex.withLock {
|
||||
updateInfo?.let { return it }
|
||||
return fetchInternal().apply { updateInfo = this }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun fetchInternal(): UpdateInfo = withContext(Dispatchers.IO) {
|
||||
val configPath = envFetcher.get("UPDATE_CONFIG")
|
||||
if (configPath.isBlank()) {
|
||||
UpdateInfo()
|
||||
} else {
|
||||
json.decodeFromString<UpdateInfo>(File(configPath).readText())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateFile(): File {
|
||||
updateFile?.let { return it }
|
||||
updateFileMutex.withLock {
|
||||
updateFile?.let { return it }
|
||||
return updateFileInternal().apply { updateFile = this }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateFileInternal(): File = withContext(Dispatchers.IO) {
|
||||
val updateFilePath = envFetcher.get("UPDATE_FILE")
|
||||
if (updateFilePath.isBlank()) throw HttpException(HttpStatusCode.NotFound.value, "Update not found")
|
||||
val file = File(updateFilePath)
|
||||
file.readChannel()
|
||||
if (!file.exists()) throw HttpException(HttpStatusCode.NotFound.value, "Update not exists")
|
||||
file
|
||||
}
|
||||
|
||||
private companion object {
|
||||
var updateInfo: UpdateInfo? = null
|
||||
var updateFile: File? = null
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.shadowsparky.updater.backend.presentation
|
||||
|
||||
import io.ktor.server.application.ApplicationCall
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.response.respondFile
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.util.getOrFail
|
||||
import ru.shadowsparky.backend.presentation.obtainUserId
|
||||
import ru.shadowsparky.updater.backend.data.BackendUpdateFetcherFactory
|
||||
import ru.shadowsparky.updater.domain.UpdateFetcher
|
||||
|
||||
fun Route.setupUpdates(
|
||||
updateFetcherFactory: BackendUpdateFetcherFactory,
|
||||
prefix: String = "",
|
||||
onUserId: suspend ApplicationCall.() -> Long = { obtainUserId() }
|
||||
) {
|
||||
get("${prefix}checkUpdates") {
|
||||
val versionCode = call.parameters.getOrFail(UpdateFetcher.ARG_VERSION_CODE).toLong()
|
||||
val updateFetcher = updateFetcherFactory.create(onUserId(call))
|
||||
call.respond(updateFetcher.fetch(versionCode))
|
||||
}
|
||||
get("${prefix}getApk") {
|
||||
val updateFetcher = updateFetcherFactory.create(onUserId(call))
|
||||
call.respondFile(updateFetcher.updateFile())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user