Hello everyone :wave: I have an api-gateway writte...
# server
r
Hello everyone 👋 I have an api-gateway written in kotlin (using ktor) for my microservices app. One of my new services is a node.js app streaming audio. I'm having difficulties in building my route to this streaming endpoint. Everything I've done on my api-gateway so far was to forward the request and copy the response from the inner service to the client. But I'm not sure how to build it for this streaming endpoint. I'm a Java and JavaScript developer but I'm trying to get in touch with Kotlin, that's why there's a mix of tech stacks in my app. I'll paste my current implementation on the thread. The message I'm getting from my api-gateway is a 504 timeout error. Therefore the large timeout.
Copy code
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import io.ktor.http.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.json.JsonObject
import khttp.get as httpGet
import <http://khttp.post|khttp.post> as httpPost
import khttp.put as httpPut
import khttp.delete as httpDelete


fun Route.radioRouting() {

  val ROOT = "<http://radio:3000>"

  route("/radio") {
      get {
        val customTimeout = 60_000 // 60 seconds
        val response = httpGet(
          url = "$ROOT/radio",
          timeout = customTimeout)
        if (response.statusCode == 200) {
          val stream = response.content.inputStream()
          call.response.header(HttpHeaders.ContentType, "audio/mpeg")
          call.respondOutputStream {
            stream.copyTo(this)
          }
          stream.close()
        } else {
          call.respondText("Failed to fetch audio", status = HttpStatusCode.InternalServerError)
        }

      }
  }

}
Oh, just found https://kotlinlang.slack.com/archives/C0B8MA7FA/p1684358417477399 and will take a look at the referenced article from @eenriquelopez. But any additional ideas are still welcome 😅