From 7a100c96c91518fde17259bad789d0a35d0fac60 Mon Sep 17 00:00:00 2001 From: calboo Date: Sun, 30 Nov 2025 21:52:51 +0100 Subject: [PATCH] settzing an getting values --- .../src/main/kotlin/api/apiServ.kt | 8 +++- .../src/main/kotlin/api/plugins/Routing.kt | 12 ++++-- .../src/main/kotlin/api/routes/getValue.kt | 40 +++++++++++++++++++ .../src/main/kotlin/api/routes/helloRoute.kt | 4 ++ .../src/main/kotlin/api/routes/login.kt | 31 ++++++++++++++ .../src/main/kotlin/api/routes/setValue.kt | 37 +++++++++++++++++ .../main/kotlin/api/utils/tokenGenerator.kt | 18 +++++++++ .../src/main/kotlin/stuff/authService.kt | 30 ++++++++++++++ .../src/main/kotlin/theGoodStuff/Pool.kt | 39 ++++++++++++++++++ .../main/kotlin/theGoodStuff/PoolManager.kt | 24 +++++++++++ .../src/main/kotlin/theGoodStuff/Value.kt | 38 ++++++++++++++++++ .../main/kotlin/theGoodStuff/ValueVersion.kt | 15 +++++++ 12 files changed, 291 insertions(+), 5 deletions(-) create mode 100644 Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/api/routes/login.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/api/utils/tokenGenerator.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/stuff/authService.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/PoolManager.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Value.kt create mode 100644 Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/ValueVersion.kt diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/apiServ.kt b/Server/FastKeyValueServer/src/main/kotlin/api/apiServ.kt index 5c9e0d0..869b3f2 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/apiServ.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/apiServ.kt @@ -15,6 +15,8 @@ import io.ktor.server.websocket.pingPeriod import io.ktor.server.websocket.timeout import kotlinx.serialization.json.Json import org.calvin.erfmann.api.plugins.configureRouting +import org.calvin.erfmann.stuff.authService +import org.calvin.erfmann.theGoodStuff.PoolManager import java.time.Duration class apiServ { @@ -23,7 +25,8 @@ class apiServ { - + var authService = authService("password") + var poolManager = PoolManager() @@ -45,7 +48,8 @@ class apiServ { masking = false } configureRouting( - + authService, + poolManager ) } } \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt b/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt index 987521d..65532bc 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/plugins/Routing.kt @@ -10,13 +10,19 @@ import io.ktor.server.application.Application import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.routing.routing import kotlinx.serialization.json.Json +import org.calvin.erfmann.api.routes.getValue import org.calvin.erfmann.api.routes.helloRoutes +import org.calvin.erfmann.api.routes.login +import org.calvin.erfmann.api.routes.setValue +import org.calvin.erfmann.stuff.authService +import org.calvin.erfmann.theGoodStuff.PoolManager -fun Application.configureRouting() { - +fun Application.configureRouting(authService: authService, poolManager: PoolManager) { routing { helloRoutes() - + login(authService) + getValue(authService, poolManager) + setValue(authService, poolManager) } } diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt new file mode 100644 index 0000000..4daedea --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/getValue.kt @@ -0,0 +1,40 @@ +package org.calvin.erfmann.api.routes + +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import kotlinx.serialization.Serializable +import org.calvin.erfmann.stuff.authService +import org.calvin.erfmann.theGoodStuff.PoolManager + +@Serializable +data class GetValueRequest(val token: String, val pool: String, val key: String) + + +fun Route.getValue(authService: authService, poolManager: PoolManager) { + + get("/value") { + val request = call.receive() + + val isValid = authService.isTokenValid(request.token) + if (isValid) { + val pool = poolManager.getPool(request.pool) + if (pool != null) { + val value = pool.getValueValue(request.key) + if (value != null) { + call.respond(mapOf("value" to value)) + } else { + call.respond(mapOf("error" to "Key not found")) + } + } else { + call.respond(mapOf("error" to "Pool not found")) + } + } else { + call.respond(mapOf("error" to "Invalid token")) + } + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/helloRoute.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/helloRoute.kt index 89c7608..ddcc4ba 100644 --- a/Server/FastKeyValueServer/src/main/kotlin/api/routes/helloRoute.kt +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/helloRoute.kt @@ -5,6 +5,10 @@ import io.ktor.server.response.respond import io.ktor.server.routing.Route import io.ktor.server.routing.get import io.ktor.server.routing.route +import kotlinx.serialization.Serializable + + + fun Route.helloRoutes() { route("/hello") { diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/login.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/login.kt new file mode 100644 index 0000000..4542ab8 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/login.kt @@ -0,0 +1,31 @@ +package org.calvin.erfmann.api.routes + +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.route +import kotlinx.serialization.Serializable +import org.calvin.erfmann.stuff.authService + +@Serializable +data class LoginRequest(val password: String) + + +fun Route.login(authService: authService) { + + post("/login") { + val request = call.receive() + + val token = authService.getToken(request.password) + + if (token != null) { + call.respond(mapOf("token" to token)) + } else { + call.respond(mapOf("error" to "Invalid password")) + } + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt b/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt new file mode 100644 index 0000000..d2abab2 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/api/routes/setValue.kt @@ -0,0 +1,37 @@ +package org.calvin.erfmann.api.routes + +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.post +import kotlinx.serialization.Serializable +import org.calvin.erfmann.stuff.authService +import org.calvin.erfmann.theGoodStuff.PoolManager + + +@Serializable +data class SetValueRequest(val token: String, val pool: String, val key: String, val value: String) + + +fun Route.setValue(authService: authService, poolManager: PoolManager) { + + post("/value") { + val request = call.receive() + + val isValid = authService.isTokenValid(request.token) + + if (isValid) { + val pool = poolManager.getPool(request.pool) + if (pool != null) { + pool.setValueValue(request.key, request.value) + call.respond(mapOf("status" to "success")) + } else { + poolManager.createPool(request.pool).setValueValue(request.key, request.value) + } + } else { + call.respond(mapOf("error" to "Invalid token")) + } + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/api/utils/tokenGenerator.kt b/Server/FastKeyValueServer/src/main/kotlin/api/utils/tokenGenerator.kt new file mode 100644 index 0000000..f626292 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/api/utils/tokenGenerator.kt @@ -0,0 +1,18 @@ +package org.calvin.erfmann.api.utils + +import java.security.SecureRandom + +class tokenGenerator { + private val CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + private val secureRandom = SecureRandom() + + /** + * Generiert einen sicheren Token. + * @param length Standardmäßig 32 Zeichen, kann aber angepasst werden. + */ + fun generate(length: Int = 32): String { + return (1..length) + .map { CHARACTERS[secureRandom.nextInt(CHARACTERS.length)] } + .joinToString("") + } +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/stuff/authService.kt b/Server/FastKeyValueServer/src/main/kotlin/stuff/authService.kt new file mode 100644 index 0000000..2de93c9 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/stuff/authService.kt @@ -0,0 +1,30 @@ +package org.calvin.erfmann.stuff + +import org.calvin.erfmann.api.utils.tokenGenerator + +class authService(password: String) { + val password: String = password + + var activeTokens = mutableListOf() + + + + + + fun getToken(inputPassword: String): String?{ + if (inputPassword != password){ + return null + } + val token = tokenGenerator().generate() + activeTokens.add(token) + return token + } + + fun isTokenValid(token: String): Boolean{ + return activeTokens.contains(token) + } + + + + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt new file mode 100644 index 0000000..6210ffa --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Pool.kt @@ -0,0 +1,39 @@ +package org.calvin.erfmann.theGoodStuff + + + +class Pool(initName: String) { + + var name: String = initName + var values: MutableMap = mutableMapOf() + + fun getValueValue(key: String): String? { + val value = values[key] + return value?.getValueValue() + } + + fun setValueValue(key: String, newValue: String) { + + + val value = values[key] + if (value != null) { + value.setValueValue(newValue) + } else { + values[key] = Value(key, newValue) + } + } + + fun getVersion(key: String, version: Long): String? { + val value = values[key] + return value?.getVersion(version) + } + + fun deleteValue(key: String) { + values.remove(key) + } + + fun isKeyPresent(key: String): Boolean { + return values.containsKey(key) + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/PoolManager.kt b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/PoolManager.kt new file mode 100644 index 0000000..3612f64 --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/PoolManager.kt @@ -0,0 +1,24 @@ +package org.calvin.erfmann.theGoodStuff + +class PoolManager{ + var pools: MutableMap = mutableMapOf() + + fun getPool(poolName: String): Pool? { + return pools[poolName] + } + + fun createPool(poolName: String): Pool { + val pool = Pool(poolName) + pools[poolName] = pool + return pool + } + + fun deletePool(poolName: String) { + pools.remove(poolName) + } + + fun isPoolPresent(poolName: String): Boolean { + return pools.containsKey(poolName) + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Value.kt b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Value.kt new file mode 100644 index 0000000..ab4712b --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/Value.kt @@ -0,0 +1,38 @@ +package org.calvin.erfmann.theGoodStuff + +class Value(initKey: String, initValue: String) { + + // Das einfachste Value Element + + + var key: String = initKey + var value: String = initValue + var lastUpdated: Long = System.currentTimeMillis() + var createdAt: Long = System.currentTimeMillis() + var lastAcccessed: Long = System.currentTimeMillis() + var currentVersion: Long = 0 + var versions = mutableListOf() + + fun getValueValue(): String { + this.lastAcccessed = System.currentTimeMillis() + return this.value + } + + fun getCurrentValueVersion(): Long { + return this.currentVersion + } + + + fun setValueValue(newValue: String) { + versions.add(ValueVersion(this.value , this.currentVersion)) + this.value = newValue + this.lastUpdated = System.currentTimeMillis() + this.currentVersion += 1 + } + + fun getVersion(version: Long): String? { + val ver = versions.find { it.version == version } + return ver?.getValueValue() + } + +} \ No newline at end of file diff --git a/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/ValueVersion.kt b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/ValueVersion.kt new file mode 100644 index 0000000..7063a9c --- /dev/null +++ b/Server/FastKeyValueServer/src/main/kotlin/theGoodStuff/ValueVersion.kt @@ -0,0 +1,15 @@ +package org.calvin.erfmann.theGoodStuff + +class ValueVersion(initValue: String, initVersion: Long) { + + // Eine Version eines Values + + var value: String = initValue + var lastAcccessed: Long = 1 + var version : Long = initVersion + + fun getValueValue(): String { + this.lastAcccessed = System.currentTimeMillis() + return this.value + } +} \ No newline at end of file