com.lightningkite.lightningserver.meta.MetaEndpoints.kt Maven / Gradle / Ivy
Show all versions of server-core Show documentation
package com.lightningkite.lightningserver.meta
import com.lightningkite.lightningserver.HtmlDefaults
import com.lightningkite.lightningserver.auth.AuthInfo
import com.lightningkite.lightningserver.auth.jwt
import com.lightningkite.lightningserver.auth.rawUser
import com.lightningkite.lightningserver.client
import com.lightningkite.lightningserver.core.ContentType
import com.lightningkite.lightningserver.core.ServerPath
import com.lightningkite.lightningserver.core.ServerPathGroup
import com.lightningkite.lightningserver.db.adminIndex
import com.lightningkite.lightningserver.http.*
import com.lightningkite.lightningserver.http.HttpResponse
import com.lightningkite.lightningserver.jsonschema.lightningServerSchema
import com.lightningkite.lightningserver.jsonschema.openApiDescription
import com.lightningkite.lightningserver.metrics.Metrics
import com.lightningkite.lightningserver.routes.fullUrl
import com.lightningkite.lightningserver.schedule.Scheduler
import com.lightningkite.lightningserver.serialization.Serialization
import com.lightningkite.lightningserver.serverhealth.healthCheck
import com.lightningkite.lightningserver.settings.generalSettings
import com.lightningkite.lightningserver.tasks.Tasks
import com.lightningkite.lightningserver.typed.apiDocs
import com.lightningkite.lightningserver.websocket.WebSockets
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.html.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
class MetaEndpoints(
path: ServerPath,
val authInfo: AuthInfo,
packageName: String = "com.mypackage",
isAdmin: suspend (USER) -> Boolean,
) : ServerPathGroup(path) {
companion object {
var isAdministrator: suspend (Any?) -> Boolean = { false }
}
init {
isAdministrator = { isAdmin(authInfo.tryCast(it) as USER) }
}
val root = get.handler {
HttpResponse(body = HttpContent.Html {
head { title("${generalSettings().projectName} - Meta Information") }
body {
ul {
for (endpoint in endpoints) {
li { a(href = endpoint.path.fullUrl()) { +endpoint.path.segments.last().toString() } }
}
}
}
})
}
val docs = path("docs").apiDocs(packageName)
val health = path("health").healthCheck(authInfo, isAdmin)
val isOnline = path("online").get.handler { HttpResponse.plainText("Server is running.") }
private suspend fun openAdmin(jwt: String?): HttpResponse {
val inject = buildJsonObject {
put("url", generalSettings().publicUrl)
put("basePage", path("admin/").toString())
jwt?.let {
put("jwt", it)
}
}
val original = client.get("https://lightning-server-admin.s3.us-west-2.amazonaws.com/index.html").bodyAsText()
val page = (original.substringBeforeLast("