Skip to content

Commit 7d3b630

Browse files
luigi617luigi
andauthored
Service sdk routing (#1318)
* service init, code to separate client and service * server can be ran * application in build.gradle.kts * detele non-finished code * ktlintformat * change style * ktlintformat * add abstraction for service * minor fix * minor fix * cbor serde * fix * formatting * comment * fix * fix * generate routing based on smithy file * ktlint format * service framework abstraction * ktlint foramt * logging * ktlint format * add ContentTypeGuard * ktlint * fix content type guard * ktlint * move some parameters, e.g. engine, port, log level to be chosen in runtime rather than compile * ktlint * fix * fix logging * fix * fix * ktlint --------- Co-authored-by: luigi <[email protected]>
1 parent 5c86ab1 commit 7d3b630

File tree

6 files changed

+387
-92
lines changed

6 files changed

+387
-92
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Unit>() {
153153
}
154154

155155
if (generateServiceProject) {
156-
val serviceStubGenerator = ServiceStubGenerator(baseGenerationContext, writers)
156+
val serviceStubGenerator = ServiceStubGenerator(baseGenerationContext, writers, fileManifest)
157157
serviceStubGenerator.render()
158158
}
159159

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import software.amazon.smithy.aws.traits.protocols.RestJson1Trait
1313
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait
1414
import software.amazon.smithy.codegen.core.CodegenException
1515
import software.amazon.smithy.kotlin.codegen.lang.isValidPackageName
16-
import software.amazon.smithy.kotlin.codegen.service.ServiceEngine
1716
import software.amazon.smithy.kotlin.codegen.service.ServiceFramework
1817
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
1918
import software.amazon.smithy.kotlin.codegen.utils.toCamelCase
@@ -349,30 +348,20 @@ data class ApiSettings(
349348
/**
350349
* Contains configurations settings for a Kotlin service project
351350
* @param framework Enum representing the server framework of the service.
352-
* @param engine Enum representing the server engine of the service.
353-
* @param port Int representing the port which the service will be exposed.
354351
*/
355352
data class ServiceStubSettings(
356353
val framework: ServiceFramework = ServiceFramework.KTOR,
357-
val engine: ServiceEngine = ServiceEngine.NETTY,
358-
val port: Int = 8080,
359354
) {
360355
companion object {
361356
const val SERVER_FRAMEWORK = "serverFramework"
362-
const val SERVER_ENGINE = "serverEngine"
363-
const val PORT = "port"
364357

365358
fun fromNode(node: Optional<ObjectNode>): ServiceStubSettings = node.map {
366359
val serverFramework = node.get()
367360
.getStringMember(SERVER_FRAMEWORK)
368361
.map { ServiceFramework.fromValue(it.value) }
369362
.getOrNull() ?: ServiceFramework.KTOR
370-
val serverEngine = node.get()
371-
.getStringMember(SERVER_ENGINE)
372-
.map { ServiceEngine.fromValue(it.value) }
373-
.getOrNull() ?: ServiceEngine.NETTY
374-
val port = node.get().getNumberMemberOrDefault(PORT, 8080).toInt()
375-
ServiceStubSettings(serverFramework, serverEngine, port)
363+
364+
ServiceStubSettings(serverFramework)
376365
}.orElse(Default)
377366

378367
/**

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinDependency.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ data class KotlinDependency(
143143
val KTOR_SERVER_CORE = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server", "io.ktor", "ktor-server-core", KTOR_VERSION)
144144
val KTOR_SERVER_NETTY = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server.netty", "io.ktor", "ktor-server-netty", KTOR_VERSION)
145145
val KTOR_SERVER_HTTP = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.http", "io.ktor", "ktor-http-jvm", KTOR_VERSION)
146-
val KTOR_LOGGING_BACKEND = KotlinDependency(GradleConfiguration.Implementation, "ch.qos.logback", "ch.qos.logback", "logback-classic", KTOR_LOGGING_BACKEND_VERSION)
146+
val KTOR_SERVER_LOGGING = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server.plugins.calllogging", "io.ktor", "ktor-server-call-logging", KTOR_VERSION)
147+
val KTOR_LOGGING_SLF4J = KotlinDependency(GradleConfiguration.Implementation, "org.slf4j", "ch.qos.logback", "logback-classic", KTOR_LOGGING_BACKEND_VERSION)
148+
val KTOR_LOGGING_LOGBACK = KotlinDependency(GradleConfiguration.Implementation, "ch.qos.logback", "ch.qos.logback", "logback-classic", KTOR_LOGGING_BACKEND_VERSION)
147149
val KTOR_SERVER_CONTENT_NEGOTIATION = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.server.plugins.contentnegotiation", "io.ktor", "ktor-server-content-negotiation", KTOR_VERSION)
148150
val KTOR_SERVER_CBOR_SERDE = KotlinDependency(GradleConfiguration.Implementation, "io.ktor.serialization.kotlinx.cbor", "io.ktor", "ktor-serialization-kotlinx-cbor", KTOR_VERSION)
149151
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,19 +487,37 @@ object RuntimeTypes {
487487

488488
object KtorServerCore : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CORE) {
489489
val embeddedServer = symbol("embeddedServer", "engine")
490+
val EmbeddedServerType = symbol("EmbeddedServer", "engine")
491+
val ApplicationEngineFactory = symbol("ApplicationEngineFactory", "engine")
492+
490493
val Application = symbol("Application", "application")
494+
val ApplicationStopping = symbol("ApplicationStopping", "application")
495+
val ApplicationStopped = symbol("ApplicationStopped", "application")
496+
val ApplicationCreateRouteScopedPlugin = symbol("createRouteScopedPlugin", "application")
497+
val ApplicationRouteScopedPlugin = symbol("RouteScopedPlugin", "application")
491498
val applicationCall = symbol("call", "application")
492499
val install = symbol("install", "application")
493500
}
494501

495502
object KtorServerRouting : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CORE) {
496503
val routing = symbol("routing", "routing")
504+
val route = symbol("route", "routing")
497505
val get = symbol("get", "routing")
498506
val post = symbol("post", "routing")
507+
val put = symbol("put", "routing")
508+
val delete = symbol("delete", "routing")
509+
val patch = symbol("patch", "routing")
510+
val head = symbol("head", "routing")
511+
val options = symbol("options", "routing")
499512

500-
val responseText = symbol("respondText", "response")
501513
val requestReceive = symbol("receive", "request")
502-
val requestRespondBytes = symbol("respondBytes", "response")
514+
val requestUri = symbol("uri", "request")
515+
val requestHttpMethod = symbol("httpMethod", "request")
516+
517+
val responseText = symbol("respondText", "response")
518+
val responseRespond = symbol("respond", "response")
519+
val responseRespondBytes = symbol("respondBytes", "response")
520+
val requestContentType = symbol("contentType", "request")
503521
}
504522

505523
object KtorServerNetty : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_NETTY) {
@@ -510,13 +528,21 @@ object RuntimeTypes {
510528
val ContentType = symbol("ContentType")
511529
val HttpStatusCode = symbol("HttpStatusCode")
512530
val Cbor = symbol("Cbor", "ContentType.Application")
531+
val Json = symbol("Json", "ContentType.Application")
532+
}
533+
534+
object KtorServerLogging : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_LOGGING) {
535+
val CallLogging = symbol("CallLogging")
513536
}
514537

515-
object KtorServerContentNegotiation : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CONTENT_NEGOTIATION) {
516-
val ContentNegotiation = symbol("ContentNegotiation")
538+
object KtorLoggingSlf4j : RuntimeTypePackage(KotlinDependency.KTOR_LOGGING_SLF4J) {
539+
val Level = symbol("Level", "event")
540+
val LoggerFactory = symbol("LoggerFactory")
541+
val ROOT_LOGGER_NAME = symbol("ROOT_LOGGER_NAME", "Logger")
517542
}
518543

519-
object KtorServerCbor : RuntimeTypePackage(KotlinDependency.KTOR_SERVER_CBOR_SERDE) {
520-
val cbor = symbol("cbor")
544+
object KtorLoggingLogback : RuntimeTypePackage(KotlinDependency.KTOR_LOGGING_LOGBACK) {
545+
val Level = symbol("Level", "classic")
546+
val LoggerContext = symbol("LoggerContext", "classic")
521547
}
522548
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/service/ServiceStubConfigurations.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,3 @@ enum class ServiceFramework(val value: String) {
1313
}
1414
}
1515
}
16-
17-
enum class ServiceEngine(val value: String) {
18-
NETTY("Netty"),
19-
;
20-
21-
override fun toString(): String = value
22-
23-
companion object {
24-
fun fromValue(value: String): ServiceEngine = when (value.lowercase()) {
25-
"netty" -> NETTY
26-
else -> throw IllegalArgumentException("$value is not a valid ServerFramework value, expected $NETTY")
27-
}
28-
}
29-
}

0 commit comments

Comments
 (0)