GET / (route overview)
This commit is contained in:
parent
2ea83ee242
commit
eea2af9789
|
@ -1,6 +1,8 @@
|
|||
package com.jaytux.simd.server
|
||||
|
||||
import com.jaytux.simd.data.*
|
||||
import com.jaytux.simd.server.RouteCache.register
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.content.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
@ -46,18 +48,18 @@ data class Versioning(
|
|||
)
|
||||
|
||||
fun Routing.installGetAll() {
|
||||
getPagedUrl("/all", { 100 }, { IntrinsicSummary(it.id.value, it.mnemonic) }) {
|
||||
getPagedUrl("/all", "Get a quick overview of all endpoints", { 100 }, { IntrinsicSummary(it.id.value, it.mnemonic) }) {
|
||||
Intrinsic.all().orderAsc(Intrinsics.mnemonic)
|
||||
}
|
||||
|
||||
getPagedUrl("/cpuid", { 100 }, { it.name }) { CPUID.all().orderAsc(CPUIDs.name) }
|
||||
getPagedUrl("/tech", { 100 }, { it.name }) { Tech.all().orderAsc(Techs.name) }
|
||||
getPagedUrl("/category", { 100 }, { it.name }) { Category.all().orderAsc(Categories.name) }
|
||||
getPagedUrl("/types", { 100 }, { it.name }) { CppType.all().orderAsc(CppTypes.name) }
|
||||
getPagedUrl("/cpuid", "Get a list of all CPUIDs", { 100 }, { it.name }) { CPUID.all().orderAsc(CPUIDs.name) }
|
||||
getPagedUrl("/tech", "Get a list of all technologies", { 100 }, { it.name }) { Tech.all().orderAsc(Techs.name) }
|
||||
getPagedUrl("/category", "Get a list of all intrinsic categories", { 100 }, { it.name }) { Category.all().orderAsc(Categories.name) }
|
||||
getPagedUrl("/types", "Get a list of all used C/C++ types", { 100 }, { it.name }) { CppType.all().orderAsc(CppTypes.name) }
|
||||
}
|
||||
|
||||
fun Routing.installSearch() {
|
||||
getPagedRequest("/search", { 100 }, { IntrinsicSummary(it[Intrinsics.id].value, it[Intrinsics.mnemonic]) }) {
|
||||
getPagedRequest("/search", "Search for intrinsics matching certain filters", { 100 }, { IntrinsicSummary(it[Intrinsics.id].value, it[Intrinsics.mnemonic]) }) {
|
||||
val name = call.request.queryParameters["name"]
|
||||
val returnType = call.request.queryParameters["return"]?.let {
|
||||
CppType.find { CppTypes.name eq it }.firstOrNull()
|
||||
|
@ -90,6 +92,7 @@ fun Routing.installSearch() {
|
|||
}
|
||||
|
||||
fun Routing.installDetails() {
|
||||
register(HttpMethod.Get, "/details/{id}", "Get details of an intrinsic by its ID")
|
||||
get("/details/{id}") {
|
||||
runCatching {
|
||||
transaction {
|
||||
|
@ -136,6 +139,7 @@ fun Routing.installDetails() {
|
|||
}
|
||||
|
||||
fun Routing.installVersion() {
|
||||
register(HttpMethod.Get, "/version", "Get the current version of the data")
|
||||
get("/version") {
|
||||
val upd = Loader.DatedVersion.fromPrefs()
|
||||
call.respond(Versioning(upd.version.toString(), intelUpdate = upd.releaseDate, upd.updateDate))
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.jaytux.simd.server
|
||||
|
||||
import com.jaytux.simd.data.*
|
||||
import com.jaytux.simd.server.RouteCache.installRoot
|
||||
import com.jaytux.simd.server.RouteCache.register
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.autohead.*
|
||||
|
@ -26,15 +28,19 @@ inline suspend fun <reified R: Any> RoutingContext.runCatching(crossinline block
|
|||
|
||||
inline fun <reified T, reified R> Route.getPagedUrl(
|
||||
path: String,
|
||||
desc: String,
|
||||
crossinline perPage: RoutingContext.() -> Int,
|
||||
crossinline mapper: (T) -> R,
|
||||
crossinline getSet: RoutingContext.() -> SizedIterable<T>,
|
||||
) {
|
||||
register(HttpMethod.Get, path, "${desc} (initial page)")
|
||||
get(path) {
|
||||
runCatching {
|
||||
transaction { getSet().paginated(0, perPage(), mapper) }
|
||||
}
|
||||
}
|
||||
|
||||
register(HttpMethod.Get, "$path/{page}", "${desc} (paginated)")
|
||||
get("$path/{page}") {
|
||||
runCatching {
|
||||
val page = call.parameters["page"]?.toLongOrNull() ?: 0
|
||||
|
@ -45,10 +51,12 @@ inline fun <reified T, reified R> Route.getPagedUrl(
|
|||
|
||||
inline fun <reified T, reified R> Route.getPagedRequest(
|
||||
path: String,
|
||||
desc: String,
|
||||
crossinline perPage: RoutingContext.() -> Int,
|
||||
crossinline mapper: (T) -> R,
|
||||
crossinline getSet: RoutingContext.() -> SizedIterable<T>,
|
||||
) {
|
||||
register(HttpMethod.Get, path, desc)
|
||||
get(path) {
|
||||
runCatching {
|
||||
val page = call.request.queryParameters["page"]?.toLongOrNull() ?: 0
|
||||
|
@ -64,6 +72,8 @@ fun Application.configureRouting() {
|
|||
installSearch()
|
||||
installDetails()
|
||||
installVersion()
|
||||
|
||||
installRoot()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,3 +86,20 @@ fun Application.configureFallback() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
object RouteCache {
|
||||
@Serializable data class RouteOverview(val desc: String, val routes: List<RouteEntry>)
|
||||
@Serializable data class RouteEntry(val method: String, val path: String, val desc: String)
|
||||
private val cache = mutableListOf<RouteEntry>()
|
||||
|
||||
fun Route.register(method: HttpMethod, path: String, desc: String) {
|
||||
cache += RouteEntry(method.value, path, desc)
|
||||
cache.sortWith(compareBy<RouteEntry> { it.path }.thenBy { it.method })
|
||||
}
|
||||
|
||||
fun Routing.installRoot() {
|
||||
get("/") {
|
||||
call.respond(RouteOverview("Jay's C/C++ intrinsics API [using data from Intel]", cache))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue