From a1b08f2557f9bb3f7b801a6d052c71d0dbe90677 Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 10 Dec 2022 21:44:56 +0100 Subject: [PATCH] more docs, improved server initialization --- README.md | 35 +++++++++++++++++-- build.gradle.kts | 1 + .../superlogin/server/SuperloginServer.kt | 14 ++++---- .../kotlin/net/sergeych/WsServerKtTest.kt | 19 +++------- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 0b4dbe9..bf5a1ce 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,40 @@ It also contains useful tools for this type of application: - `AccessControlObject` to contain recoverable password-protected data with a backup `secret` word to restore access. -## Usage +## Setup + +Currently, complie it and publish into mavenLocal, and use from there. Soon will be published to some public maven repo. + +## Usage with ktor server + +Use module for initialization like this: +~~~ +fun Application.testServerModule() { + superloginServer(TestApiServer(), { TestSession() }) { + // This is a sample of your porvate API implementation: + on(api.loginName) { + println("login name called. now we have $currentLoginName : $superloginData") + currentLoginName + } + } +} +~~~ +Nothe that your test session should inhert from and implement all abstract methods of `SLServerSession` abstract class to work properly. + +Now you can just use the module in your ktor server initialization: +~~~ +embeddedServer(Netty, port = 8082, module = Application::testServerModule).start() +~~~ +Of course you can initialize superlogin server separately. In any case see autodocs for `superloginServer()` function. + +On the client side it could look like: +~~~ + val api = TestApiServer() + val slc = SuperloginClient(client) + var rt = slc.register("foo", "passwd", TestData("bar!")) +~~~ +See autodocs for `SuperloginClient` for more. -Currently, complie it and publisj into mavenLocal, and use from there. Soon will be published to some public maven repo. ## License diff --git a/build.gradle.kts b/build.gradle.kts index 843889f..003799b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -63,6 +63,7 @@ kotlin { val jvmMain by getting { dependencies { implementation("io.ktor:ktor-server-core:$ktor_version") + implementation("io.ktor:ktor-server-websockets-jvm:$ktor_version") } } val jvmTest by getting { diff --git a/src/jvmMain/kotlin/net/sergeych/superlogin/server/SuperloginServer.kt b/src/jvmMain/kotlin/net/sergeych/superlogin/server/SuperloginServer.kt index dc6d7de..a060bb2 100644 --- a/src/jvmMain/kotlin/net/sergeych/superlogin/server/SuperloginServer.kt +++ b/src/jvmMain/kotlin/net/sergeych/superlogin/server/SuperloginServer.kt @@ -21,7 +21,7 @@ fun randomACOLike(): ByteArray { * ~~~ * * // Application-specific API declaration - * class TestApiServer : CommandHost() { + * class TestApiServer : CommandHost() { * val foobar by command() * } * @@ -48,14 +48,14 @@ fun randomACOLike(): ByteArray { * @param adapterBuilder block that should implement service-specific api. * @param D application specific data (included in the session and transferred with registration * and login) - * @param T service-specific session, class implementing any session-based code specific to the + * @param S service-specific session, class implementing any session-based code specific to the * application but also implement necessary `superlogin` methods (implementing abstract * ones) that implement user login data persistence and search. */ -inline fun ,> Application.SuperloginServer( - api: CommandHost, - crossinline sessionBuilder: suspend AdapterBuilder>.()->T, - crossinline adapterBuilder: AdapterBuilder>.()->Unit +inline fun ,A: CommandHost> Application.superloginServer( + api: A, + crossinline sessionBuilder: suspend AdapterBuilder.()->S, + crossinline adapterBuilder: AdapterBuilder.()->Unit ) { parsec3TransportServer(api) { setupSuperloginServer { sessionBuilder() } @@ -67,7 +67,7 @@ inline fun ,> Application.SuperloginServer( /** * Set up a superlogin server manually, on a current adapter builder, using some session builder lambda. * Note that session must inherit [SLServerSession] and implement necessary abstract methods - * that do store and retrieve user data. Usually you can do it easier with [SuperloginServer] call. + * that do store and retrieve user data. Usually you can do it easier with [superloginServer] call. * If you want to do it manually (fr example using a custom transport), do it like this: * ``` * parsec3TransportServer(TestApiServer()) { diff --git a/src/jvmTest/kotlin/net/sergeych/WsServerKtTest.kt b/src/jvmTest/kotlin/net/sergeych/WsServerKtTest.kt index 1583738..d4e0237 100644 --- a/src/jvmTest/kotlin/net/sergeych/WsServerKtTest.kt +++ b/src/jvmTest/kotlin/net/sergeych/WsServerKtTest.kt @@ -5,13 +5,15 @@ import io.ktor.server.engine.* import io.ktor.server.netty.* import kotlinx.coroutines.runBlocking import kotlinx.serialization.Serializable -import net.sergeych.parsec3.* +import net.sergeych.parsec3.CommandHost +import net.sergeych.parsec3.Parsec3WSClient +import net.sergeych.parsec3.WithAdapter import net.sergeych.superlogin.* import net.sergeych.superlogin.client.LoginState import net.sergeych.superlogin.client.Registration import net.sergeych.superlogin.client.SuperloginClient import net.sergeych.superlogin.server.SLServerSession -import net.sergeych.superlogin.server.setupSuperloginServer +import net.sergeych.superlogin.server.superloginServer import net.sergeych.unikrypto.PublicKey import superlogin.assertThrowsAsync import kotlin.random.Random @@ -235,19 +237,8 @@ internal class WsServerKtTest { } -inline fun , H : CommandHost> Application.SuperloginServer( - api: H, - crossinline sessionBuilder: suspend AdapterBuilder.()->T, - crossinline adapterBuilder: AdapterBuilder.()->Unit -) { - parsec3TransportServer(api) { - setupSuperloginServer { sessionBuilder() } - adapterBuilder() - } -} - fun Application.testServerModule() { - SuperloginServer(TestApiServer(), { TestSession() }) { + superloginServer(TestApiServer(), { TestSession() }) { on(api.loginName) { println("login name called. now we have $currentLoginName : $superloginData") currentLoginName