kotlin-spring-boot by ashchupliak/dream-team
npx skills add https://github.com/ashchupliak/dream-team --skill kotlin-spring-boot// build.gradle.kts
plugins {
kotlin("jvm") version "2.2.21"
kotlin("plugin.spring") version "2.2.21"
id("org.springframework.boot") version "3.5.7"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}
data class Environment(
val id: UUID,
val name: String,
val status: EnvironmentStatus,
val createdAt: Instant,
val updatedAt: Instant?
)
enum class EnvironmentStatus {
PENDING, RUNNING, STOPPED, FAILED
}
@Service
class EnvironmentService(
private val repository: EnvironmentRepository,
private val computeClient: ComputeClient
) {
// 使用 NEVER 传播 - 让调用者控制事务
@Transactional(propagation = Propagation.NEVER)
fun create(request: CreateEnvironmentRequest): Pair<EnvironmentResponse, Boolean> {
// 检查是否已存在(幂等性)
repository.findByName(request.name)?.let {
return Pair(it.toResponse(), false) // 已存在
}
// 创建新的
val environment = Environment(
id = UUID.randomUUID(),
name = request.name,
status = EnvironmentStatus.PENDING,
createdAt = Instant.now(),
updatedAt = null
)
val saved = repository.save(environment)
return Pair(saved.toResponse(), true) // 已创建
}
fun findById(id: UUID): Environment =
repository.findById(id)
?: throw ResourceNotFoundRestException("Environment", id)
fun findAll(): List<Environment> =
repository.findAll()
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@RestController
class EnvironmentController(
private val service: EnvironmentService
) : EnvironmentApi {
override fun create(request: CreateEnvironmentRequest): ResponseEntity<EnvironmentResponse> {
val (result, isNew) = service.create(request)
return if (isNew) {
ResponseEntity.status(HttpStatus.CREATED).body(result)
} else {
ResponseEntity.ok(result)
}
}
override fun getById(id: UUID): ResponseEntity<EnvironmentResponse> =
ResponseEntity.ok(service.findById(id).toResponse())
override fun list(): ResponseEntity<List<EnvironmentResponse>> =
ResponseEntity.ok(service.findAll().map { it.toResponse() })
}
@Tag(name = "Environments", description = "环境管理")
interface EnvironmentApi {
@Operation(summary = "创建环境")
@ApiResponses(
ApiResponse(responseCode = "201", description = "已创建"),
ApiResponse(responseCode = "200", description = "已存在"),
ApiResponse(responseCode = "400", description = "验证错误")
)
@PostMapping("/api/v1/environments")
fun create(
@RequestBody @Valid request: CreateEnvironmentRequest
): ResponseEntity<EnvironmentResponse>
@Operation(summary = "通过 ID 获取环境")
@GetMapping("/api/v1/environments/{id}")
fun getById(@PathVariable id: UUID): ResponseEntity<EnvironmentResponse>
@Operation(summary = "列出所有环境")
@GetMapping("/api/v1/environments")
fun list(): ResponseEntity<List<EnvironmentResponse>>
}
data class CreateEnvironmentRequest(
@field:NotBlank(message = "名称是必需的")
@field:Size(max = 100, message = "名称必须 <= 100 个字符")
val name: String,
@field:Size(max = 500)
val description: String? = null
)
data class EnvironmentResponse(
val id: UUID,
val name: String,
val status: String,
val createdAt: Instant
)
// 用于映射的扩展函数
fun Environment.toResponse() = EnvironmentResponse(
id = id,
name = name,
status = status.name,
createdAt = createdAt
)
// 类型化异常
throw ResourceNotFoundRestException("Environment", id)
throw ValidationRestException("名称不能为空")
throw ConflictRestException("环境已存在")
// 全局处理器
@RestControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundRestException::class)
fun handleNotFound(ex: ResourceNotFoundRestException): ResponseEntity<ErrorResponse> =
ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ErrorResponse(ex.message ?: "未找到"))
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidation(ex: MethodArgumentNotValidException): ResponseEntity<ErrorResponse> {
val errors = ex.bindingResult.fieldErrors.map { "${it.field}: ${it.defaultMessage}" }
return ResponseEntity.badRequest()
.body(ErrorResponse("验证失败", errors))
}
}
// 使用 ?.let 进行可选操作
user?.let { repository.save(it) }
// 使用 when 进行详尽匹配
when (status) {
EnvironmentStatus.PENDING -> startEnvironment()
EnvironmentStatus.RUNNING -> return // 已在运行
EnvironmentStatus.STOPPED -> restartEnvironment()
EnvironmentStatus.FAILED -> throw IllegalStateException("无法启动失败的环境")
}
// 避免 !! - 使用替代方案
repository.findById(id).single() // 如果不是恰好一个则抛出异常
repository.findById(id).firstOrNull() // 如果没有则返回 null
// 使用数据类 copy 进行不可变更新
val updated = environment.copy(
status = EnvironmentStatus.RUNNING,
updatedAt = Instant.now()
)
@ConfigurationProperties(prefix = "orca")
data class OrcaProperties(
val compute: ComputeProperties,
val timeouts: TimeoutProperties
) {
data class ComputeProperties(
val url: String,
val timeout: Duration = Duration.ofSeconds(30)
)
data class TimeoutProperties(
val creation: Duration = Duration.ofMinutes(5),
val termination: Duration = Duration.ofMinutes(2)
)
}
每周安装量
106
代码仓库
GitHub 星标数
4
首次出现
2026年1月20日
安全审计
安装于
opencode88
codex81
gemini-cli79
github-copilot72
claude-code63
cursor62
// build.gradle.kts
plugins {
kotlin("jvm") version "2.2.21"
kotlin("plugin.spring") version "2.2.21"
id("org.springframework.boot") version "3.5.7"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}
data class Environment(
val id: UUID,
val name: String,
val status: EnvironmentStatus,
val createdAt: Instant,
val updatedAt: Instant?
)
enum class EnvironmentStatus {
PENDING, RUNNING, STOPPED, FAILED
}
@Service
class EnvironmentService(
private val repository: EnvironmentRepository,
private val computeClient: ComputeClient
) {
// Use NEVER propagation - let caller control transaction
@Transactional(propagation = Propagation.NEVER)
fun create(request: CreateEnvironmentRequest): Pair<EnvironmentResponse, Boolean> {
// Check for existing (idempotency)
repository.findByName(request.name)?.let {
return Pair(it.toResponse(), false) // existing
}
// Create new
val environment = Environment(
id = UUID.randomUUID(),
name = request.name,
status = EnvironmentStatus.PENDING,
createdAt = Instant.now(),
updatedAt = null
)
val saved = repository.save(environment)
return Pair(saved.toResponse(), true) // created
}
fun findById(id: UUID): Environment =
repository.findById(id)
?: throw ResourceNotFoundRestException("Environment", id)
fun findAll(): List<Environment> =
repository.findAll()
}
@RestController
class EnvironmentController(
private val service: EnvironmentService
) : EnvironmentApi {
override fun create(request: CreateEnvironmentRequest): ResponseEntity<EnvironmentResponse> {
val (result, isNew) = service.create(request)
return if (isNew) {
ResponseEntity.status(HttpStatus.CREATED).body(result)
} else {
ResponseEntity.ok(result)
}
}
override fun getById(id: UUID): ResponseEntity<EnvironmentResponse> =
ResponseEntity.ok(service.findById(id).toResponse())
override fun list(): ResponseEntity<List<EnvironmentResponse>> =
ResponseEntity.ok(service.findAll().map { it.toResponse() })
}
@Tag(name = "Environments", description = "Environment management")
interface EnvironmentApi {
@Operation(summary = "Create environment")
@ApiResponses(
ApiResponse(responseCode = "201", description = "Created"),
ApiResponse(responseCode = "200", description = "Already exists"),
ApiResponse(responseCode = "400", description = "Validation error")
)
@PostMapping("/api/v1/environments")
fun create(
@RequestBody @Valid request: CreateEnvironmentRequest
): ResponseEntity<EnvironmentResponse>
@Operation(summary = "Get environment by ID")
@GetMapping("/api/v1/environments/{id}")
fun getById(@PathVariable id: UUID): ResponseEntity<EnvironmentResponse>
@Operation(summary = "List all environments")
@GetMapping("/api/v1/environments")
fun list(): ResponseEntity<List<EnvironmentResponse>>
}
data class CreateEnvironmentRequest(
@field:NotBlank(message = "Name is required")
@field:Size(max = 100, message = "Name must be <= 100 chars")
val name: String,
@field:Size(max = 500)
val description: String? = null
)
data class EnvironmentResponse(
val id: UUID,
val name: String,
val status: String,
val createdAt: Instant
)
// Extension function for mapping
fun Environment.toResponse() = EnvironmentResponse(
id = id,
name = name,
status = status.name,
createdAt = createdAt
)
// Typed exceptions
throw ResourceNotFoundRestException("Environment", id)
throw ValidationRestException("Name cannot be empty")
throw ConflictRestException("Environment already exists")
// Global handler
@RestControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundRestException::class)
fun handleNotFound(ex: ResourceNotFoundRestException): ResponseEntity<ErrorResponse> =
ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ErrorResponse(ex.message ?: "Not found"))
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidation(ex: MethodArgumentNotValidException): ResponseEntity<ErrorResponse> {
val errors = ex.bindingResult.fieldErrors.map { "${it.field}: ${it.defaultMessage}" }
return ResponseEntity.badRequest()
.body(ErrorResponse("Validation failed", errors))
}
}
// Use ?.let for optional operations
user?.let { repository.save(it) }
// Use when for exhaustive matching
when (status) {
EnvironmentStatus.PENDING -> startEnvironment()
EnvironmentStatus.RUNNING -> return // already running
EnvironmentStatus.STOPPED -> restartEnvironment()
EnvironmentStatus.FAILED -> throw IllegalStateException("Cannot start failed env")
}
// Avoid !! - use alternatives
repository.findById(id).single() // throws if not exactly one
repository.findById(id).firstOrNull() // returns null if none
// Data class copy for immutable updates
val updated = environment.copy(
status = EnvironmentStatus.RUNNING,
updatedAt = Instant.now()
)
@ConfigurationProperties(prefix = "orca")
data class OrcaProperties(
val compute: ComputeProperties,
val timeouts: TimeoutProperties
) {
data class ComputeProperties(
val url: String,
val timeout: Duration = Duration.ofSeconds(30)
)
data class TimeoutProperties(
val creation: Duration = Duration.ofMinutes(5),
val termination: Duration = Duration.ofMinutes(2)
)
}
Weekly Installs
106
Repository
GitHub Stars
4
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode88
codex81
gemini-cli79
github-copilot72
claude-code63
cursor62
Lark CLI IM 即时消息管理工具:机器人/用户身份操作聊天、消息、文件下载
32,700 周安装