Add possibility for disabling endpoints
This commit is contained in:
@@ -11,7 +11,6 @@ import org.luaj.vm2.LuaClosure
|
|||||||
import org.luaj.vm2.LuaNil
|
import org.luaj.vm2.LuaNil
|
||||||
import org.luaj.vm2.LuaString
|
import org.luaj.vm2.LuaString
|
||||||
import org.luaj.vm2.LuaTable
|
import org.luaj.vm2.LuaTable
|
||||||
import java.util.concurrent.ExecutorService
|
|
||||||
|
|
||||||
class EndpointLoader(
|
class EndpointLoader(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
@@ -27,6 +26,7 @@ class EndpointLoader(
|
|||||||
it as? LuaTable
|
it as? LuaTable
|
||||||
?: throw IllegalArgumentException("Expected single table to be returned")
|
?: throw IllegalArgumentException("Expected single table to be returned")
|
||||||
}
|
}
|
||||||
|
.filter { parseEnabled(it) }
|
||||||
.map { createEndpoint(it) }
|
.map { createEndpoint(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +63,11 @@ class EndpointLoader(
|
|||||||
}
|
}
|
||||||
?: throw IllegalArgumentException("'consumer' field is required")
|
?: throw IllegalArgumentException("'consumer' field is required")
|
||||||
|
|
||||||
|
private fun parseEnabled(luaTable: LuaTable) = luaTable.get("enabled")
|
||||||
|
.takeIf { it !is LuaNil }
|
||||||
|
?.checkboolean()
|
||||||
|
?: true
|
||||||
|
|
||||||
private fun parseAccepts(luaTable: LuaTable) = luaTable.get("accepts")
|
private fun parseAccepts(luaTable: LuaTable) = luaTable.get("accepts")
|
||||||
.takeIf { it !is LuaNil }
|
.takeIf { it !is LuaNil }
|
||||||
?.let {
|
?.let {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.bartlomiejpluta.ttsserver.core.web.endpoint
|
package com.bartlomiejpluta.ttsserver.core.web.endpoint
|
||||||
|
|
||||||
import fi.iki.elonen.NanoHTTPD
|
|
||||||
import fi.iki.elonen.NanoHTTPD.IHTTPSession
|
import fi.iki.elonen.NanoHTTPD.IHTTPSession
|
||||||
import fi.iki.elonen.NanoHTTPD.Response
|
import fi.iki.elonen.NanoHTTPD.Response
|
||||||
|
|
||||||
|
|||||||
@@ -29,23 +29,21 @@ class WebServer(
|
|||||||
private val sonos: SonosQueue,
|
private val sonos: SonosQueue,
|
||||||
private val endpoints: List<Endpoint>
|
private val endpoints: List<Endpoint>
|
||||||
) : NanoHTTPD(port) {
|
) : NanoHTTPD(port) {
|
||||||
private val queuedEndpoints: List<QueuedEndpoint> = endpoints
|
private val queuedEndpoints = endpoints.mapNotNull { it as? QueuedEndpoint }
|
||||||
.map { it as? QueuedEndpoint }
|
|
||||||
.filterNotNull()
|
|
||||||
|
|
||||||
private val speakersSilenceSchedulerEnabled: Boolean
|
// private val speakersSilenceSchedulerEnabled: Boolean
|
||||||
get() = preferences.getBoolean(PreferenceKey.ENABLE_SPEAKERS_SILENCE_SCHEDULER, false)
|
// get() = preferences.getBoolean(PreferenceKey.ENABLE_SPEAKERS_SILENCE_SCHEDULER, false)
|
||||||
|
//
|
||||||
private val sonosSilenceSchedulerEnabled: Boolean
|
// private val sonosSilenceSchedulerEnabled: Boolean
|
||||||
get() = preferences.getBoolean(PreferenceKey.ENABLE_SONOS_SILENCE_SCHEDULER, false)
|
// get() = preferences.getBoolean(PreferenceKey.ENABLE_SONOS_SILENCE_SCHEDULER, false)
|
||||||
|
//
|
||||||
private val speakersSilenceSchedule: TimeRange
|
// private val speakersSilenceSchedule: TimeRange
|
||||||
get() = preferences.getString(PreferenceKey.SPEAKERS_SILENCE_SCHEDULE, "")!!
|
// get() = preferences.getString(PreferenceKey.SPEAKERS_SILENCE_SCHEDULE, "")!!
|
||||||
.let { TimeRange.parse(it) }
|
// .let { TimeRange.parse(it) }
|
||||||
|
//
|
||||||
private val sonosSilenceSchedule: TimeRange
|
// private val sonosSilenceSchedule: TimeRange
|
||||||
get() = preferences.getString(PreferenceKey.SONOS_SILENCE_SCHEDULE, "")!!
|
// get() = preferences.getString(PreferenceKey.SONOS_SILENCE_SCHEDULE, "")!!
|
||||||
.let { TimeRange.parse(it) }
|
// .let { TimeRange.parse(it) }
|
||||||
|
|
||||||
override fun serve(session: IHTTPSession?): Response {
|
override fun serve(session: IHTTPSession?): Response {
|
||||||
try {
|
try {
|
||||||
@@ -112,12 +110,12 @@ class WebServer(
|
|||||||
// return newFixedLengthResponse(OK, MIME_JSON, SUCCESS_RESPONSE)
|
// return newFixedLengthResponse(OK, MIME_JSON, SUCCESS_RESPONSE)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private fun <T> extractBody(session: IHTTPSession, provider: (String) -> T): T {
|
// private fun <T> extractBody(session: IHTTPSession, provider: (String) -> T): T {
|
||||||
return mutableMapOf<String, String>().let {
|
// return mutableMapOf<String, String>().let {
|
||||||
session.parseBody(it)
|
// session.parseBody(it)
|
||||||
provider(it["postData"] ?: "{}")
|
// provider(it["postData"] ?: "{}")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// private fun file(session: IHTTPSession, audioFormat: AudioFormat): Response {
|
// private fun file(session: IHTTPSession, audioFormat: AudioFormat): Response {
|
||||||
// if (!preferences.getBoolean(PreferenceKey.ENABLE_FILE_ENDPOINTS, true)) {
|
// if (!preferences.getBoolean(PreferenceKey.ENABLE_FILE_ENDPOINTS, true)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user