GET / (route overview)

This commit is contained in:
jay-tux 2025-04-25 10:32:11 +02:00
parent 2ea83ee242
commit eea2af9789
Signed by: jay-tux
GPG Key ID: 84302006B056926E
2 changed files with 37 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package com.jaytux.simd.server package com.jaytux.simd.server
import com.jaytux.simd.data.* import com.jaytux.simd.data.*
import com.jaytux.simd.server.RouteCache.register
import io.ktor.http.*
import io.ktor.http.content.* import io.ktor.http.content.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
@ -46,18 +48,18 @@ data class Versioning(
) )
fun Routing.installGetAll() { 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) Intrinsic.all().orderAsc(Intrinsics.mnemonic)
} }
getPagedUrl("/cpuid", { 100 }, { it.name }) { CPUID.all().orderAsc(CPUIDs.name) } getPagedUrl("/cpuid", "Get a list of all CPUIDs", { 100 }, { it.name }) { CPUID.all().orderAsc(CPUIDs.name) }
getPagedUrl("/tech", { 100 }, { it.name }) { Tech.all().orderAsc(Techs.name) } getPagedUrl("/tech", "Get a list of all technologies", { 100 }, { it.name }) { Tech.all().orderAsc(Techs.name) }
getPagedUrl("/category", { 100 }, { it.name }) { Category.all().orderAsc(Categories.name) } getPagedUrl("/category", "Get a list of all intrinsic categories", { 100 }, { it.name }) { Category.all().orderAsc(Categories.name) }
getPagedUrl("/types", { 100 }, { it.name }) { CppType.all().orderAsc(CppTypes.name) } getPagedUrl("/types", "Get a list of all used C/C++ types", { 100 }, { it.name }) { CppType.all().orderAsc(CppTypes.name) }
} }
fun Routing.installSearch() { 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 name = call.request.queryParameters["name"]
val returnType = call.request.queryParameters["return"]?.let { val returnType = call.request.queryParameters["return"]?.let {
CppType.find { CppTypes.name eq it }.firstOrNull() CppType.find { CppTypes.name eq it }.firstOrNull()
@ -90,6 +92,7 @@ fun Routing.installSearch() {
} }
fun Routing.installDetails() { fun Routing.installDetails() {
register(HttpMethod.Get, "/details/{id}", "Get details of an intrinsic by its ID")
get("/details/{id}") { get("/details/{id}") {
runCatching { runCatching {
transaction { transaction {
@ -136,6 +139,7 @@ fun Routing.installDetails() {
} }
fun Routing.installVersion() { fun Routing.installVersion() {
register(HttpMethod.Get, "/version", "Get the current version of the data")
get("/version") { get("/version") {
val upd = Loader.DatedVersion.fromPrefs() val upd = Loader.DatedVersion.fromPrefs()
call.respond(Versioning(upd.version.toString(), intelUpdate = upd.releaseDate, upd.updateDate)) call.respond(Versioning(upd.version.toString(), intelUpdate = upd.releaseDate, upd.updateDate))

View File

@ -1,6 +1,8 @@
package com.jaytux.simd.server package com.jaytux.simd.server
import com.jaytux.simd.data.* 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.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.plugins.autohead.* 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( inline fun <reified T, reified R> Route.getPagedUrl(
path: String, path: String,
desc: String,
crossinline perPage: RoutingContext.() -> Int, crossinline perPage: RoutingContext.() -> Int,
crossinline mapper: (T) -> R, crossinline mapper: (T) -> R,
crossinline getSet: RoutingContext.() -> SizedIterable<T>, crossinline getSet: RoutingContext.() -> SizedIterable<T>,
) { ) {
register(HttpMethod.Get, path, "${desc} (initial page)")
get(path) { get(path) {
runCatching { runCatching {
transaction { getSet().paginated(0, perPage(), mapper) } transaction { getSet().paginated(0, perPage(), mapper) }
} }
} }
register(HttpMethod.Get, "$path/{page}", "${desc} (paginated)")
get("$path/{page}") { get("$path/{page}") {
runCatching { runCatching {
val page = call.parameters["page"]?.toLongOrNull() ?: 0 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( inline fun <reified T, reified R> Route.getPagedRequest(
path: String, path: String,
desc: String,
crossinline perPage: RoutingContext.() -> Int, crossinline perPage: RoutingContext.() -> Int,
crossinline mapper: (T) -> R, crossinline mapper: (T) -> R,
crossinline getSet: RoutingContext.() -> SizedIterable<T>, crossinline getSet: RoutingContext.() -> SizedIterable<T>,
) { ) {
register(HttpMethod.Get, path, desc)
get(path) { get(path) {
runCatching { runCatching {
val page = call.request.queryParameters["page"]?.toLongOrNull() ?: 0 val page = call.request.queryParameters["page"]?.toLongOrNull() ?: 0
@ -64,6 +72,8 @@ fun Application.configureRouting() {
installSearch() installSearch()
installDetails() installDetails()
installVersion() installVersion()
installRoot()
} }
} }
@ -75,4 +85,21 @@ 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))
}
}
} }