Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
How to make request-bound data globally available in Ktor?

I'm building a microservice in Ktor. While we are working within the application module, Ktor provides access to the call object which contains authorization data.

My problem is that I need to access the call object from a service-level class. In Spring, you would do this by accessing the SecurityContext which is globally available via a ThreadLocal. Ktor, being coroutine-driven, does not have that option.

Do I really need to pass down the call object through my service layer methods, or is there a way in Ktor to have some sort of "call context" object that you can access from anywhere?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think there is no build-in official opportunity at the moment.

I have written a Ktor-Feature, that implements that behaviour. The answer is to complicated to describe it in a few words.

Check out the code + samples, if you are interested. Also an install guide is provided.

=> https://github.com/MaaxGr/ktor-globalcalldata


Here a short snippet, what my dsl looks like:

Call suspending function test() directly or indirectly from a route:

routing {
    get("/test") {
        test()
        call.respond("OK")
    }
}

Access call object via callData().call

suspend fun test() {
    val url = callData().call.request.uri
    println(url) // prints "/test"
}

The library also allows to add custom properties that can be bound to the current coroutine/call.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda