feat(backend): simple product list API with Ktor
This commit is contained in:
1 parent
4e10335748
commit
2b51111a04
4 files changed
+122
No files matched your search
@@ -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"
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
rootProject.name = "kmp-mini-shopping-backend"
|
||||||
@@ -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<Product> = 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")
|
||||||
|
)
|
||||||
Reference in new issue
Block a user