From 2b51111a04a15703731ae711fa4720f917ed4666 Mon Sep 17 00:00:00 2001 From: xiaobai Date: Wed, 4 Mar 2026 17:02:54 +0800 Subject: [PATCH] feat(backend): simple product list API with Ktor --- backend/README.md | 31 ++++++++++ backend/build.gradle.kts | 28 +++++++++ backend/settings.gradle.kts | 1 + .../com/example/minishop/backend/Main.kt | 62 +++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 backend/README.md create mode 100644 backend/build.gradle.kts create mode 100644 backend/settings.gradle.kts create mode 100644 backend/src/main/kotlin/com/example/minishop/backend/Main.kt diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..3682437 --- /dev/null +++ b/backend/README.md @@ -0,0 +1,31 @@ +# kmp-mini-shopping backend + +一个超轻量 Ktor 后端,用于提供商品列表接口。 + +## 运行 + +```bash +cd backend +./gradlew run +``` + +默认监听:`http://localhost:8080` + +## 接口 + +- `GET /` 服务状态 +- `GET /api/health` 健康检查 +- `GET /api/products` 商品列表(推荐) +- `GET /products` 商品列表(兼容) + +## 返回字段 + +```json +{ + "id": "1", + "title": "KMP 入门课", + "price": 99.0, + "description": "跨平台开发基础课程", + "imageUrl": "https://picsum.photos/seed/kmp1/600/360" +} +``` diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts new file mode 100644 index 0000000..6910d41 --- /dev/null +++ b/backend/build.gradle.kts @@ -0,0 +1,28 @@ +plugins { + kotlin("jvm") version "2.2.20" + application + kotlin("plugin.serialization") version "2.2.20" +} + +group = "com.example.minishop" +version = "1.0.0" + +repositories { + mavenCentral() +} + +dependencies { + implementation("io.ktor:ktor-server-core-jvm:2.3.12") + implementation("io.ktor:ktor-server-netty-jvm:2.3.12") + implementation("io.ktor:ktor-server-content-negotiation-jvm:2.3.12") + implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:2.3.12") + implementation("ch.qos.logback:logback-classic:1.5.6") +} + +application { + mainClass.set("com.example.minishop.backend.MainKt") +} + +kotlin { + jvmToolchain(17) +} diff --git a/backend/settings.gradle.kts b/backend/settings.gradle.kts new file mode 100644 index 0000000..710edc4 --- /dev/null +++ b/backend/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "kmp-mini-shopping-backend" diff --git a/backend/src/main/kotlin/com/example/minishop/backend/Main.kt b/backend/src/main/kotlin/com/example/minishop/backend/Main.kt new file mode 100644 index 0000000..97b9e15 --- /dev/null +++ b/backend/src/main/kotlin/com/example/minishop/backend/Main.kt @@ -0,0 +1,62 @@ +package com.example.minishop.backend + +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.Application +import io.ktor.server.application.call +import io.ktor.server.engine.embeddedServer +import io.ktor.server.netty.Netty +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.response.respond +import io.ktor.server.routing.get +import io.ktor.server.routing.route +import io.ktor.server.routing.routing +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.Serializable + +@Serializable +data class Product( + val id: String, + val title: String, + val price: Double, + val description: String, + val imageUrl: String = "" +) + +fun main() { + embeddedServer(Netty, port = 8080, host = "0.0.0.0") { + module() + }.start(wait = true) +} + +fun Application.module() { + install(ContentNegotiation) { json() } + + routing { + get("/") { + call.respond(mapOf("service" to "kmp-mini-shopping-backend", "status" to "ok")) + } + + route("/api") { + get("/health") { + call.respond(HttpStatusCode.OK, mapOf("ok" to true)) + } + + get("/products") { + call.respond(products()) + } + } + + get("/products") { + call.respond(products()) + } + } +} + +private fun products(): List = listOf( + Product("1", "KMP 入门课", 99.0, "跨平台开发基础课程", "https://picsum.photos/seed/kmp1/600/360"), + Product("2", "Compose 实战课", 149.0, "Compose 多端 UI 实战", "https://picsum.photos/seed/kmp2/600/360"), + Product("3", "Kotlin 协程训练营", 129.0, "协程与 Flow 进阶", "https://picsum.photos/seed/kmp3/600/360"), + Product("4", "Clean 架构速成", 199.0, "从0构建可维护架构", "https://picsum.photos/seed/kmp4/600/360"), + Product("5", "SQLDelight 实战", 119.0, "KMP 本地数据库方案", "https://picsum.photos/seed/kmp5/600/360"), + Product("6", "Ktor API 实战", 159.0, "跨平台网络层搭建", "https://picsum.photos/seed/kmp6/600/360") +)