Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plan to support Ktor? #452

Open
wonsuc opened this issue Mar 24, 2022 · 2 comments
Open

Any plan to support Ktor? #452

wonsuc opened this issue Mar 24, 2022 · 2 comments
Labels
needs-user-input The author of an issue or PR should provide more details

Comments

@wonsuc
Copy link

wonsuc commented Mar 24, 2022

Hi, I'm writing an application with the Ktor server framework.
Do you guys have any plan to support Ktor in the future?

@pierDipi
Copy link
Member

pierDipi commented Apr 1, 2022

We don't have plans to support Ktor.

are you finding difficulties in integrating Ktor with the SDK?

@matejvasek
Copy link
Contributor

matejvasek commented May 2, 2022

@wonsuc doesn't at least generic HTTP adapter work?
Something like:

package com.example

import com.example.plugins.configureRouting
import io.cloudevents.core.message.MessageReader
import io.cloudevents.http.HttpMessageFactory
import io.cloudevents.http.impl.HttpMessageWriter
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.*
import kotlinx.coroutines.runBlocking
import java.util.function.BiConsumer


suspend fun getMessageReader(call: ApplicationCall): MessageReader {

    return HttpMessageFactory.createReader({ processHeader ->
        call.request.headers.flattenForEach { header, value -> processHeader.accept(header, value) }
    }, call.request.receiveChannel().toByteArray());
}

fun getMessageWriter(call: ApplicationCall): HttpMessageWriter {
    var contentType = ContentType.Any
    val putHeader = BiConsumer<String,String> {header, value ->
        if (header.equals("Content-Type", true)) {
            contentType = ContentType.parse(value)
        } else {
            call.response.header(header, value)
        }
    }
    return HttpMessageFactory.createWriter(putHeader) { body ->
        runBlocking {
            call.respondBytes(body, contentType, HttpStatusCode.OK)
        }
    }
}

fun main() {

    embeddedServer(Netty, port = 8080, host = "127.0.0.1") {
        configureRouting()
        routing {
            post("/") {
                val ce = getMessageReader(call).toEvent()
                println(ce)
                getMessageWriter(call).writeBinary(ce)
            }
        }
    }.start(wait = true)
}

Only problem with it is that ktor doesn't like manually setting of Content-Type header, but that ktor issue.
Another inconvenience is that our API is synchronous -- no co-routines.

@pierDipi pierDipi added the needs-user-input The author of an issue or PR should provide more details label Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-user-input The author of an issue or PR should provide more details
Projects
None yet
Development

No branches or pull requests

3 participants