diff --git a/api/src/main/kotlin/server/Endpoints.kt b/api/src/main/kotlin/server/Endpoints.kt index 5b9cbf4..e2a65b8 100644 --- a/api/src/main/kotlin/server/Endpoints.kt +++ b/api/src/main/kotlin/server/Endpoints.kt @@ -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)) diff --git a/api/src/main/kotlin/server/Routing.kt b/api/src/main/kotlin/server/Routing.kt index 12dc2fd..f059be7 100644 --- a/api/src/main/kotlin/server/Routing.kt +++ b/api/src/main/kotlin/server/Routing.kt @@ -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 RoutingContext.runCatching(crossinline block inline fun Route.getPagedUrl( path: String, + desc: String, crossinline perPage: RoutingContext.() -> Int, crossinline mapper: (T) -> R, crossinline getSet: RoutingContext.() -> SizedIterable, ) { + 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 Route.getPagedUrl( inline fun Route.getPagedRequest( path: String, + desc: String, crossinline perPage: RoutingContext.() -> Int, crossinline mapper: (T) -> R, crossinline getSet: RoutingContext.() -> SizedIterable, ) { + 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() } } @@ -75,4 +85,21 @@ fun Application.configureFallback() { } } } +} + +object RouteCache { + @Serializable data class RouteOverview(val desc: String, val routes: List) + @Serializable data class RouteEntry(val method: String, val path: String, val desc: String) + private val cache = mutableListOf() + + fun Route.register(method: HttpMethod, path: String, desc: String) { + cache += RouteEntry(method.value, path, desc) + cache.sortWith(compareBy { it.path }.thenBy { it.method }) + } + + fun Routing.installRoot() { + get("/") { + call.respond(RouteOverview("Jay's C/C++ intrinsics API [using data from Intel]", cache)) + } + } } \ No newline at end of file