more docs, improved server initialization

This commit is contained in:
Sergey Chernov 2022-12-10 21:44:56 +01:00
parent c5acba1511
commit a1b08f2557
4 changed files with 46 additions and 23 deletions

View File

@ -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. - `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>(), { 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<WithAdapter>()
val slc = SuperloginClient<TestData, S1>(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 ## License

View File

@ -63,6 +63,7 @@ kotlin {
val jvmMain by getting { val jvmMain by getting {
dependencies { dependencies {
implementation("io.ktor:ktor-server-core:$ktor_version") implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-websockets-jvm:$ktor_version")
} }
} }
val jvmTest by getting { val jvmTest by getting {

View File

@ -21,7 +21,7 @@ fun randomACOLike(): ByteArray {
* ~~~ * ~~~
* *
* // Application-specific API declaration * // Application-specific API declaration
* class TestApiServer<T : WithAdapter> : CommandHost<T>() { * class TestApiServer<S : WithAdapter> : CommandHost<S>() {
* val foobar by command<Unit, String?>() * val foobar by command<Unit, String?>()
* } * }
* *
@ -48,14 +48,14 @@ fun randomACOLike(): ByteArray {
* @param adapterBuilder block that should implement service-specific api. * @param adapterBuilder block that should implement service-specific api.
* @param D application specific data (included in the session and transferred with registration * @param D application specific data (included in the session and transferred with registration
* and login) * 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 * application but also implement necessary `superlogin` methods (implementing abstract
* ones) that implement user login data persistence and search. * ones) that implement user login data persistence and search.
*/ */
inline fun <reified D, T : SLServerSession<D>,> Application.SuperloginServer( inline fun <reified D, S : SLServerSession<D>,A: CommandHost<S>> Application.superloginServer(
api: CommandHost<T>, api: A,
crossinline sessionBuilder: suspend AdapterBuilder<T, CommandHost<T>>.()->T, crossinline sessionBuilder: suspend AdapterBuilder<S, A>.()->S,
crossinline adapterBuilder: AdapterBuilder<T, CommandHost<T>>.()->Unit crossinline adapterBuilder: AdapterBuilder<S, A>.()->Unit
) { ) {
parsec3TransportServer(api) { parsec3TransportServer(api) {
setupSuperloginServer { sessionBuilder() } setupSuperloginServer { sessionBuilder() }
@ -67,7 +67,7 @@ inline fun <reified D, T : SLServerSession<D>,> Application.SuperloginServer(
/** /**
* Set up a superlogin server manually, on a current adapter builder, using some session builder lambda. * 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 * 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: * If you want to do it manually (fr example using a custom transport), do it like this:
* ``` * ```
* parsec3TransportServer(TestApiServer<TestSession>()) { * parsec3TransportServer(TestApiServer<TestSession>()) {

View File

@ -5,13 +5,15 @@ import io.ktor.server.engine.*
import io.ktor.server.netty.* import io.ktor.server.netty.*
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable 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.*
import net.sergeych.superlogin.client.LoginState import net.sergeych.superlogin.client.LoginState
import net.sergeych.superlogin.client.Registration import net.sergeych.superlogin.client.Registration
import net.sergeych.superlogin.client.SuperloginClient import net.sergeych.superlogin.client.SuperloginClient
import net.sergeych.superlogin.server.SLServerSession import net.sergeych.superlogin.server.SLServerSession
import net.sergeych.superlogin.server.setupSuperloginServer import net.sergeych.superlogin.server.superloginServer
import net.sergeych.unikrypto.PublicKey import net.sergeych.unikrypto.PublicKey
import superlogin.assertThrowsAsync import superlogin.assertThrowsAsync
import kotlin.random.Random import kotlin.random.Random
@ -235,19 +237,8 @@ internal class WsServerKtTest {
} }
inline fun <reified D, T : SLServerSession<D>, H : CommandHost<T>> Application.SuperloginServer(
api: H,
crossinline sessionBuilder: suspend AdapterBuilder<T, H>.()->T,
crossinline adapterBuilder: AdapterBuilder<T, H>.()->Unit
) {
parsec3TransportServer(api) {
setupSuperloginServer { sessionBuilder() }
adapterBuilder()
}
}
fun Application.testServerModule() { fun Application.testServerModule() {
SuperloginServer(TestApiServer<TestSession>(), { TestSession() }) { superloginServer(TestApiServer<TestSession>(), { TestSession() }) {
on(api.loginName) { on(api.loginName) {
println("login name called. now we have $currentLoginName : $superloginData") println("login name called. now we have $currentLoginName : $superloginData")
currentLoginName currentLoginName