@@ -7,88 +7,135 @@ It produces **complete service stubs**—including routing, serialization/deseri
7
7
8
8
While Ktor is the default backend, the architecture is framework-agnostic, allowing future support for other server frameworks.
9
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
10
+ ---
11
+
12
+ ## Feature Summary
13
+
14
+ | ** Features** | ** Description** |
15
+ | ---------------------------| -----------------|
16
+ | Service Framework | Abstracted service framework interface and base implementation with Ktor as the default backend |
17
+ | CBOR Protocol | Support for CBOR serialization / deserialization and CBOR protocol traits |
18
+ | Json Protocol | Support for Json serialization / deserialization and Json protocol traits |
19
+ | Routing | Per-operation routing generation with Ktor DSL; ties to handler and validation |
20
+ | Error Handler | Unified exception handling logic mapped to HTTP status codes and support for error trait |
21
+ | Authentication (bearer) | Bearer token authentication middleware with model-driven configuration |
22
+ | Logging | Structured logging setup |
23
+ | Constraints Checker | Validation logic generated from Smithy traits and invoked pre-handler |
24
+ | Unit Test | Covers serialization/deserialization, routing, validation, and integration tests |
25
+
26
+ ---
27
+
28
+ ## Smithy Protocol Traits Support
29
+
30
+ | ** Traits** | ** CBOR Protocol** | ** Json Protocol** |
31
+ | --------------------------| -------------------| -------------------|
32
+ | http | Yes | Yes |
33
+ | httpError | Yes | Yes |
34
+ | httpHeader | Not supported | Yes |
35
+ | httpPrefixHeader | Not supported | Yes |
36
+ | httpLabel | Not supported | Yes |
37
+ | httpQuery | Not supported | Yes |
38
+ | httpQueryParams | Not supported | Yes |
39
+ | httpPayload | Not supported | Yes |
40
+ | jsonName | Not supported | Yes |
41
+ | timestampFormat | Not supported | Yes |
42
+ | httpChecksumRequired | Not supported | Not implemented yet |
43
+ | requestCompression | Not implemented yet | Not implemented yet |
44
+
45
+ ---
46
+
47
+ ## Constraint Traits Support
48
+
49
+ | ** Traits** | ** CBOR Protocol** | ** Json Protocol** |
50
+ | -----------------| ------------------------------| ------------------------------|
51
+ | required | Yes | Yes |
52
+ | length | Yes | Yes |
53
+ | pattern | Yes | Yes |
54
+ | private | Yes (handled by Smithy) | Yes (handled by Smithy) |
55
+ | range | Yes | Yes |
56
+ | uniqueItems | Yes | Yes |
57
+ | idRef | Not implemented yet | Not implemented yet |
19
58
20
59
---
21
60
22
61
## Getting Started
23
62
24
- ### 1. Build & Publish to Local Maven
25
- From the project root, run :
63
+ ### Step 1: Build & Publish Codegen to Local Maven
64
+ First, in ** this repository ** , build and publish the code generator locally :
26
65
``` bash
27
66
./gradlew :codegen:smithy-kotlin-codegen:build
28
67
./gradlew publishToMavenLocal
29
68
```
30
69
31
70
---
32
71
33
- ### 2. Create a New Kotlin Project
72
+ ### Step 2: Create a New Kotlin Project
73
+ Now, create a ** new Kotlin project** where you will use the Smithy Kotlin service code generator.
74
+
75
+ From this point forward, ** all steps apply to the new Kotlin project** you just created.
34
76
35
- In your ** ` build.gradle.kts ` ** :
77
+ ---
78
+
79
+ ### Step 3: Configure ` build.gradle.kts ` in the New Project
36
80
37
81
``` kotlin
38
82
plugins {
39
- id(" software.amazon.smithy.gradle.smithy-jar" ) version " 1.3.0" // check for latest version
83
+ alias(libs.plugins.kotlin.jvm)
84
+ id(" software.amazon.smithy.gradle.smithy-jar" ) version " 1.3.0" // check for latest version
85
+ application
40
86
}
41
87
42
88
repositories {
43
- mavenLocal()
44
- mavenCentral()
89
+ mavenLocal()
90
+ mavenCentral()
45
91
}
46
92
47
93
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>" )
94
+ smithyBuild(" software.amazon.smithy.kotlin:smithy-kotlin-codegen:<codegenVersion>" )
95
+ implementation(" software.amazon.smithy.kotlin:smithy-aws-kotlin-codegen:<codegenVersion>" )
96
+ implementation(" software.amazon.smithy:smithy-model:<smithyVersion>" )
97
+ implementation(" software.amazon.smithy:smithy-build:<smithyVersion>" )
98
+ implementation(" software.amazon.smithy:smithy-aws-traits:<smithyVersion>" )
99
+ .. .
53
100
}
54
101
```
55
102
56
103
---
57
104
58
- ### 3. Configure Smithy Build
59
-
60
- Create ` smithy-build.json ` in the same directory as ` build.gradle.kts ` :
61
-
105
+ ### Step 4: Create ` smithy-build.json ` in the New Project
106
+ This is an example of smithy-build.json.
62
107
``` json
63
108
{
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
- }
109
+ "version" : " 1.0" ,
110
+ "outputDirectory" : " build/generated-src" , // define the output path
111
+ "plugins" : {
112
+ "kotlin-codegen" : {
113
+ // change based on smithy model <namespace>#<service shape name>
114
+ "service" : " com.demo#DemoService" ,
115
+ "package" : {
116
+ "name" : " com.demo.server" ,
117
+ "version" : " 1.0.0"
118
+ },
119
+ "build" : {
120
+ "rootProject" : true ,
121
+ "generateServiceProject" : true ,
122
+ "optInAnnotations" : [
123
+ " aws.smithy.kotlin.runtime.InternalApi" ,
124
+ " kotlinx.serialization.ExperimentalSerializationApi"
125
+ ]
126
+ },
127
+ "serviceStub" : {
128
+ // choose server framework, only ktor is supported now
129
+ "framework" : " ktor"
130
+ }
131
+ }
132
+ }
86
133
}
87
134
```
88
135
89
136
---
90
137
91
- ### 4. Define Your Smithy Model
138
+ ### Step 5: Define Your Smithy Model in the New Project
92
139
93
140
Create a ` model ` directory and add your ` .smithy ` files.
94
141
Example ` model/greeter.smithy ` :
@@ -103,43 +150,47 @@ use smithy.api#httpBearerAuth
103
150
@restJson1
104
151
@httpBearerAuth
105
152
service DemoService {
106
- version: "1.0.0"
107
- operations: [SayHello]
153
+ version: "1.0.0"
154
+ operations: [
155
+ SayHello
156
+ ]
108
157
}
109
158
110
159
@http(method: "POST", uri: "/greet", code: 201)
111
160
operation SayHello {
112
- input: SayHelloInput
113
- output: SayHelloOutput
114
- errors: [CustomError]
161
+ input: SayHelloInput
162
+ output: SayHelloOutput
163
+ errors: [
164
+ CustomError
165
+ ]
115
166
}
116
167
117
168
@input
118
169
structure SayHelloInput {
119
- @required
120
- @length(min: 3, max: 10)
121
- name: String
122
- @httpHeader("X-User-ID")
123
- id: Integer
170
+ @required
171
+ @length(min: 3, max: 10)
172
+ name: String
173
+ @httpHeader("X-User-ID")
174
+ id: Integer
124
175
}
125
176
126
177
@output
127
178
structure SayHelloOutput {
128
- greeting: String
179
+ greeting: String
129
180
}
130
181
131
182
@error("server")
132
183
@httpError(500)
133
184
structure CustomError {
134
- msg: String
135
- @httpHeader("X-User-error")
136
- err: String
185
+ msg: String
186
+ @httpHeader("X-User-error")
187
+ err: String
137
188
}
138
189
```
139
190
140
191
---
141
192
142
- ### 5. Generate the Service
193
+ ### Step 6: Generate the Service in the New Project
143
194
144
195
Run:
145
196
``` bash
@@ -153,7 +204,7 @@ gradle clean
153
204
154
205
---
155
206
156
- ### 6. Run the Service
207
+ ### Step 7: Run the Generated Service
157
208
158
209
The generated service will be in the directory specified in ` smithy-build.json ` (` outputDirectory ` ).
159
210
You can start it by running:
@@ -164,7 +215,7 @@ By default, it listens on port **8080**.
164
215
165
216
---
166
217
167
- ### 7. Adjust Service Configuration
218
+ ### Step 8: Adjust Service Configuration
168
219
169
220
You can override runtime settings (such as port or HTTP engine) using command-line arguments:
170
221
``` bash
0 commit comments