1
0
Fork 0
keylight-emulator/src/main/kotlin/fi/schro/routing/Routing.kt

48 lines
1.6 KiB
Kotlin

package fi.schro.routing
import fi.schro.data.AccessoryInfo
import fi.schro.data.AccessoryInfoRepository
import fi.schro.data.LightRepository
import fi.schro.data.LightStatus
import io.ktor.server.routing.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
const val PATH_SEPARATOR = "/"
const val DEFAULT_PATH = "elgato"
const val LIGHT_PATH = "lights"
const val ACCESSORY_INFO_PATH = "accessory-info"
val LIGHT_ENDPOINT = listOf(DEFAULT_PATH, LIGHT_PATH)
val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH)
class Routing(
private val lightRepository: LightRepository,
private val accessoryInfoRepository: AccessoryInfoRepository
){
fun configureRouting(application: Application) {
application.routing {
route(createPath(LIGHT_ENDPOINT)) {
get {
call.respond(lightRepository.getLightStatus())
}
put<LightStatus> { statusChange ->
call.respond(lightRepository.updateLightStatus(statusChange))
}
}
route(createPath(ACCESSORY_INFO_ENDPOINT)){
get {
call.respond(accessoryInfoRepository.getAccessoryInfo())
}
put<AccessoryInfo>{ infoChange ->
call.respond(accessoryInfoRepository.updateAccessoryInfo(infoChange))
}
}
}
}
private fun createPath(pathElements: List<String>): String {
return pathElements.joinToString(separator = PATH_SEPARATOR, prefix = PATH_SEPARATOR)
}
}