Skip to content

Instantly share code, notes, and snippets.

@laithnurie
Created November 4, 2022 07:45
Show Gist options
  • Save laithnurie/2f4d3ed3f0a8dac053961ce2899f528b to your computer and use it in GitHub Desktop.
Save laithnurie/2f4d3ed3f0a8dac053961ce2899f528b to your computer and use it in GitHub Desktop.
Local ktor server
object LocalServer {
private var server: CIOApplicationEngine? = null
fun start() {
val assets = InstrumentationRegistry.getInstrumentation().context.assets
server?.stop()
server = embeddedServer(CIO, port = 8080) {
routing {
get("api/path/here") {
call.respondText(
text = assets.toJson("json path here"),
contentType = ContentType.Application.Json,
status = HttpStatusCode.OK
)
}
}
server.start()
}
private fun AssetManager.toJson(fileName: String) = open(fileName).let {
JSONObject(it.bufferedReader().readText()).toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment