|
1 |
| -This is README file |
| 1 | +# Smithy Kotlin Service Codegen (Ktor) |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This project extends **Smithy Kotlin** to generate **service-side code** from Smithy models, targeting the **Ktor** framework for server implementation. |
| 6 | +It produces **complete service stubs**—including routing, serialization/deserialization, authentication, and validation—so developers can focus entirely on implementing business logic. |
| 7 | + |
| 8 | +While Ktor is the default backend, the architecture is framework-agnostic, allowing future support for other server frameworks. |
| 9 | + |
| 10 | +### Key Features |
| 11 | +- **Automatic Service Stub Generation** from Smithy models |
| 12 | +- **Protocol Support**: CBOR & JSON |
| 13 | +- **Request Routing** generated from Smithy operations |
| 14 | +- **Authentication**: Bearer, SigV4, SigV4A |
| 15 | +- **Request Constraints & Validation** from Smithy traits |
| 16 | +- **Error Handling** with a consistent JSON/CBOR envelope |
| 17 | +- **Logging** with structured output for production readiness |
| 18 | +- **Extensible Architecture** for alternative frameworks |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Getting Started |
| 23 | + |
| 24 | +### 1. Build & Publish to Local Maven |
| 25 | +From the project root, run: |
| 26 | +```bash |
| 27 | +./gradlew :codegen:smithy-kotlin-codegen:build |
| 28 | +./gradlew publishToMavenLocal |
| 29 | +``` |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +### 2. Create a New Kotlin Project |
| 34 | + |
| 35 | +In your **`build.gradle.kts`**: |
| 36 | + |
| 37 | +```kotlin |
| 38 | +plugins { |
| 39 | +id("software.amazon.smithy.gradle.smithy-jar") version "1.3.0" // check for latest version |
| 40 | +} |
| 41 | + |
| 42 | +repositories { |
| 43 | +mavenLocal() |
| 44 | +mavenCentral() |
| 45 | +} |
| 46 | + |
| 47 | +dependencies { |
| 48 | +smithyBuild("software.amazon.smithy.kotlin:smithy-kotlin-codegen:<codegenVersion>") |
| 49 | +implementation("software.amazon.smithy.kotlin:smithy-aws-kotlin-codegen:<codegenVersion>") |
| 50 | +implementation("software.amazon.smithy:smithy-model:<smithyVersion>") |
| 51 | +implementation("software.amazon.smithy:smithy-build:<smithyVersion>") |
| 52 | +implementation("software.amazon.smithy:smithy-aws-traits:<smithyVersion>") |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +### 3. Configure Smithy Build |
| 59 | + |
| 60 | +Create `smithy-build.json` in the same directory as `build.gradle.kts`: |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | +"version": "1.0", |
| 65 | +"outputDirectory": "build/generated-src", |
| 66 | +"plugins": { |
| 67 | +"kotlin-codegen": { |
| 68 | +"service": "com.demo#DemoService", |
| 69 | +"package": { |
| 70 | +"name": "com.demo.server", |
| 71 | +"version": "1.0.0" |
| 72 | +}, |
| 73 | +"build": { |
| 74 | +"rootProject": true, |
| 75 | +"generateServiceProject": true, |
| 76 | +"optInAnnotations": [ |
| 77 | +"aws.smithy.kotlin.runtime.InternalApi", |
| 78 | +"kotlinx.serialization.ExperimentalSerializationApi" |
| 79 | +] |
| 80 | +}, |
| 81 | +"serviceStub": { |
| 82 | +"framework": "ktor" |
| 83 | +} |
| 84 | +} |
| 85 | +} |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### 4. Define Your Smithy Model |
| 92 | + |
| 93 | +Create a `model` directory and add your `.smithy` files. |
| 94 | +Example `model/greeter.smithy`: |
| 95 | + |
| 96 | +```smithy |
| 97 | +$version: "2.0" |
| 98 | +namespace com.demo |
| 99 | +
|
| 100 | +use aws.protocols#restJson1 |
| 101 | +use smithy.api#httpBearerAuth |
| 102 | +
|
| 103 | +@restJson1 |
| 104 | +@httpBearerAuth |
| 105 | +service DemoService { |
| 106 | +version: "1.0.0" |
| 107 | +operations: [SayHello] |
| 108 | +} |
| 109 | +
|
| 110 | +@http(method: "POST", uri: "/greet", code: 201) |
| 111 | +operation SayHello { |
| 112 | +input: SayHelloInput |
| 113 | +output: SayHelloOutput |
| 114 | +errors: [CustomError] |
| 115 | +} |
| 116 | +
|
| 117 | +@input |
| 118 | +structure SayHelloInput { |
| 119 | +@required |
| 120 | +@length(min: 3, max: 10) |
| 121 | +name: String |
| 122 | +@httpHeader("X-User-ID") |
| 123 | +id: Integer |
| 124 | +} |
| 125 | +
|
| 126 | +@output |
| 127 | +structure SayHelloOutput { |
| 128 | +greeting: String |
| 129 | +} |
| 130 | +
|
| 131 | +@error("server") |
| 132 | +@httpError(500) |
| 133 | +structure CustomError { |
| 134 | +msg: String |
| 135 | +@httpHeader("X-User-error") |
| 136 | +err: String |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +### 5. Generate the Service |
| 143 | + |
| 144 | +Run: |
| 145 | +```bash |
| 146 | +gradle build run |
| 147 | +``` |
| 148 | + |
| 149 | +If you want to clean previously generated code: |
| 150 | +```bash |
| 151 | +gradle clean |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +### 6. Run the Service |
| 157 | + |
| 158 | +The generated service will be in the directory specified in `smithy-build.json` (`outputDirectory`). |
| 159 | +You can start it by running: |
| 160 | +```bash |
| 161 | +gradle run |
| 162 | +``` |
| 163 | +By default, it listens on port **8080**. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +### 7. Adjust Service Configuration |
| 168 | + |
| 169 | +You can override runtime settings (such as port or HTTP engine) using command-line arguments: |
| 170 | +```bash |
| 171 | +gradle run --args="port 8000 engineFactory cio" |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Notes |
| 177 | +- **Business Logic**: Implement your own logic in the generated operation handler interfaces. |
| 178 | +- **Configuration**: Adjust port, engine, auth, and other settings via `ServiceFrameworkConfig` or CLI args. |
| 179 | +- **Future Extensions**: Planned support for more serialization formats (JSON, XML) and AWS SigV4 auth. |
0 commit comments