├── .editorconfig ├── .github └── workflows │ └── build.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle.kts ├── buildSrc ├── build.gradle.kts ├── settings.gradle.kts └── src │ └── main │ └── kotlin │ ├── .gitignore │ └── build │ ├── Common.kt │ ├── Consts.kt │ ├── Library.kt │ └── Plugin.kt ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── kert-graphql ├── build.gradle.kts └── src │ ├── main │ ├── kotlin │ │ └── ws │ │ │ └── leap │ │ │ └── kert │ │ │ └── graphql │ │ │ ├── Client.kt │ │ │ ├── ContextFunctionDataFetcher.kt │ │ │ └── Server.kt │ └── resources │ │ └── playground.html │ └── test │ ├── kotlin │ └── ws │ │ └── leap │ │ └── kert │ │ └── graphql │ │ └── Example.kt │ └── resources │ ├── .graphqlconfig │ └── ExampleQuery.graphql ├── kert-grpc-compiler ├── build.gradle.kts └── src │ ├── main │ └── go │ │ ├── generator │ │ ├── generator.go │ │ ├── service-generator.go │ │ └── templates │ │ │ └── service.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── main.go │ │ └── util │ │ └── util.go │ └── test │ ├── golden │ └── TestServiceGrpcRx.java.txt │ └── proto │ └── test.proto ├── kert-grpc ├── build.gradle.kts ├── readme.md └── src │ ├── main │ ├── kotlin │ │ └── ws │ │ │ └── leap │ │ │ └── kert │ │ │ └── grpc │ │ │ ├── AbstractStub.kt │ │ │ ├── ClientCalls.kt │ │ │ ├── Constants.kt │ │ │ ├── GrpcUtils.kt │ │ │ ├── Server.kt │ │ │ ├── ServerCalls.kt │ │ │ ├── ServerMethodDefinition.kt │ │ │ ├── ServerReflectionImpl.kt │ │ │ ├── ServerServiceDefinition.kt │ │ │ ├── ServiceRegistry.kt │ │ │ └── Types.kt │ └── proto │ │ └── reflection.proto │ └── test │ ├── kotlin │ └── ws │ │ └── leap │ │ └── kert │ │ └── grpc │ │ ├── EchoServiceImpl.kt │ │ ├── EchoServiceJavaImpl.kt │ │ ├── Example.kt │ │ ├── GrpcBasicSpec.kt │ │ ├── GrpcErrorSpec.kt │ │ ├── GrpcInterceptorSpec.kt │ │ ├── GrpcNestedBidiSpec.kt │ │ ├── GrpcSpec.kt │ │ ├── GrpcUnimplementedSpec.kt │ │ └── ManualTestClient.kt │ ├── proto │ └── echo.proto │ └── resources │ └── logback.xml ├── kert-http ├── build.gradle.kts └── src │ ├── main │ └── kotlin │ │ └── ws │ │ └── leap │ │ └── kert │ │ └── http │ │ ├── HttpClient.kt │ │ ├── HttpClientImpl.kt │ │ ├── HttpClientRequest.kt │ │ ├── HttpClientResponse.kt │ │ ├── HttpRouter.kt │ │ ├── HttpRouterDsl.kt │ │ ├── HttpServer.kt │ │ ├── HttpServerRequest.kt │ │ ├── HttpServerResponse.kt │ │ ├── Kert.kt │ │ ├── Message.kt │ │ ├── Stream.kt │ │ ├── StreamChannel.kt │ │ └── Types.kt │ └── test │ ├── kotlin │ └── ws │ │ └── leap │ │ └── kert │ │ └── http │ │ ├── ClientServerSpec.kt │ │ ├── HttpClientSpec.kt │ │ ├── HttpFilterSpec.kt │ │ ├── KertClientSpec.kt │ │ ├── KertTestServer.kt │ │ ├── MockReadStream.kt │ │ ├── MockWriteStream.kt │ │ ├── TestConfig.kt │ │ ├── VertxClientSpec.kt │ │ ├── VertxTestServer.kt │ │ └── VertxWebClientSpec.kt │ └── resources │ └── logback.xml ├── logo.png ├── publish.md ├── publish.sh └── settings.gradle.kts /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | max_line_length = 120 7 | 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 11 16 | uses: actions/setup-java@v2 17 | with: 18 | java-version: 11 19 | distribution: 'adopt' 20 | - name: Test with Gradle 21 | run: ./gradlew test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .idea 3 | out 4 | build 5 | gen 6 | *.iml 7 | *.ipr 8 | *.iws 9 | request.bin 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![build](https://github.com/wsleap/kert/actions/workflows/build.yml/badge.svg)](https://github.com/wsleap/kert/actions/workflows/build.yml) 3 | ![License](https://img.shields.io/github/license/wsleap/kert) 4 | [](https://search.maven.org/search?q=g:ws.leap.kert) 5 | [](https://oss.sonatype.org/content/repositories/snapshots/ws/leap/kert/) 6 | 7 | 8 | 9 | # Kert 10 | 11 | 12 | Kert is a concise HTTP, GRPC and GraphQL library for Kotlin. It's not an Android library, it's a JVM library for backend development. 13 | 14 | Compare to the official [gRPC-Java](https://github.com/grpc/grpc-java), Kert provides the benefits like: 15 | * No need for 2 separate libraries / ports to serve HTTP and GRPC requests. 16 | * Simply to use HTTP health check in Kubernetes. 17 | * Coroutine / Flow based interface more intuitive for async processing and context propagation. 18 | * Simple filter & interceptor interface for async handling. 19 | 20 | Server Example: 21 | ```kotlin 22 | val server = httpServer(8080) { 23 | // http filter 24 | filter { req, next -> 25 | println("Serving request ${req.path}") 26 | next(req) 27 | } 28 | 29 | // http service 30 | router { 31 | // http request handler 32 | get("/ping") { 33 | response(body = "pong") 34 | } 35 | } 36 | 37 | // grpc service 38 | grpc { 39 | // enable server reflection 40 | serverReflection = true 41 | 42 | // grpc interceptor 43 | interceptor( object : GrpcInterceptor { 44 | override suspend fun invoke(method: MethodDescriptor, req: GrpcRequest, next: GrpcHandler): GrpcResponse { 45 | // intercept the request 46 | if (req.metadata["authentication"] == null) throw IllegalArgumentException("Authentication header is missing") 47 | 48 | // intercept each message in the streaming request 49 | val filteredReq = req.copy(messages = req.messages.map { 50 | println(it) 51 | it 52 | }) 53 | return next(method, filteredReq) 54 | } 55 | }) 56 | 57 | // register service implementation 58 | service(EchoServiceImpl()) 59 | } 60 | 61 | // GraphQL 62 | graphql { 63 | playground = true 64 | 65 | schema { 66 | config { 67 | supportedPackages = listOf("") 68 | } 69 | query(MyExampleQuery()) 70 | } 71 | } 72 | } 73 | 74 | server.start() 75 | ``` 76 | 77 | Client Example: 78 | ```kotlin 79 | // http request 80 | val client = httpClient { 81 | options { 82 | defaultHost = "localhost" 83 | defaultPort = 8551 84 | protocolVersion = HttpVersion.HTTP_2 85 | } 86 | 87 | // a client side filter to set authorization header in request 88 | filter { req, next -> 89 | req.headers["authorization"] = "my-authorization-header" 90 | next(req) 91 | } 92 | } 93 | client.get("ping") 94 | 95 | // grpc request 96 | val stub = EchoGrpcKt.stub(client) 97 | stub.unary(EchoReq.newBuilder().setId(1).setValue("hello").build()) 98 | ``` 99 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.gradle.api.tasks.testing.logging.TestExceptionFormat 2 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 3 | 4 | plugins { 5 | idea 6 | `kotlin-dsl` 7 | kotlin("jvm") apply false // Enables Kotlin Gradle plugin 8 | signing 9 | `maven-publish` 10 | alias(libs.plugins.versions) 11 | alias(libs.plugins.test.logger) 12 | alias(libs.plugins.kotest) 13 | } 14 | 15 | allprojects { 16 | group = "ws.leap.kert" 17 | 18 | apply { 19 | plugin("idea") 20 | plugin("java") 21 | plugin("kotlin") 22 | plugin("com.google.protobuf") 23 | 24 | plugin("maven-publish") 25 | plugin("signing") 26 | plugin("com.adarshr.test-logger") 27 | plugin("com.github.ben-manes.versions") 28 | 29 | plugin("org.jetbrains.dokka") 30 | } 31 | 32 | repositories { 33 | mavenLocal() 34 | mavenCentral() 35 | } 36 | 37 | dependencies { 38 | // "libs" not working, must use "rootProject.libs" here 39 | // https://github.com/gradle/gradle/issues/16634 40 | implementation(rootProject.libs.kotlin.logging) 41 | 42 | testImplementation(rootProject.libs.bundles.kotest) 43 | testImplementation(rootProject.libs.logback.classic) 44 | } 45 | 46 | // set target jvm version, otherwise gradle will use the jdk version during compiling for "org.gradle.jvm.version" in module file 47 | java { 48 | sourceCompatibility = JavaVersion.VERSION_1_8 49 | targetCompatibility = JavaVersion.VERSION_1_8 50 | } 51 | 52 | tasks { 53 | withType { 54 | useJUnitPlatform() 55 | } 56 | 57 | withType { 58 | kotlinOptions { 59 | jvmTarget = "1.8" 60 | freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn", /*"-Xuse-k2"*/) 61 | } 62 | } 63 | 64 | withType { 65 | testLogging { 66 | exceptionFormat = TestExceptionFormat.FULL 67 | showExceptions = true 68 | showCauses = true 69 | showStackTraces = true 70 | events("passed", "skipped", "failed") 71 | } 72 | } 73 | } 74 | 75 | val isSnapshot = (version as String).endsWith("SNAPSHOT", true) 76 | publishing { 77 | repositories { 78 | maven { 79 | url = if (isSnapshot) { 80 | uri("https://oss.sonatype.org/content/repositories/snapshots") 81 | } else { 82 | uri("https://oss.sonatype.org/service/local/staging/deploy/maven2") 83 | } 84 | 85 | val ossrhUsername: String? by project 86 | val ossrhPassword: String? by project 87 | credentials { 88 | username = ossrhUsername 89 | password = ossrhPassword 90 | } 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * require these properties defined 97 | * signing.keyId 98 | * signing.password 99 | * signing.secretKeyRingFile 100 | */ 101 | signing { 102 | isRequired = !isSnapshot 103 | } 104 | 105 | // configure the test log output 106 | configure { 107 | theme = com.adarshr.gradle.testlogger.theme.ThemeType.MOCHA 108 | showExceptions = true 109 | showStandardStreams = false 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /buildSrc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `kotlin-dsl` // use Gradle Kotlin DSL for build files 3 | } 4 | 5 | repositories { 6 | mavenCentral() 7 | gradlePluginPortal() 8 | } 9 | 10 | dependencies { 11 | implementation(libs.plugin.kotlin) 12 | implementation(libs.plugin.os.detector) 13 | implementation(libs.plugin.protobuf) 14 | implementation(libs.plugin.dokka) 15 | } 16 | -------------------------------------------------------------------------------- /buildSrc/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "kert-build" 2 | 3 | dependencyResolutionManagement { 4 | versionCatalogs { 5 | create("libs") { 6 | from(files("../gradle/libs.versions.toml")) 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/.gitignore: -------------------------------------------------------------------------------- 1 | !build/ -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/build/Common.kt: -------------------------------------------------------------------------------- 1 | package build 2 | 3 | import org.gradle.api.Project 4 | import org.gradle.api.artifacts.VersionCatalog 5 | import org.gradle.api.artifacts.VersionCatalogsExtension 6 | import org.gradle.kotlin.dsl.getByType 7 | 8 | fun Project.versionCatalog(name: String): VersionCatalog { 9 | return extensions.getByType().named(name) 10 | } 11 | 12 | fun VersionCatalog.library(name: String) = findLibrary(name).get() 13 | fun VersionCatalog.version(name: String) = findVersion(name).get() 14 | -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/build/Consts.kt: -------------------------------------------------------------------------------- 1 | package build 2 | 3 | import org.gradle.api.Action 4 | import org.gradle.api.publish.maven.MavenPom 5 | 6 | object Consts { 7 | val pom = Action { 8 | name.set("kert") 9 | description.set("Concise HTTP & GRPC library for Kotlin") 10 | url.set("https://github.com/wsleap/kert") 11 | licenses { 12 | license { 13 | name.set("The Apache License, Version 2.0") 14 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") 15 | } 16 | } 17 | developers { 18 | developer { 19 | id.set("xiaodongw") 20 | name.set("Xiaodong Wang") 21 | email.set("xiaodongw79@gmail.com") 22 | } 23 | } 24 | scm { 25 | connection.set("scm:git:git://github.com/wsleap/kert.git") 26 | developerConnection.set("scm:git:ssh://github.com/wsleap/kert.git") 27 | url.set("https://github.com/wsleap/kert.git") 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/build/Library.kt: -------------------------------------------------------------------------------- 1 | package build 2 | 3 | import org.gradle.api.Project 4 | import org.gradle.api.plugins.JavaPluginExtension 5 | import org.gradle.api.publish.PublishingExtension 6 | import org.gradle.api.publish.maven.MavenPublication 7 | import org.gradle.api.tasks.SourceSetContainer 8 | import org.gradle.api.tasks.bundling.Jar 9 | import org.gradle.kotlin.dsl.* 10 | import org.gradle.plugins.signing.SigningExtension 11 | 12 | /** 13 | * Config the project as a library. 14 | * It adds Kotlin dependency, and publishing support. 15 | */ 16 | fun Project.configureLibrary() { 17 | val api by configurations 18 | val implementation by configurations 19 | val dokkaHtmlPlugin by configurations 20 | val libs = versionCatalog("libs") 21 | 22 | dependencies { 23 | api(libs.library("kotlin.stdlib")) 24 | api(libs.library("kotlinx.coroutines")) 25 | implementation(libs.library("slf4j.api")) 26 | 27 | dokkaHtmlPlugin(libs.library("dokka.kotlin.as.java.plugin")) 28 | } 29 | 30 | val sourceSets = extensions.getByName("sourceSets") as SourceSetContainer 31 | tasks { 32 | register("sourcesJar") { 33 | from(sourceSets["main"].allJava) 34 | archiveClassifier.set("sources") 35 | from(sourceSets.getByName("main").allSource) 36 | } 37 | 38 | register("javadocJar") { 39 | archiveClassifier.set("javadoc") 40 | from(tasks["dokkaHtml"]) 41 | dependsOn(tasks["dokkaHtml"]) 42 | } 43 | } 44 | 45 | configure { 46 | withSourcesJar() 47 | withJavadocJar() 48 | } 49 | 50 | configure { 51 | publications { 52 | create("maven") { 53 | artifactId = project.name 54 | from(components["java"]) 55 | 56 | versionMapping { 57 | usage("java-api") { 58 | fromResolutionOf("runtimeClasspath") 59 | } 60 | usage("java-runtime") { 61 | fromResolutionResult() 62 | } 63 | } 64 | 65 | pom(Consts.pom) 66 | } 67 | } 68 | } 69 | 70 | val publishing = extensions.getByName("publishing") as PublishingExtension 71 | configure { 72 | sign(publishing.publications["maven"]) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/build/Plugin.kt: -------------------------------------------------------------------------------- 1 | package build 2 | 3 | import com.google.gradle.osdetector.OsDetector 4 | import org.gradle.api.Project 5 | import org.gradle.kotlin.dsl.* 6 | import org.gradle.api.tasks.Copy 7 | import java.io.File 8 | import com.google.protobuf.gradle.* 9 | import org.gradle.api.plugins.JavaPluginExtension 10 | import org.gradle.api.publish.PublishingExtension 11 | import org.gradle.api.publish.maven.MavenPublication 12 | import org.gradle.api.tasks.Exec 13 | import org.gradle.api.tasks.SourceSetContainer 14 | import org.gradle.plugins.signing.SigningExtension 15 | import org.gradle.kotlin.dsl.* 16 | 17 | // os/arch from osDetector https://github.com/trustin/os-maven-plugin 18 | // os/arch for golang https://go.dev/doc/install/source#environment 19 | private fun goArch(arch: String): String { 20 | return when(arch) { 21 | "x86_64" -> "amd64" 22 | "x86_32" -> "386" 23 | "aarch_64" -> "arm64" 24 | else -> throw RuntimeException("Unsupported arch $arch") 25 | } 26 | } 27 | 28 | private fun goOs(os: String): String { 29 | return when(os) { 30 | "osx" -> "darwin" 31 | "linux" -> "linux" 32 | "windows" -> "windows" 33 | else -> throw RuntimeException("Unsupported os $os") 34 | } 35 | } 36 | 37 | /** 38 | * Configure the project as a GRPC plugin. 39 | * It adds support for compiling the plugin to Linux, Windows and MacOS, and publishing support. 40 | */ 41 | fun Project.configureGrpcPlugin(pluginName: String) { 42 | val osDetector = extensions.getByType(OsDetector::class) 43 | 44 | val testImplementation by configurations 45 | val libs = versionCatalog("libs") 46 | dependencies { 47 | testImplementation(libs.library("kotlin-stdlib")) 48 | } 49 | 50 | // overwrite os & arch if specified by command line 51 | val os = if (hasProperty("targetOs")) property("targetOs") as String else osDetector.os 52 | val arch = if (hasProperty("targetArch")) property("targetArch") as String else osDetector.arch 53 | val exeSuffix = if(os == "windows") ".exe" else "" 54 | 55 | val pluginPath = "$buildDir/exe/$pluginName${exeSuffix}" 56 | val artifactStagingPath: File = file("$buildDir/artifacts") 57 | 58 | tasks.register("buildPlugin", Exec::class) { 59 | workingDir = file("src/main/go") 60 | environment = environment + mapOf("GOOS" to goOs(os), "GOARCH" to goArch(arch)) 61 | commandLine = listOf("go", "build", "-o", pluginPath, "main.go") 62 | } 63 | 64 | tasks.register("buildArtifacts", Copy::class) { 65 | dependsOn("buildPlugin") 66 | from("$buildDir/exe") { 67 | if (os != "windows") { 68 | rename("(.+)", "$1.exe") 69 | } 70 | } 71 | into(artifactStagingPath) 72 | } 73 | 74 | configure { 75 | // generatedFilesBaseDir = "$projectDir/gen" 76 | protoc { 77 | artifact = "com.google.protobuf:protoc:${libs.version("protobuf")}" 78 | } 79 | plugins { 80 | id("grpc-kert") { 81 | path = pluginPath 82 | } 83 | } 84 | generateProtoTasks { 85 | all().forEach { task -> 86 | task.inputs.file(pluginPath) 87 | } 88 | ofSourceSet("test").forEach { task -> 89 | task.plugins { 90 | id("grpc-kert") { 91 | // enable this to write the input request to disk 92 | // option("write_input=true") 93 | } 94 | } 95 | } 96 | } 97 | } 98 | 99 | configure { 100 | publications { 101 | create("maven") { 102 | artifactId = pluginName 103 | artifact(file("$artifactStagingPath/$pluginName.exe")) { 104 | classifier = "$os-$arch" 105 | extension = "exe" 106 | builtBy(tasks.named("buildArtifacts")) 107 | } 108 | pom(Consts.pom) 109 | } 110 | } 111 | } 112 | 113 | val publishing = extensions.getByName("publishing") as PublishingExtension 114 | configure { 115 | sign(publishing.publications["maven"]) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | # 1) Remove -SNAPSHOT, 2) Publish, 3) Bump version with -SNAPSHOT 3 | version=0.7.1-SNAPSHOT 4 | -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | # https://docs.gradle.org/current/userguide/platforms.html#sub:conventional-dependencies-toml 2 | 3 | [versions] 4 | kotlin = "1.9.20" 5 | dokka = "1.9.20" 6 | kotlinx-coroutines = "1.6.4" 7 | vertx = "4.4.1" 8 | protobuf = "3.22.3" 9 | grpc = "1.54.1" 10 | kotest = "5.6.1" 11 | jackson = "2.15.0" 12 | graphql-kotlin = "6.4.0" 13 | kotlin-logging = "3.0.5" 14 | logback = "1.4.7" 15 | slf4j = "2.0.7" 16 | 17 | [libraries] 18 | kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } 19 | kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } 20 | kotlin-script-runtime = { module = "org.jetbrains.kotlin:kotlin-script-runtime", version.ref = "kotlin" } 21 | kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8", version.ref = "kotlinx-coroutines" } 22 | kotlinx-coroutines-slf4j = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j", version.ref = "kotlinx-coroutines" } 23 | vertx-web = { module = "io.vertx:vertx-web", version.ref = "vertx" } 24 | vertx-lang-kotlin-coroutines = { module = "io.vertx:vertx-lang-kotlin-coroutines", version.ref = "vertx" } 25 | vertx-web-client = { module = "io.vertx:vertx-web-client", version.ref = "vertx" } 26 | protobuf-kotlin = { module = "com.google.protobuf:protobuf-kotlin", version.ref = "protobuf" } 27 | protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" } 28 | grpc-protobuf = { module = "io.grpc:grpc-protobuf", version.ref = "grpc" } 29 | grpc-stub = { module = "io.grpc:grpc-stub", version.ref = "grpc" } 30 | grpc-netty = { module = "io.grpc:grpc-netty", version.ref = "grpc" } 31 | javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version = "1.3.2" } 32 | protoc-gen-grpc-java = { module = "io.grpc:protoc-gen-grpc-java", version.ref = "grpc" } 33 | protoc = { module = "com.google.protobuf:protoc", version.ref = "protobuf" } 34 | graphql-kotlin-server = { module = "com.expediagroup:graphql-kotlin-server", version.ref = "graphql-kotlin" } 35 | graphql-kotlin-client = { module = "com.expediagroup:graphql-kotlin-client", version.ref = "graphql-kotlin" } 36 | jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" } 37 | jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" } 38 | jackson-module-afterburner = { module = "com.fasterxml.jackson.module:jackson-module-afterburner", version.ref = "jackson" } 39 | slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } 40 | dokka-kotlin-as-java-plugin = { module = "org.jetbrains.dokka:kotlin-as-java-plugin", version.ref = "dokka" } 41 | kotlin-logging = { module = "io.github.microutils:kotlin-logging", version.ref = "kotlin-logging" } 42 | kotest-framework-engine-jvm = { module = "io.kotest:kotest-framework-engine-jvm", version.ref = "kotest" } 43 | kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } 44 | kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } 45 | logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } 46 | 47 | # plugins (for buildSrc) 48 | plugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } 49 | plugin-os-detector = { module = "com.google.gradle:osdetector-gradle-plugin", version = "1.7.3" } 50 | plugin-protobuf = { module = "com.google.protobuf:protobuf-gradle-plugin", version = "0.9.3" } 51 | plugin-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" } 52 | 53 | [bundles] 54 | kotlin = [ 55 | "kotlin-stdlib", 56 | "kotlin-reflect", 57 | "kotlin-script-runtime" 58 | ] 59 | kotest = [ 60 | "kotest-framework-engine-jvm", 61 | "kotest-runner-junit5", 62 | "kotest-assertions-core" 63 | ] 64 | 65 | [plugins] 66 | versions = { id = "com.github.ben-manes.versions", version = "0.42.0" } 67 | test-logger = { id = "com.adarshr.test-logger", version = "3.0.0" } # latest version 3.2.0 not working well with kotest, the output is duplicated 68 | kotest = { id = "io.kotest", version = "0.3.9" } 69 | graphql = { id = "com.expediagroup.graphql", version.ref = "graphql-kotlin" } 70 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wsleap/kert/e2bbded0d3b177868cadc50f922361a0dc423d68/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit 88 | 89 | # Use the maximum available, or set MAX_FD != -1 to use that value. 90 | MAX_FD=maximum 91 | 92 | warn () { 93 | echo "$*" 94 | } >&2 95 | 96 | die () { 97 | echo 98 | echo "$*" 99 | echo 100 | exit 1 101 | } >&2 102 | 103 | # OS specific support (must be 'true' or 'false'). 104 | cygwin=false 105 | msys=false 106 | darwin=false 107 | nonstop=false 108 | case "$( uname )" in #( 109 | CYGWIN* ) cygwin=true ;; #( 110 | Darwin* ) darwin=true ;; #( 111 | MSYS* | MINGW* ) msys=true ;; #( 112 | NONSTOP* ) nonstop=true ;; 113 | esac 114 | 115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 116 | 117 | 118 | # Determine the Java command to use to start the JVM. 119 | if [ -n "$JAVA_HOME" ] ; then 120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 121 | # IBM's JDK on AIX uses strange locations for the executables 122 | JAVACMD=$JAVA_HOME/jre/sh/java 123 | else 124 | JAVACMD=$JAVA_HOME/bin/java 125 | fi 126 | if [ ! -x "$JAVACMD" ] ; then 127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 128 | 129 | Please set the JAVA_HOME variable in your environment to match the 130 | location of your Java installation." 131 | fi 132 | else 133 | JAVACMD=java 134 | if ! command -v java >/dev/null 2>&1 135 | then 136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | fi 142 | 143 | # Increase the maximum file descriptors if we can. 144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 145 | case $MAX_FD in #( 146 | max*) 147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 148 | # shellcheck disable=SC2039,SC3045 149 | MAX_FD=$( ulimit -H -n ) || 150 | warn "Could not query maximum file descriptor limit" 151 | esac 152 | case $MAX_FD in #( 153 | '' | soft) :;; #( 154 | *) 155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 156 | # shellcheck disable=SC2039,SC3045 157 | ulimit -n "$MAX_FD" || 158 | warn "Could not set maximum file descriptor limit to $MAX_FD" 159 | esac 160 | fi 161 | 162 | # Collect all arguments for the java command, stacking in reverse order: 163 | # * args from the command line 164 | # * the main class name 165 | # * -classpath 166 | # * -D...appname settings 167 | # * --module-path (only if needed) 168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 169 | 170 | # For Cygwin or MSYS, switch paths to Windows format before running java 171 | if "$cygwin" || "$msys" ; then 172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -classpath "$CLASSPATH" \ 214 | org.gradle.wrapper.GradleWrapperMain \ 215 | "$@" 216 | 217 | # Stop when "xargs" is not available. 218 | if ! command -v xargs >/dev/null 2>&1 219 | then 220 | die "xargs is not available" 221 | fi 222 | 223 | # Use "xargs" to parse quoted args. 224 | # 225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 226 | # 227 | # In Bash we could simply go: 228 | # 229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 230 | # set -- "${ARGS[@]}" "$@" 231 | # 232 | # but POSIX shell has neither arrays nor command substitution, so instead we 233 | # post-process each arg (as a line of input to sed) to backslash-escape any 234 | # character that might be a shell metacharacter, then use eval to reverse 235 | # that process (while maintaining the separation between arguments), and wrap 236 | # the whole thing up as a single "set" statement. 237 | # 238 | # This will of course break if any of these variables contains a newline or 239 | # an unmatched quote. 240 | # 241 | 242 | eval "set -- $( 243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 244 | xargs -n1 | 245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 246 | tr '\n' ' ' 247 | )" '"$@"' 248 | 249 | exec "$JAVACMD" "$@" 250 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /kert-graphql/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import build.* 2 | 3 | plugins { 4 | alias(libs.plugins.graphql) 5 | } 6 | 7 | description = "Kert GraphQL support" 8 | 9 | configureLibrary() 10 | 11 | dependencies { 12 | api(project(":kert-http")) 13 | api(libs.graphql.kotlin.server) 14 | api(libs.graphql.kotlin.client) 15 | 16 | implementation(libs.jackson.databind) 17 | implementation(libs.jackson.module.kotlin) 18 | implementation(libs.jackson.module.afterburner) 19 | } 20 | 21 | //graphql { 22 | // client { 23 | // // Gradle build fails if server is not running 24 | // endpoint = "http://localhost:8500/graphql" 25 | // packageName = "ws.leap.kert.graphql.example" 26 | // queryFiles = listOf(file("${project.projectDir}/src/test/resources/ExampleQuery.graphql")) 27 | // } 28 | //} 29 | -------------------------------------------------------------------------------- /kert-graphql/src/main/kotlin/ws/leap/kert/graphql/Client.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.graphql 2 | 3 | class Client { 4 | } 5 | -------------------------------------------------------------------------------- /kert-graphql/src/main/kotlin/ws/leap/kert/graphql/ContextFunctionDataFetcher.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.graphql 2 | 3 | import com.expediagroup.graphql.generator.execution.FunctionDataFetcher 4 | import com.expediagroup.graphql.generator.execution.SimpleKotlinDataFetcherFactoryProvider 5 | import graphql.schema.DataFetcherFactory 6 | import graphql.schema.DataFetchingEnvironment 7 | import kotlin.reflect.KFunction 8 | import kotlin.reflect.full.instanceParameter 9 | 10 | open class ContextDataFetcherFactoryProvider : SimpleKotlinDataFetcherFactoryProvider() { 11 | 12 | override fun functionDataFetcherFactory(target: Any?, kFunction: KFunction<*>) = DataFetcherFactory { 13 | ContextFunctionDataFetcher( 14 | target = target, 15 | fn = kFunction 16 | ) 17 | } 18 | } 19 | 20 | class ContextFunctionDataFetcher( 21 | private val target: Any?, 22 | private val fn: KFunction<*> 23 | ) : FunctionDataFetcher(target, fn) { 24 | override fun get(environment: DataFetchingEnvironment): Any? { 25 | val instance: Any? = target ?: environment.getSource() 26 | val instanceParameter = fn.instanceParameter 27 | 28 | return if (instance != null && instanceParameter != null) { 29 | val parameterValues = getParameters(fn, environment) 30 | .plus(instanceParameter to instance) 31 | 32 | if (fn.isSuspend) { 33 | runSuspendingFunction(environment, parameterValues) 34 | } else { 35 | runBlockingFunction(parameterValues) 36 | } 37 | } else { 38 | null 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /kert-graphql/src/main/kotlin/ws/leap/kert/graphql/Server.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.graphql 2 | 3 | import com.expediagroup.graphql.generator.SchemaGeneratorConfig 4 | import com.expediagroup.graphql.generator.TopLevelNames 5 | import com.expediagroup.graphql.generator.TopLevelObject 6 | import com.expediagroup.graphql.generator.hooks.NoopSchemaGeneratorHooks 7 | import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks 8 | import com.expediagroup.graphql.generator.toSchema 9 | import com.expediagroup.graphql.server.operations.Mutation 10 | import com.expediagroup.graphql.server.operations.Query 11 | import com.expediagroup.graphql.server.operations.Subscription 12 | import com.fasterxml.jackson.databind.ObjectMapper 13 | import com.fasterxml.jackson.module.afterburner.AfterburnerModule 14 | import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder 15 | import graphql.ExecutionInput.newExecutionInput 16 | import graphql.GraphQL 17 | import graphql.schema.GraphQLSchema 18 | import io.vertx.core.http.HttpMethod 19 | import kotlinx.coroutines.future.await 20 | import ws.leap.kert.http.HttpRouterDsl 21 | import ws.leap.kert.http.HttpServerBuilderDsl 22 | import ws.leap.kert.http.response 23 | import kotlin.coroutines.coroutineContext 24 | 25 | 26 | fun HttpServerBuilderDsl.graphql(configure: GraphQlServerBuilder.() -> Unit) { 27 | router { 28 | val builder = GraphQlServerBuilder(this) 29 | configure(builder) 30 | builder.build() 31 | } 32 | } 33 | 34 | data class GraphqlRequest( 35 | val operationName: String? = null, 36 | val variables: Map? = null, 37 | val query: String 38 | ) 39 | 40 | class ConfigBuilder() { 41 | var supportedPackages: List = emptyList() 42 | var hooks: SchemaGeneratorHooks = NoopSchemaGeneratorHooks 43 | 44 | fun build(): SchemaGeneratorConfig { 45 | return SchemaGeneratorConfig( 46 | supportedPackages = supportedPackages, 47 | hooks = hooks, 48 | dataFetcherFactoryProvider = ContextDataFetcherFactoryProvider(), 49 | ) 50 | } 51 | } 52 | 53 | class SchemaBuilder(private val mapper: ObjectMapper) { 54 | private val queries = mutableListOf() 55 | private val mutations = mutableListOf() 56 | private val subscriptions = mutableListOf() 57 | 58 | private val configBuilder = ConfigBuilder() 59 | fun config(configure: ConfigBuilder.() -> Unit) { 60 | configure(configBuilder) 61 | } 62 | 63 | fun query(query: Query) { 64 | queries.add(query) 65 | } 66 | 67 | fun mutation(mutation: Mutation) { 68 | mutations.add(mutation) 69 | } 70 | 71 | fun subscription(subscription: Subscription) { 72 | subscriptions.add(subscription) 73 | } 74 | 75 | fun build(): GraphQLSchema { 76 | return toSchema( 77 | configBuilder.build(), 78 | queries.map { TopLevelObject(it) }, 79 | mutations.map { TopLevelObject(it) }, 80 | subscriptions.map { TopLevelObject(it) }, 81 | ) 82 | } 83 | } 84 | 85 | class GraphQlServerBuilder(private val routerBuilder: HttpRouterDsl) { 86 | var endpoint: String = "/graphql" 87 | var playground: Boolean = false 88 | 89 | var mapper: ObjectMapper = jacksonMapperBuilder() 90 | .addModule(AfterburnerModule()) 91 | .build() 92 | 93 | private var schemaConfigurator: SchemaBuilder.() -> Unit = {} 94 | 95 | fun schema(configure: SchemaBuilder.() -> Unit) { 96 | schemaConfigurator = configure 97 | } 98 | 99 | fun build() { 100 | val schemaBuilder = SchemaBuilder(mapper) 101 | schemaConfigurator(schemaBuilder) 102 | val graphql = GraphQL.newGraphQL(schemaBuilder.build()).build() 103 | 104 | routerBuilder.call(HttpMethod.POST, endpoint) { req -> 105 | val json = req.body().toString(Charsets.UTF_8) 106 | val request = mapper.readValue(json, GraphqlRequest::class.java) 107 | 108 | val input = newExecutionInput() 109 | .graphQLContext(mapOf("coroutine-context" to coroutineContext)) 110 | .query(request.query) 111 | .operationName(request.operationName ?: "") 112 | .variables(request.variables ?: emptyMap()) 113 | .build() 114 | 115 | val result = graphql.executeAsync(input).await() 116 | 117 | val responseBody = mapper.writeValueAsString(result.toSpecification()) 118 | response(body = responseBody, contentType = "application/json") 119 | } 120 | 121 | if(playground) { 122 | routerBuilder.call(HttpMethod.GET, endpoint) { 123 | val playgroundHtml = GraphQlServerBuilder::class.java.classLoader.getResource("playground.html").readBytes() 124 | response(body = playgroundHtml, contentType = "text/html") 125 | } 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /kert-graphql/src/test/kotlin/ws/leap/kert/graphql/Example.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.graphql 2 | 3 | import com.expediagroup.graphql.generator.annotations.GraphQLDescription 4 | import com.expediagroup.graphql.server.operations.Query 5 | import kotlinx.coroutines.runBlocking 6 | import ws.leap.kert.http.httpServer 7 | 8 | enum class Gender { 9 | MALE, 10 | FEMALE 11 | } 12 | 13 | class StudentQuery : Query { 14 | companion object { 15 | val students = mapOf( 16 | "andy" to Student("andy", "Andy", 12, Gender.MALE), 17 | "john" to Student("john", "John", 13, Gender.MALE), 18 | "mary" to Student("mary", "Mary", 11, Gender.FEMALE), 19 | "lucy" to Student("lucy", "Lucy", 14, Gender.FEMALE), 20 | "mike" to Student("mike", "Mike", 12, Gender.MALE), 21 | ) 22 | val friendships = mapOf( 23 | "andy" to setOf("mary", "john"), 24 | "john" to setOf("mary", "lucy"), 25 | "mary" to setOf("andy", "john", "lucy"), 26 | "lucy" to setOf("mike"), 27 | "mike" to setOf("andy", "john") 28 | ) 29 | } 30 | 31 | data class AddressFormat(val showZipcode: Boolean? = true, val showState: Boolean? = true) 32 | 33 | data class Student(private val id: String, val name: String, val age: Int, val gender: Gender) { 34 | @Suppress("unused") 35 | suspend fun address(format: AddressFormat? = AddressFormat()): String { 36 | val showZipcode = format?.showZipcode ?: true 37 | val showState = format?.showState ?: true 38 | 39 | return "$name's address, ${if(showState) "CA" else ""}, ${if(showZipcode) "94555" else ""}" 40 | } 41 | 42 | fun friends(): List { 43 | return friendships[id]!!.map { students[it]!! }.toList() 44 | } 45 | } 46 | 47 | @GraphQLDescription("Return students") 48 | suspend fun students( 49 | @GraphQLDescription("Limit of the result") limit: Int = 10 50 | ): List { 51 | return students.values.take(limit) 52 | } 53 | } 54 | 55 | fun main() { 56 | val server = httpServer(8500) { 57 | graphql { 58 | playground = true 59 | 60 | schema { 61 | config { 62 | supportedPackages = listOf("ws.leap.kert.graphql") 63 | } 64 | query(StudentQuery()) 65 | } 66 | } 67 | } 68 | 69 | runBlocking { 70 | server.start() 71 | println("Server started at http://localhost:8500/graphql") 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /kert-graphql/src/test/resources/.graphqlconfig: -------------------------------------------------------------------------------- 1 | { 2 | "_comment": "this is required by Intellij GraphQL plugin", 3 | "name": "Example GraphQL Schema", 4 | "schemaPath": "graphql", 5 | "extensions": { 6 | "endpoints": { 7 | "Endpoint": { 8 | "url": "http://localhost:8500/graphql", 9 | "headers": { 10 | "user-agent": "JS GraphQL" 11 | }, 12 | "introspect": false 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /kert-graphql/src/test/resources/ExampleQuery.graphql: -------------------------------------------------------------------------------- 1 | query StudentAndFriends { 2 | students(limit:2){ 3 | name 4 | age 5 | address(format: {showState:false}) 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /kert-grpc-compiler/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import build.* 2 | 3 | description = "The protoc plugin for Kert" 4 | 5 | configureGrpcPlugin("protoc-gen-grpc-kert") 6 | 7 | dependencies { 8 | testImplementation(project(":kert-grpc")) 9 | testImplementation(libs.protobuf.kotlin) 10 | } 11 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/generator/generator.go: -------------------------------------------------------------------------------- 1 | package generator 2 | 3 | import ( 4 | "google.golang.org/protobuf/proto" 5 | descriptor "google.golang.org/protobuf/types/descriptorpb" 6 | plugin "google.golang.org/protobuf/types/pluginpb" 7 | "io/ioutil" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Generator is the type whose methods generate the output, stored in the associated response structure. 13 | type Generator struct { 14 | Request *plugin.CodeGeneratorRequest // The input. 15 | Response *plugin.CodeGeneratorResponse // The output. 16 | 17 | Param map[string]string // Command-line parameters. 18 | writeOutput bool 19 | writeInput bool 20 | } 21 | 22 | // New creates a new generator and allocates the request and response protobufs. 23 | func New() *Generator { 24 | g := new(Generator) 25 | g.Request = new(plugin.CodeGeneratorRequest) 26 | g.Response = new(plugin.CodeGeneratorResponse) 27 | return g 28 | } 29 | 30 | // CommandLineParameters breaks the comma-separated list of key=value pairs 31 | // in the parameter (a member of the request protobuf) into a key/value map. 32 | // It then sets file name mappings defined by those entries. 33 | func (g *Generator) CommandLineParameters(parameter string) { 34 | g.Param = make(map[string]string) 35 | for _, p := range strings.Split(parameter, ",") { 36 | if i := strings.Index(p, "="); i < 0 { 37 | g.Param[p] = "" 38 | } else { 39 | g.Param[p[0:i]] = p[i+1:] 40 | } 41 | } 42 | 43 | for k, v := range g.Param { 44 | switch k { 45 | case "write_input": 46 | g.writeInput, _ = strconv.ParseBool(v) 47 | default: 48 | // unsupported parameter 49 | } 50 | } 51 | } 52 | 53 | func (g *Generator) WriteInput(data []byte) error { 54 | if !g.writeInput { 55 | return nil 56 | } 57 | 58 | filename := "request.bin" 59 | return ioutil.WriteFile(filename, data, 0644) 60 | } 61 | 62 | // GenerateAllFiles generates the output for all the files we're outputting. 63 | func (g *Generator) GenerateAllFiles() error { 64 | // map of all files 65 | files := make(map[string]*descriptor.FileDescriptorProto) 66 | for _, file := range g.Request.ProtoFile { 67 | files[file.GetName()] = file 68 | } 69 | 70 | // collect all message types 71 | messages := make(map[string]Message) 72 | for _, file := range g.Request.ProtoFile { 73 | for _, msg := range file.MessageType { 74 | messages[file.GetPackage()+"."+msg.GetName()] = Message{ 75 | Package: file.GetPackage(), 76 | Name: msg.GetName(), 77 | JavaName: getJavaName(file, msg), 78 | } 79 | } 80 | } 81 | 82 | for _, fileName := range g.Request.FileToGenerate { 83 | file := files[fileName] 84 | for _, service := range file.Service { 85 | sg := ServiceGenerator{ 86 | file: file, 87 | service: service, 88 | messages: &messages, 89 | } 90 | if err := sg.Generate(); err != nil { 91 | return err 92 | } 93 | 94 | fname := sg.GetFileName() 95 | g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{ 96 | Name: proto.String(fname), 97 | Content: proto.String(sg.String()), 98 | }) 99 | } 100 | } 101 | 102 | return nil 103 | } 104 | 105 | func getJavaName(file *descriptor.FileDescriptorProto, msg *descriptor.DescriptorProto) string { 106 | javaPackage := getJavaPackage(file) 107 | if file.GetOptions().GetJavaMultipleFiles() { 108 | return javaPackage + "." + msg.GetName() 109 | } else { 110 | return javaPackage + "." + getOuterClassName(file) + "." + msg.GetName() 111 | } 112 | } 113 | 114 | func getOuterClassName(file *descriptor.FileDescriptorProto) string { 115 | outer := file.GetOptions().GetJavaOuterClassname() 116 | if len(outer) != 0 { 117 | return outer 118 | } else { 119 | name := strings.Replace(file.GetName(), ".proto", "", -1) 120 | return strings.Title(name) 121 | } 122 | } 123 | 124 | func getJavaPackage(file *descriptor.FileDescriptorProto) string { 125 | pkg := file.GetOptions().GetJavaPackage() 126 | if len(pkg) != 0 { 127 | return pkg 128 | } else { 129 | return file.GetPackage() 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/generator/service-generator.go: -------------------------------------------------------------------------------- 1 | package generator 2 | 3 | import ( 4 | "bytes" 5 | descriptor "google.golang.org/protobuf/types/descriptorpb" 6 | "leap.ws/kert-grpc-compiler/generator/templates" 7 | "path" 8 | "strings" 9 | "text/template" 10 | "unicode" 11 | ) 12 | 13 | const ( 14 | pluginVersion = "0.5" 15 | ) 16 | 17 | type MethodType int32 18 | 19 | const ( 20 | Unary MethodType = 0 21 | ServerStreaming MethodType = 1 22 | ClientStreaming MethodType = 2 23 | BidiStreaming MethodType = 3 24 | ) 25 | 26 | type Message struct { 27 | Name string 28 | JavaName string 29 | Package string 30 | } 31 | 32 | type Method struct { 33 | InputType string 34 | OutputType string 35 | Name string 36 | JavaName string 37 | MethodType MethodType 38 | GrpcMethodType string 39 | Id int 40 | IdName string 41 | FieldName string 42 | } 43 | 44 | type Service struct { 45 | ProtoFile string 46 | ProtoPackage string 47 | ProtoName string 48 | 49 | JavaPackage string 50 | Name string 51 | OuterClassName string 52 | Methods []Method 53 | } 54 | 55 | type ServiceGenerator struct { 56 | *bytes.Buffer 57 | 58 | file *descriptor.FileDescriptorProto 59 | service *descriptor.ServiceDescriptorProto 60 | indent string 61 | messages *map[string]Message 62 | } 63 | 64 | func (g *ServiceGenerator) GetFileName() string { 65 | // get package name 66 | pkg := getJavaPackage(g.file) 67 | parent := strings.Replace(pkg, ".", "/", -1) 68 | name := *g.service.Name + "GrpcKt.kt" 69 | 70 | return path.Join(parent, name) 71 | } 72 | 73 | // Fill the response protocol buffer with the generated output for all the files we're 74 | // supposed to generate. 75 | func (g *ServiceGenerator) Generate() error { 76 | g.Buffer = new(bytes.Buffer) 77 | 78 | service := g.generateTemplateParams() 79 | tmpl, err := template.New("service").Parse(templates.Service) 80 | if err != nil { 81 | return err 82 | } 83 | 84 | if tmpl.Execute(g.Buffer, service) != nil { 85 | return nil 86 | } 87 | 88 | return nil 89 | } 90 | 91 | func (g *ServiceGenerator) generateTemplateParams() *Service { 92 | methods := make([]Method, len(g.service.Method)) 93 | for i, method := range g.service.Method { 94 | methodType := getMethodType(method) 95 | methods[i] = Method{ 96 | Name: method.GetName(), 97 | JavaName: lowerMethodName(method), 98 | InputType: g.javaClassName(method.GetInputType()), 99 | OutputType: g.javaClassName(method.GetOutputType()), 100 | MethodType: methodType, 101 | GrpcMethodType: getGrpcMethodType(methodType), 102 | Id: i, 103 | IdName: methodIdFieldName(method), 104 | FieldName: methodPropertiesFieldName(method), 105 | } 106 | } 107 | 108 | service := Service{ 109 | JavaPackage: getJavaPackage(g.file), 110 | Name: g.service.GetName(), 111 | ProtoFile: g.file.GetName(), 112 | ProtoPackage: g.file.GetPackage(), 113 | ProtoName: g.file.GetPackage() + "." + g.service.GetName(), 114 | OuterClassName: getOuterClassName(g.file), 115 | Methods: methods, 116 | } 117 | 118 | return &service 119 | } 120 | 121 | func (m *Method) FullInputType() string { 122 | switch m.MethodType { 123 | case Unary: 124 | fallthrough 125 | case ServerStreaming: 126 | return m.InputType 127 | case ClientStreaming: 128 | fallthrough 129 | case BidiStreaming: 130 | return "Flow<" + m.InputType + ">" 131 | default: 132 | return "UNKNOWN" 133 | } 134 | } 135 | 136 | func (m *Method) FullOutputType() string { 137 | switch m.MethodType { 138 | case Unary: 139 | fallthrough 140 | case ClientStreaming: 141 | return m.OutputType 142 | case ServerStreaming: 143 | fallthrough 144 | case BidiStreaming: 145 | return "Flow<" + m.OutputType + ">" 146 | default: 147 | return "UNKNOWN" 148 | } 149 | } 150 | 151 | func (m *Method) UnimplementedCall() string { 152 | switch m.MethodType { 153 | case Unary: 154 | fallthrough 155 | case ClientStreaming: 156 | return "unimplementedUnaryCall" 157 | case ServerStreaming: 158 | fallthrough 159 | case BidiStreaming: 160 | return "unimplementedStreamingCall" 161 | default: 162 | return "UNKNOWN" 163 | } 164 | } 165 | 166 | func (m *Method) Call() string { 167 | switch m.MethodType { 168 | case Unary: 169 | return "unaryCall" 170 | case ClientStreaming: 171 | return "clientStreamingCall" 172 | case ServerStreaming: 173 | return "serverStreamingCall" 174 | case BidiStreaming: 175 | return "bidiStreamingCall" 176 | default: 177 | return "UNKNOWN" 178 | } 179 | } 180 | 181 | func (m *Method) CallParams() string { 182 | switch m.MethodType { 183 | case Unary: 184 | fallthrough 185 | case ServerStreaming: 186 | return "req" 187 | case ClientStreaming: 188 | fallthrough 189 | case BidiStreaming: 190 | return "req" 191 | default: 192 | return "UNKNOWN" 193 | } 194 | } 195 | 196 | func getGrpcMethodType(methodType MethodType) string { 197 | switch methodType { 198 | case Unary: 199 | return "UNARY" 200 | case ServerStreaming: 201 | return "SERVER_STREAMING" 202 | case ClientStreaming: 203 | return "CLIENT_STREAMING" 204 | case BidiStreaming: 205 | return "BIDI_STREAMING" 206 | default: 207 | return "UNKNOWN" 208 | } 209 | } 210 | 211 | func getMethodType(method *descriptor.MethodDescriptorProto) MethodType { 212 | clientStreaming := method.GetClientStreaming() 213 | serverStreaming := method.GetServerStreaming() 214 | if clientStreaming { 215 | if serverStreaming { 216 | return BidiStreaming 217 | } else { 218 | return ClientStreaming 219 | } 220 | } else { 221 | if serverStreaming { 222 | return ServerStreaming 223 | } else { 224 | return Unary 225 | } 226 | } 227 | } 228 | 229 | // Adjust a method name prefix identifier to follow the JavaBean spec: 230 | // - decapitalize the first letter 231 | // - remove embedded underscores & capitalize the following letter 232 | func mixedLower(word string) string { 233 | buffer := new(bytes.Buffer) 234 | buffer.WriteRune(unicode.ToLower(rune(word[0]))) 235 | 236 | afterUnderscore := false 237 | for i := 1; i < len(word); i++ { 238 | if word[i] == '_' { 239 | afterUnderscore = true 240 | } else { 241 | if afterUnderscore { 242 | buffer.WriteRune(unicode.ToUpper(rune(word[i]))) 243 | } else { 244 | buffer.WriteByte(word[i]) 245 | } 246 | afterUnderscore = false 247 | } 248 | } 249 | 250 | return buffer.String() 251 | } 252 | 253 | // Converts to the identifier to the ALL_UPPER_CASE format. 254 | // - An underscore is inserted where a lower case letter is followed by an 255 | // upper case letter. 256 | // - All letters are converted to upper case 257 | func toAllUpperCase(word string) string { 258 | buffer := new(bytes.Buffer) 259 | for i := 0; i < len(word); i++ { 260 | buffer.WriteRune(unicode.ToUpper(rune(word[i]))) 261 | if (i < len(word)-1) && unicode.IsLower(rune(word[i])) && unicode.IsUpper(rune(word[i+1])) { 262 | buffer.WriteByte('_') 263 | } 264 | } 265 | return buffer.String() 266 | } 267 | 268 | func lowerMethodName(method *descriptor.MethodDescriptorProto) string { 269 | return mixedLower(method.GetName()) 270 | } 271 | 272 | func methodPropertiesFieldName(method *descriptor.MethodDescriptorProto) string { 273 | return "METHOD_" + toAllUpperCase(method.GetName()) 274 | } 275 | 276 | func methodIdFieldName(method *descriptor.MethodDescriptorProto) string { 277 | return "METHODID_" + toAllUpperCase(method.GetName()) 278 | } 279 | 280 | func (g *ServiceGenerator) javaClassName(messageType string) string { 281 | return (*g.messages)[strings.Trim(messageType, ".")].JavaName 282 | } 283 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/generator/templates/service.go: -------------------------------------------------------------------------------- 1 | package templates 2 | 3 | const ( 4 | Service = ` 5 | {{- with $s := .}} 6 | package {{$s.JavaPackage}} 7 | 8 | import java.net.URL 9 | import kotlinx.coroutines.flow.Flow 10 | import io.grpc.MethodDescriptor.generateFullMethodName 11 | import ws.leap.kert.grpc.combineInterceptors 12 | import ws.leap.kert.grpc.ClientCalls 13 | import ws.leap.kert.grpc.ServerCalls 14 | import ws.leap.kert.http.HttpClient 15 | import ws.leap.kert.grpc.CallOptions 16 | import ws.leap.kert.grpc.AbstractStub 17 | import ws.leap.kert.grpc.BindableService 18 | import ws.leap.kert.grpc.ServerServiceDefinition 19 | import ws.leap.kert.grpc.GrpcInterceptor 20 | import ws.leap.kert.grpc.GrpcUtils 21 | 22 | {{/** 23 | *
 24 |  * Test service that supports all call types.
 25 |  * 
26 | */}} 27 | @javax.annotation.Generated( 28 | value = ["by Kert gRPC proto compiler"], 29 | comments = "Source: {{.ProtoFile}}") 30 | object {{$s.Name}}GrpcKt { 31 | const val SERVICE_NAME = "{{$s.ProtoName}}" 32 | 33 | // Static method descriptors that strictly reflect the proto. 34 | {{- range $i, $m := .Methods}} 35 | @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") 36 | val {{$m.FieldName}}: io.grpc.MethodDescriptor<{{$m.InputType}}, {{$m.OutputType}}> = 37 | io.grpc.MethodDescriptor.newBuilder<{{$m.InputType}}, {{$m.OutputType}}>() 38 | .setType(io.grpc.MethodDescriptor.MethodType.{{$m.GrpcMethodType}}) 39 | .setFullMethodName(generateFullMethodName("{{$s.ProtoName}}", "{{$m.Name}}")) 40 | .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller({{$m.InputType}}.getDefaultInstance())) 41 | .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller({{$m.OutputType}}.getDefaultInstance())) 42 | .build() 43 | {{- end}} 44 | 45 | /** 46 | * Creates a new stub with Client 47 | */ 48 | fun stub(client: HttpClient, callOptions: CallOptions = CallOptions(), interceptors: List = emptyList()): {{.Name}}Stub { 49 | val combinedInterceptor = combineInterceptors(*interceptors.toTypedArray()) 50 | return {{.Name}}Stub(client, callOptions, combinedInterceptor) 51 | } 52 | 53 | {{/** 54 | *
 55 |    * Test service that supports all call types.
 56 |    * 
57 | */}} 58 | interface {{$s.Name}} { 59 | {{range $i, $m := .Methods}} 60 | {{- /** 61 | *
 62 |      * One requestMore followed by one response.
 63 |      * The server returns the client payload as-is.
 64 |      * 
65 | */ -}} 66 | suspend fun {{$m.JavaName}}(req: {{$m.FullInputType}}): {{$m.FullOutputType}} 67 | {{end}} 68 | } 69 | 70 | {{/** 71 | *
 72 |    * Test service that supports all call types.
 73 |    * 
74 | */}} 75 | abstract class {{.Name}}ImplBase : BindableService, {{$s.Name}} { 76 | {{range $i, $m := .Methods}} 77 | {{- /** 78 | *
 79 |      * One requestMore followed by one response.
 80 |      * The server returns the client payload as-is.
 81 |      * 
82 | */ -}} 83 | override suspend fun {{$m.JavaName}}(req: {{$m.FullInputType}}): {{$m.FullOutputType}} { 84 | return ServerCalls.{{$m.UnimplementedCall}}({{$m.FieldName}}) 85 | } 86 | {{end}} 87 | override fun bindService(): ServerServiceDefinition { 88 | return ServerServiceDefinition(serviceDescriptor) { 89 | {{- range $i, $m := .Methods}} 90 | addMethod({{$m.FieldName}}, ServerCalls.{{$m.Call}}(::{{$m.JavaName}})) 91 | {{- end}} 92 | } 93 | } 94 | } 95 | 96 | {{/** 97 | *
 98 |    * Test service that supports all call types.
 99 |    * 
100 | */}} 101 | class {{$s.Name}}Stub internal constructor(client: HttpClient, callOptions: CallOptions, interceptors: GrpcInterceptor?) 102 | : AbstractStub<{{$s.Name}}Stub>(client, callOptions, interceptors), {{$s.Name}} { 103 | {{range $i, $m := .Methods}} 104 | {{- /** 105 | *
106 |      * One requestMore followed by one response.
107 |      * The server returns the client payload as-is.
108 |      * 
109 | */ -}} 110 | override suspend fun {{$m.JavaName}}(req: {{$m.FullInputType}}): {{$m.FullOutputType}} { 111 | return ClientCalls.{{$m.Call}}( 112 | newCall({{$m.FieldName}}, callOptions), {{$m.CallParams}}) 113 | } 114 | {{end}} 115 | 116 | override fun build(client: HttpClient, callOptions: CallOptions, interceptors: GrpcInterceptor?): {{$s.Name}}Stub { 117 | return {{.Name}}Stub(client, callOptions, interceptors) 118 | } 119 | } 120 | 121 | private class {{$s.Name}}DescriptorSupplier : io.grpc.protobuf.ProtoFileDescriptorSupplier { 122 | override fun getFileDescriptor(): com.google.protobuf.Descriptors.FileDescriptor { 123 | return {{$s.JavaPackage}}.{{$s.OuterClassName}}.getDescriptor() 124 | } 125 | } 126 | 127 | val serviceDescriptor: io.grpc.ServiceDescriptor by lazy { 128 | io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME) 129 | .setSchemaDescriptor({{$s.Name}}DescriptorSupplier()) 130 | {{- range $i, $m := .Methods}} 131 | .addMethod({{$m.FieldName}}) 132 | {{- end}} 133 | .build() 134 | } 135 | } 136 | {{- end}} 137 | ` 138 | ) 139 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/go.mod: -------------------------------------------------------------------------------- 1 | module leap.ws/kert-grpc-compiler 2 | 3 | go 1.19 4 | 5 | require google.golang.org/protobuf v1.28.1 6 | require github.com/golang/protobuf v1.5.2 7 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 2 | github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= 3 | github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 4 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 5 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 6 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 7 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 8 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 9 | google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 10 | google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= 11 | google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 12 | google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= 13 | google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 14 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "google.golang.org/protobuf/proto" 5 | "io/ioutil" 6 | "leap.ws/kert-grpc-compiler/generator" 7 | "leap.ws/kert-grpc-compiler/util" 8 | "os" 9 | ) 10 | 11 | func main() { 12 | // Begin by allocating a generator. The request and response structures are stored there 13 | // so we can do error handling easily - the response structure contains the field to 14 | // report failure. 15 | g := generator.New() 16 | 17 | var data []byte = nil 18 | var err error = nil 19 | 20 | if len(os.Args) > 1 { 21 | filename := os.Args[1] 22 | if data, err = ioutil.ReadFile(filename); err != nil { 23 | util.Error(err, "reading input from file") 24 | } 25 | } else { 26 | if data, err = ioutil.ReadAll(os.Stdin); err != nil { 27 | util.Error(err, "reading input") 28 | } 29 | } 30 | 31 | if err := proto.Unmarshal(data, g.Request); err != nil { 32 | util.Error(err, "parsing input proto") 33 | } 34 | 35 | if len(g.Request.FileToGenerate) == 0 { 36 | util.Fail("no files to generate") 37 | } 38 | 39 | g.CommandLineParameters(g.Request.GetParameter()) 40 | 41 | if err = g.WriteInput(data); err != nil { 42 | util.Error(err, "failed to write input data") 43 | } 44 | 45 | if err = g.GenerateAllFiles(); err != nil { 46 | util.Error(err, "failed to generate files") 47 | } 48 | 49 | // Send back the results. 50 | data, err = proto.Marshal(g.Response) 51 | if err != nil { 52 | util.Error(err, "failed to marshal output proto") 53 | } 54 | _, err = os.Stdout.Write(data) 55 | if err != nil { 56 | util.Error(err, "failed to write output proto") 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/main/go/util/util.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "strings" 7 | ) 8 | 9 | // Error reports a problem, including an error, and exits the program. 10 | func Error(err error, msgs ...string) { 11 | s := strings.Join(msgs, " ") + ":" + err.Error() 12 | log.Print("protoc-gen-go: error:", s) 13 | os.Exit(1) 14 | } 15 | 16 | // Fail reports a problem and exits the program. 17 | func Fail(msgs ...string) { 18 | s := strings.Join(msgs, " ") 19 | log.Print("protoc-gen-rxjava: error:", s) 20 | os.Exit(1) 21 | } 22 | -------------------------------------------------------------------------------- /kert-grpc-compiler/src/test/proto/test.proto: -------------------------------------------------------------------------------- 1 | // A simple service definition for testing the protoc plugin. 2 | syntax = "proto3"; 3 | 4 | package grpc.testing; 5 | 6 | option java_package = "io.grpc.testing.integration"; 7 | 8 | import "google/protobuf/empty.proto"; 9 | import "google/protobuf/wrappers.proto"; 10 | 11 | message SimpleRequest { 12 | } 13 | 14 | message SimpleResponse { 15 | } 16 | 17 | message StreamingInputCallRequest { 18 | } 19 | 20 | message StreamingInputCallResponse { 21 | } 22 | 23 | message StreamingOutputCallRequest { 24 | } 25 | 26 | message StreamingOutputCallResponse { 27 | } 28 | 29 | // Test service that supports all call types. 30 | service TestService { 31 | // One requestMore followed by one response. 32 | // The server returns the client payload as-is. 33 | rpc UnaryCall(SimpleRequest) returns (SimpleResponse); 34 | 35 | // One requestMore followed by a sequence of responses (streamed download). 36 | // The server returns the payload with client desired type and sizes. 37 | rpc StreamingOutputCall(StreamingOutputCallRequest) 38 | returns (stream StreamingOutputCallResponse); 39 | 40 | // A sequence of requests followed by one response (streamed upload). 41 | // The server returns the aggregated size of client payload as the result. 42 | rpc StreamingInputCall(stream StreamingInputCallRequest) 43 | returns (StreamingInputCallResponse); 44 | 45 | // A sequence of requests with each requestMore served by the server immediately. 46 | // As one requestMore could lead to multiple responses, this interface 47 | // demonstrates the idea of full bidirectionality. 48 | rpc FullBidiCall(stream StreamingOutputCallRequest) 49 | returns (stream StreamingOutputCallResponse); 50 | 51 | // A sequence of requests followed by a sequence of responses. 52 | // The server buffers all the client requests and then serves them in order. A 53 | // stream of responses are returned to the client when the server starts with 54 | // first requestMore. 55 | rpc HalfBidiCall(stream StreamingOutputCallRequest) 56 | returns (stream StreamingOutputCallResponse); 57 | } 58 | -------------------------------------------------------------------------------- /kert-grpc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import build.* 2 | import com.google.protobuf.gradle.* 3 | 4 | description = "Kert GRPC support" 5 | 6 | configureLibrary() 7 | 8 | dependencies { 9 | api(project(":kert-http")) 10 | api(libs.protobuf.java) 11 | api(libs.protobuf.kotlin) 12 | api(libs.grpc.protobuf) 13 | 14 | api(libs.javax.annotation.api) 15 | 16 | // generateTestProto needs compiler binary 17 | compileOnly(project(":kert-grpc-compiler")) 18 | 19 | testImplementation(libs.grpc.stub) 20 | testImplementation(libs.grpc.netty) 21 | } 22 | 23 | protobuf { 24 | protoc { 25 | artifact = "com.google.protobuf:protoc:${libs.versions.protobuf.get()}" 26 | } 27 | plugins { 28 | id("grpc-kert") { 29 | val osDetector = extensions.getByType(com.google.gradle.osdetector.OsDetector::class) 30 | val exeSuffix = if(osDetector.os == "windows") ".exe" else "" 31 | 32 | path = "$rootDir/kert-grpc-compiler/build/exe/protoc-gen-grpc-kert${exeSuffix}" 33 | } 34 | // generate java version for performance comparison 35 | id("grpc-java") { 36 | artifact = "io.grpc:protoc-gen-grpc-java:${libs.versions.grpc.get()}" 37 | } 38 | } 39 | generateProtoTasks { 40 | // protos used by library self 41 | ofSourceSet("main").forEach { task -> 42 | task.builtins { 43 | id("kotlin") 44 | } 45 | task.plugins { 46 | id("grpc-kert") 47 | } 48 | } 49 | 50 | // protos used in test (grpc-java is enabled for comparison) 51 | ofSourceSet("test").forEach { task -> 52 | task.builtins { 53 | id("kotlin") 54 | } 55 | task.plugins { 56 | id("grpc-kert") 57 | id("grpc-java") 58 | } 59 | } 60 | } 61 | } 62 | 63 | tasks.named("generateProto") { 64 | dependsOn(":kert-grpc-compiler:buildPlugin") 65 | } 66 | -------------------------------------------------------------------------------- /kert-grpc/readme.md: -------------------------------------------------------------------------------- 1 | Performance Test 2 | ```shell script 3 | ghz --insecure -c 100 -z 30s --connections 100 \ 4 | --proto kert-grpc/src/test/proto/echo.proto \ 5 | --call ws.leap.kert.test.Echo.unary \ 6 | -d '{"id":1, "value":"hello"}' \ 7 | 0.0.0.0:8550 8 | ``` 9 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/AbstractStub.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.grpc.Status 5 | import io.vertx.core.buffer.Buffer 6 | import io.vertx.core.http.HttpHeaders 7 | import io.vertx.core.http.HttpVersion 8 | import io.vertx.core.http.impl.headers.HeadersMultiMap 9 | import kotlinx.coroutines.flow.* 10 | import ws.leap.kert.http.HttpClient 11 | 12 | // placeholder, nothing to configure right now 13 | class CallOptions { 14 | 15 | } 16 | 17 | abstract class AbstractStub( 18 | private val client: HttpClient, 19 | protected val callOptions: CallOptions = CallOptions(), 20 | private val interceptors: GrpcInterceptor? = null 21 | ) { 22 | init { 23 | require(client.protocolVersion == HttpVersion.HTTP_2) { 24 | "HTTP client for GRPC must be on HTTP2" 25 | } 26 | } 27 | protected fun newCall(method: MethodDescriptor, 28 | callOptions: CallOptions): GrpcClientCallHandler { 29 | return { requestMessages -> 30 | val handler: GrpcHandler = { m, r -> invokeHttp(m, r) } 31 | val grpcRequest = GrpcRequest(emptyMetadata(), requestMessages) 32 | // TODO bidi streaming stuck when specify GrpcContext, why? 33 | val grpcResponse = //withContext(GrpcContext(method)) { 34 | handle(method, grpcRequest, handler, interceptors) 35 | //} 36 | grpcResponse.messages 37 | } 38 | } 39 | 40 | private suspend fun invokeHttp(method: MethodDescriptor, request: GrpcRequest): GrpcResponse { 41 | val responseDeserializer = GrpcUtils.responseDeserializer(method) 42 | 43 | val httpRequestBody = request.messages.map { msg -> 44 | val buf = GrpcUtils.serializeMessagePacket(msg) 45 | Buffer.buffer(buf) 46 | } 47 | 48 | val httpRequestPath = "/${method.fullMethodName}" 49 | val headers = HeadersMultiMap() 50 | headers.addAll(request.metadata) 51 | headers[HttpHeaders.CONTENT_TYPE] = Constants.contentTypeGrpcProto 52 | 53 | val httpResponse = client.post(httpRequestPath, headers = headers, body = httpRequestBody) 54 | if (httpResponse.statusCode != 200) { 55 | throw IllegalStateException("GRPC call failed, status=${httpResponse.statusCode}") 56 | } 57 | 58 | val responseMessages = GrpcUtils.readMessages(httpResponse.body, responseDeserializer) 59 | val responseMessagesFlow = responseMessages.onCompletion { cause -> 60 | if (cause == null) { 61 | val trailers = httpResponse.trailers() 62 | // fail the flow if grpc-status is missing, it should be either in headers or trailers 63 | val statusCode = httpResponse.headers[Constants.grpcStatus]?.toInt() ?: trailers[Constants.grpcStatus]?.toInt() 64 | ?: throw IllegalStateException("GRPC status is missing, request=$httpRequestPath") 65 | val status = Status.fromCodeValue(statusCode) 66 | if (!status.isOk) { 67 | val message = httpResponse.headers[Constants.grpcMessage] ?: trailers[Constants.grpcMessage] ?: "" 68 | throw status.withDescription(message).asException() 69 | } 70 | } 71 | } 72 | 73 | return GrpcResponse(httpResponse.headers, responseMessagesFlow) 74 | } 75 | 76 | fun intercepted(vararg interceptors: GrpcInterceptor): S { 77 | if (interceptors.isEmpty()) return this as S 78 | 79 | val combinedInterceptor = combineInterceptors(*interceptors)!! 80 | return intercepted(combinedInterceptor) 81 | } 82 | 83 | fun intercepted(interceptor: GrpcInterceptor): S { 84 | // TODO inherit current interceptors or not?? 85 | return build(client, callOptions, combineInterceptors(interceptors, interceptor)) 86 | } 87 | 88 | /** 89 | * Create a new stub. 90 | */ 91 | protected abstract fun build(client: HttpClient, callOptions: CallOptions = CallOptions(), interceptors: GrpcInterceptor? = null): S 92 | } 93 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ClientCalls.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import kotlinx.coroutines.Deferred 4 | import kotlinx.coroutines.flow.Flow 5 | import kotlinx.coroutines.flow.flowOf 6 | import kotlinx.coroutines.flow.single 7 | 8 | object ClientCalls { 9 | 10 | /** 11 | * Executes a unary call with a response. 12 | */ 13 | suspend fun unaryCall( 14 | call: GrpcClientCallHandler, 15 | req: REQ): RESP { 16 | val responses = call(flowOf(req)) 17 | return responses.single() 18 | } 19 | 20 | /** 21 | * Executes a server-streaming call with a response [Flow]. 22 | */ 23 | suspend fun serverStreamingCall( 24 | call: GrpcClientCallHandler, 25 | req: REQ): Flow { 26 | return call(flowOf(req)) 27 | } 28 | 29 | /** 30 | * Executes a client-streaming call by sending a [Flow] and returns a [Deferred] 31 | * 32 | * @return requestMore stream observer. 33 | */ 34 | suspend fun clientStreamingCall( 35 | call: GrpcClientCallHandler, 36 | req: Flow 37 | ): RESP { 38 | val responses = call(req) 39 | return responses.single() 40 | } 41 | 42 | /** 43 | * Executes a bidi-streaming call. 44 | * 45 | * @return requestMore stream observer. 46 | */ 47 | suspend fun bidiStreamingCall( 48 | call: GrpcClientCallHandler, 49 | req: Flow): Flow { 50 | return call(req) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/Constants.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | object Constants { 4 | // GRPC message header size (1 byte compression flag + 4 bytes size) 5 | const val messageHeaderSize = 5 6 | const val grpcStatus = "grpc-status" 7 | const val grpcMessage = "grpc-message" 8 | val contentTypeGrpcProto = "application/grpc" 9 | val contentTypeGrpcJson = "application/grpc+json" 10 | val contentTypeGrpcWeb = "application/grpc+web" 11 | } 12 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/GrpcUtils.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import com.google.protobuf.AbstractMessage 4 | import io.grpc.MethodDescriptor 5 | import io.netty.buffer.* 6 | import io.vertx.core.MultiMap 7 | import io.vertx.core.buffer.Buffer 8 | import io.vertx.core.streams.WriteStream 9 | import kotlinx.coroutines.flow.* 10 | 11 | object GrpcUtils { 12 | suspend fun writeMessages(stream: WriteStream, messages: Flow, serializer: (T) -> ByteBuf) { 13 | messages.collect { msg -> 14 | val buf = serializer(msg) 15 | 16 | // stream.writeByte(0) // compressed flag 17 | // stream.writeInt(buf.readableBytes()) // message size 18 | // stream.writeFully(ByteBufUtil.getBytes(buf)) // message bytes 19 | } 20 | } 21 | 22 | suspend fun readMessages(stream: Flow, deserializer: (ByteBuf, Int) -> T): Flow { 23 | // accumulate bytes in the buffer 24 | val accuBuffer = Unpooled.buffer() 25 | 26 | return flow { 27 | stream.collect { data -> 28 | accuBuffer.discardReadBytes() 29 | accuBuffer.writeBytes(data.byteBuf) 30 | 31 | while(true) { 32 | val msg = readMessage(accuBuffer, deserializer) ?: break 33 | emit(msg) 34 | } 35 | } 36 | } 37 | } 38 | 39 | private fun readMessage(buf: ByteBuf, deserializer: (ByteBuf, Int) -> T): T? { 40 | if(buf.readableBytes() < Constants.messageHeaderSize) return null 41 | 42 | val slice = buf.slice() // create a slice so read won't change reader position 43 | val compressedFlag = slice.readUnsignedByte() 44 | val messageSize = slice.readUnsignedInt() 45 | 46 | // there is no complete message in buffer, return null 47 | if (slice.readableBytes() < messageSize) return null 48 | 49 | // move the reader index to consume the GRPC message header 50 | buf.readerIndex(buf.readerIndex() + Constants.messageHeaderSize) 51 | 52 | // TODO message compression is not supported yet 53 | if (compressedFlag == 1.toShort()) throw UnsupportedOperationException("Compression is not supported yet") 54 | 55 | return deserializer(buf, messageSize.toInt()) 56 | } 57 | 58 | fun serializeMessagePacket(message: M): ByteBuf { 59 | require(message is AbstractMessage) 60 | val buf = Unpooled.buffer() 61 | buf.writeByte(0) 62 | buf.writeInt(message.serializedSize) 63 | serialize(message, buf) 64 | return buf 65 | } 66 | 67 | private fun serialize(message: AbstractMessage, buf: ByteBuf) { 68 | val out = ByteBufOutputStream(buf) 69 | message.writeTo(out) 70 | } 71 | 72 | fun requestSerializer(method: MethodDescriptor): (ReqT) -> ByteBuf { 73 | return { msg: ReqT -> 74 | val msgStream = method.streamRequest(msg) 75 | val buf = Unpooled.buffer() 76 | buf.writeBytes(msgStream, 1024) // TODO how to get the actual stream size 77 | buf 78 | } 79 | } 80 | 81 | fun responseSerializer(method: MethodDescriptor<*, RespT>): (RespT) -> ByteBuf { 82 | return { msg: RespT -> 83 | val msgStream = method.streamResponse(msg) 84 | val buf = Unpooled.buffer() 85 | // TODO bad performance 86 | buf.writeBytes(msgStream, 1024) // TODO how to get the actual stream size 87 | buf 88 | } 89 | } 90 | 91 | fun requestDeserializer(method: MethodDescriptor): (ByteBuf, Int) -> ReqT { 92 | return { buf: ByteBuf, size: Int -> 93 | val inStream = ByteBufInputStream(buf, size) 94 | method.parseRequest(inStream) 95 | } 96 | } 97 | 98 | fun responseDeserializer(method: MethodDescriptor<*, RespT>): (ByteBuf, Int) -> RespT { 99 | return { buf: ByteBuf, size: Int -> 100 | val inStream = ByteBufInputStream(buf, size) 101 | method.parseResponse(inStream) 102 | } 103 | } 104 | } 105 | 106 | fun emptyMetadata(): MultiMap = MultiMap.caseInsensitiveMultiMap() 107 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/Server.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.Status 4 | import io.vertx.core.buffer.Buffer 5 | import io.vertx.core.http.HttpHeaders 6 | import io.vertx.core.http.HttpMethod 7 | import io.vertx.core.http.HttpVersion 8 | import io.vertx.core.http.impl.headers.HeadersMultiMap 9 | import kotlinx.coroutines.CoroutineExceptionHandler 10 | import kotlinx.coroutines.flow.Flow 11 | import kotlinx.coroutines.flow.map 12 | import kotlinx.coroutines.withContext 13 | import mu.KotlinLogging 14 | import ws.leap.kert.http.* 15 | 16 | private val grpcExceptionLogger = KotlinLogging.logger {} 17 | val defaultGrpcExceptionHandler = CoroutineExceptionHandler { context, exception -> 18 | val routingContext = context[VertxRoutingContext]?.routingContext 19 | ?: throw IllegalStateException("Routing context is not available on coroutine context") 20 | 21 | val method = routingContext.request().path().removePrefix("/") 22 | grpcExceptionLogger.warn("GRPC call failed: method=$method", exception) 23 | 24 | val response = routingContext.response() 25 | if (!response.ended()) { 26 | try { 27 | // grpc-status and grpc-message trailers 28 | val status = Status.fromThrowable(exception) 29 | val message = status.description 30 | 31 | // if headers haven't been sent, set grpc status in header 32 | if (!response.headWritten()) { 33 | response.putHeader(HttpHeaders.CONTENT_TYPE, Constants.contentTypeGrpcProto) 34 | response.putHeader(Constants.grpcStatus, status.code.value().toString()) 35 | message?.let { response.putHeader(Constants.grpcMessage, it) } 36 | } else { 37 | // headers have been sent, put grpc status in trailers 38 | response.putTrailer(Constants.grpcStatus, status.code.value().toString()) 39 | message?.let { response.putTrailer(Constants.grpcMessage, it) } 40 | } 41 | } finally { 42 | response.end() 43 | } 44 | } 45 | } 46 | 47 | fun HttpServerBuilderDsl.grpc(configure: GrpcServerBuilder.() -> Unit) { 48 | router(defaultGrpcExceptionHandler) { 49 | val builder = GrpcServerBuilder(this) 50 | configure(builder) 51 | builder.build() 52 | } 53 | } 54 | 55 | // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md 56 | class GrpcServerBuilder(private val httpRouterBuilder: HttpRouterDsl) { 57 | var serverReflection: Boolean = false 58 | 59 | private val registry = ServiceRegistry() 60 | private val interceptors = mutableListOf() 61 | 62 | fun service(service: ServerServiceDefinition): Unit = registry.addService(service) 63 | fun service(service: BindableService): Unit = registry.addService(service) 64 | 65 | fun interceptor(interceptor: GrpcInterceptor) { 66 | interceptors.add(interceptor) 67 | } 68 | 69 | fun build() { 70 | if(serverReflection) { 71 | service(ServerReflectionImpl(registry)) 72 | } 73 | 74 | val finalInterceptor = combineInterceptors(*interceptors.toTypedArray()) 75 | 76 | for(service in registry.services()) { 77 | httpRouterBuilder.call(HttpMethod.POST, "/${service.serviceDescriptor.name}/:method") { req -> 78 | // get method from url 79 | val methodName = req.pathParams["method"] ?: throw IllegalArgumentException("method is not provided") 80 | val method = registry.lookupMethod("${service.serviceDescriptor.name}/${methodName}") 81 | if (method != null) { 82 | // TODO the context of exceptionHandler doesn't have GrpcContext 83 | withContext(GrpcContext(method.methodDescriptor)) { 84 | handleRequest(req, method, finalInterceptor) 85 | } 86 | } else { 87 | notFound() 88 | } 89 | } 90 | } 91 | } 92 | 93 | private fun notFound(): HttpServerResponse { 94 | return response( 95 | contentType = Constants.contentTypeGrpcProto, 96 | trailers = { HeadersMultiMap().add(Constants.grpcStatus, Status.NOT_FOUND.code.value().toString()) } 97 | ) 98 | } 99 | 100 | private suspend fun handleRequest(request: HttpServerRequest, method: ServerMethodDefinition, 101 | interceptors: GrpcInterceptor?): HttpServerResponse { 102 | verifyRequest(request) 103 | 104 | val requestDeserializer = GrpcUtils.requestDeserializer(method.methodDescriptor) 105 | val requestMessages = GrpcUtils.readMessages(request.body, requestDeserializer) 106 | val grpcRequest = GrpcRequest(request.headers, requestMessages) 107 | val grpcResponse = handle(method.methodDescriptor, grpcRequest, method.handler, interceptors) 108 | 109 | val httpBody: Flow = grpcResponse.messages.map { msg -> 110 | val buf = GrpcUtils.serializeMessagePacket(msg) 111 | Buffer.buffer(buf) 112 | } 113 | return response( 114 | headers = grpcResponse.metadata, 115 | body = httpBody, 116 | contentType = Constants.contentTypeGrpcProto, 117 | trailers = { HeadersMultiMap().add(Constants.grpcStatus, Status.OK.code.value().toString()) } 118 | ) 119 | } 120 | 121 | private fun verifyRequest(request: HttpServerRequest) { 122 | require(request.version == HttpVersion.HTTP_2) { "GRPC must be HTTP2, current is ${request.version}" } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ServerCalls.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.grpc.Status 5 | import kotlinx.coroutines.flow.* 6 | 7 | /** 8 | * Utility functions for adapting [GrpcServerCallHandler]s to application service implementation, 9 | * meant to be used by the generated code. 10 | */ 11 | object ServerCalls { 12 | 13 | /** 14 | * Creates a `ServerCallHandler` for a unary call method of the service. 15 | * 16 | * @param method an adaptor to the actual method on the service implementation. 17 | */ 18 | fun unaryCall(method: suspend (REQ) -> RESP): GrpcServerHandler { 19 | return { _, req -> 20 | val msg = req.messages.single() 21 | GrpcResponse(emptyMetadata(), flowOf(method(msg))) 22 | } 23 | } 24 | 25 | /** 26 | * Creates a `ServerCallHandler` for a server streaming method of the service. 27 | * 28 | * @param method an adaptor to the actual method on the service implementation. 29 | */ 30 | fun serverStreamingCall(method: suspend (REQ) -> Flow): GrpcServerHandler { 31 | return { _, req -> 32 | val msg = req.messages.single() 33 | GrpcResponse(emptyMetadata(), method(msg)) 34 | } 35 | } 36 | 37 | /** 38 | * Creates a `ServerCallHandler` for a client streaming method of the service. 39 | * 40 | * @param method an adaptor to the actual method on the service implementation. 41 | */ 42 | fun clientStreamingCall(method: suspend(Flow) -> RESP): GrpcServerHandler { 43 | return { _, req -> 44 | val resp = method(req.messages) 45 | GrpcResponse(emptyMetadata(), flowOf(resp)) 46 | } 47 | } 48 | 49 | fun bidiStreamingCall(method: suspend (Flow) -> Flow): GrpcServerHandler { 50 | return { _, req -> 51 | val resp = method(req.messages) 52 | GrpcResponse(emptyMetadata(), resp) 53 | } 54 | } 55 | 56 | fun unimplementedUnaryCall( 57 | methodDescriptor: MethodDescriptor<*, *>): T { 58 | throw Status.UNIMPLEMENTED 59 | .withDescription("Method ${methodDescriptor.fullMethodName} is unimplemented") 60 | .asRuntimeException() 61 | } 62 | 63 | fun unimplementedStreamingCall(methodDescriptor: MethodDescriptor<*, *>): Flow { 64 | throw Status.UNIMPLEMENTED 65 | .withDescription("Method ${methodDescriptor.fullMethodName} is unimplemented") 66 | .asRuntimeException() 67 | } 68 | 69 | private fun getStatus(t: Throwable): Status { 70 | val status = Status.fromThrowable(t) 71 | return if (status.description == null) { 72 | status.withDescription(t.message) 73 | } else { 74 | status 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ServerMethodDefinition.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | 5 | data class ServerMethodDefinition( 6 | /** The `MethodDescriptor` for this method. */ 7 | val methodDescriptor: MethodDescriptor, 8 | /** Handler for incoming calls. */ 9 | val handler: GrpcHandler 10 | ) { 11 | fun intercepted(interceptor: GrpcInterceptor): ServerMethodDefinition { 12 | return copy(handler = handler.intercepted(interceptor)) 13 | } 14 | 15 | fun intercepted(vararg interceptors: GrpcInterceptor): ServerMethodDefinition { 16 | if (interceptors.isEmpty()) return this 17 | 18 | val combinedInterceptor = combineInterceptors(*interceptors)!! 19 | return intercepted(combinedInterceptor) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ServerReflectionImpl.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import com.google.protobuf.Descriptors.* 4 | import grpc.reflection.v1alpha.* 5 | import grpc.reflection.v1alpha.Reflection.* 6 | import io.grpc.Status 7 | import io.grpc.protobuf.ProtoFileDescriptorSupplier 8 | import kotlinx.coroutines.flow.Flow 9 | import kotlinx.coroutines.flow.collect 10 | import kotlinx.coroutines.flow.flow 11 | import java.util.* 12 | import kotlin.collections.set 13 | 14 | class ServerReflectionImpl(private val registry: ServiceRegistry) : ServerReflectionGrpcKt.ServerReflectionImplBase() { 15 | private val serverReflectionIndex: ServerReflectionIndex by lazy { 16 | ServerReflectionIndex(registry.services(), emptyList()) 17 | 18 | // TODO handle mutable services (probably don't need it) 19 | // val serverFileDescriptors: MutableSet = HashSet() 20 | // val serverServiceNames: MutableSet = HashSet() 21 | // val serverMutableServices: List = server.getMutableServices() 22 | // for (mutableService in serverMutableServices) { 23 | // val serviceDescriptor = mutableService.serviceDescriptor 24 | // if (serviceDescriptor.schemaDescriptor is ProtoFileDescriptorSupplier) { 25 | // val serviceName = serviceDescriptor.name 26 | // val fileDescriptor = (serviceDescriptor.schemaDescriptor as ProtoFileDescriptorSupplier?) 27 | // .getFileDescriptor() 28 | // serverFileDescriptors.add(fileDescriptor) 29 | // serverServiceNames.add(serviceName) 30 | // } 31 | // } 32 | // 33 | // // Replace the index if the underlying mutable services have changed. Check both the file 34 | // // descriptors and the service names, because one file descriptor can define multiple 35 | // // services. 36 | // 37 | // // Replace the index if the underlying mutable services have changed. Check both the file 38 | // // descriptors and the service names, because one file descriptor can define multiple 39 | // // services. 40 | // val mutableServicesIndex: FileDescriptorIndex = index.getMutableServicesIndex() 41 | // if (mutableServicesIndex.getServiceFileDescriptors() != serverFileDescriptors 42 | // || mutableServicesIndex.getServiceNames() != serverServiceNames 43 | // ) { 44 | // index = ServerReflectionIndex(server.getImmutableServices(), serverMutableServices) 45 | // serverReflectionIndexes.put(server, index) 46 | // } 47 | // 48 | // return index 49 | } 50 | 51 | override suspend fun serverReflectionInfo(req: Flow): Flow { 52 | return flow { 53 | req.collect { msg -> 54 | val resp = when(msg.messageRequestCase) { 55 | ServerReflectionRequest.MessageRequestCase.FILE_BY_FILENAME -> getFileByName(msg) 56 | ServerReflectionRequest.MessageRequestCase.FILE_CONTAINING_SYMBOL -> getFileContainingSymbol(msg) 57 | ServerReflectionRequest.MessageRequestCase.FILE_CONTAINING_EXTENSION -> getFileContainingExtension(msg) 58 | ServerReflectionRequest.MessageRequestCase.ALL_EXTENSION_NUMBERS_OF_TYPE -> getAllExtensions(msg) 59 | ServerReflectionRequest.MessageRequestCase.LIST_SERVICES -> listServices(msg) 60 | else -> throw Status.UNIMPLEMENTED.withDescription("${msg.messageRequestCase} is not implemented").asException() 61 | } 62 | 63 | emit(resp) 64 | } 65 | } 66 | } 67 | 68 | private fun getFileByName(req: ServerReflectionRequest): ServerReflectionResponse { 69 | val name = req.fileByFilename 70 | val fd = serverReflectionIndex.getFileDescriptorByName(name) 71 | ?: throw Status.NOT_FOUND.withDescription("File $name is not found.").asException() 72 | return createServerReflectionResponse(req, fd) 73 | } 74 | 75 | private fun getFileContainingSymbol(req: ServerReflectionRequest): ServerReflectionResponse { 76 | val symbol = req.fileContainingSymbol 77 | val fd = serverReflectionIndex.getFileDescriptorBySymbol(symbol) 78 | ?: throw Status.NOT_FOUND.withDescription("Symbol $symbol is not found.").asException() 79 | return createServerReflectionResponse(req, fd) 80 | } 81 | 82 | private fun getFileContainingExtension(req: ServerReflectionRequest): ServerReflectionResponse { 83 | val extensionRequest = req.fileContainingExtension 84 | val type = extensionRequest.containingType 85 | val extension = extensionRequest.extensionNumber 86 | val fd = serverReflectionIndex.getFileDescriptorByExtensionAndNumber(type, extension) 87 | ?: throw Status.NOT_FOUND.withDescription("Extension $type/$extension is not found.").asException() 88 | return createServerReflectionResponse(req, fd) 89 | } 90 | 91 | private fun getAllExtensions(req: ServerReflectionRequest): ServerReflectionResponse { 92 | val type = req.allExtensionNumbersOfType 93 | val extensions = serverReflectionIndex.getExtensionNumbersOfType(type) 94 | ?: throw Status.NOT_FOUND.withDescription("Type $type is not found.").asException() 95 | 96 | return serverReflectionResponse { 97 | validHost = req.host 98 | originalRequest = req 99 | allExtensionNumbersResponse = extensionNumberResponse { 100 | baseTypeName = type 101 | extensionNumber.addAll(extensions) 102 | } 103 | } 104 | } 105 | 106 | private fun listServices(req: ServerReflectionRequest): ServerReflectionResponse { 107 | return serverReflectionResponse { 108 | validHost = req.host 109 | originalRequest = req 110 | listServicesResponse = listServiceResponse { 111 | service.addAll(serverReflectionIndex.serviceNames.map { serviceName -> 112 | serviceResponse { 113 | name = serviceName 114 | } 115 | }) 116 | } 117 | } 118 | } 119 | 120 | private fun createServerReflectionResponse( 121 | request: ServerReflectionRequest, fd: FileDescriptor 122 | ): ServerReflectionResponse { 123 | val fdRBuilder: FileDescriptorResponse.Builder = FileDescriptorResponse.newBuilder() 124 | val seenFiles: MutableSet = HashSet() 125 | val frontier: Queue = ArrayDeque() 126 | seenFiles.add(fd.name) 127 | frontier.add(fd) 128 | while (!frontier.isEmpty()) { 129 | val nextFd = frontier.remove() 130 | fdRBuilder.addFileDescriptorProto(nextFd.toProto().toByteString()) 131 | for (dependencyFd in nextFd.dependencies) { 132 | if (!seenFiles.contains(dependencyFd.name)) { 133 | seenFiles.add(dependencyFd.name) 134 | frontier.add(dependencyFd) 135 | } 136 | } 137 | } 138 | return ServerReflectionResponse.newBuilder() 139 | .setValidHost(request.host) 140 | .setOriginalRequest(request) 141 | .setFileDescriptorResponse(fdRBuilder) 142 | .build() 143 | } 144 | 145 | 146 | private class ServerReflectionIndex( 147 | immutableServices: List, 148 | mutableServices: List 149 | ) { 150 | private val immutableServicesIndex: FileDescriptorIndex 151 | private val mutableServicesIndex: FileDescriptorIndex 152 | 153 | init { 154 | immutableServicesIndex = FileDescriptorIndex(immutableServices) 155 | mutableServicesIndex = FileDescriptorIndex(mutableServices) 156 | } 157 | 158 | val serviceNames: Set 159 | get() { 160 | val immutableServiceNames = immutableServicesIndex.getServiceNames() 161 | val mutableServiceNames = mutableServicesIndex.getServiceNames() 162 | val serviceNames: MutableSet = HashSet(immutableServiceNames.size + mutableServiceNames.size) 163 | serviceNames.addAll(immutableServiceNames) 164 | serviceNames.addAll(mutableServiceNames) 165 | return serviceNames 166 | } 167 | 168 | fun getFileDescriptorByName(name: String): FileDescriptor? { 169 | var fd: FileDescriptor? = immutableServicesIndex.getFileDescriptorByName(name) 170 | if (fd == null) { 171 | fd = mutableServicesIndex.getFileDescriptorByName(name) 172 | } 173 | return fd 174 | } 175 | 176 | fun getFileDescriptorBySymbol(symbol: String): FileDescriptor? { 177 | var fd: FileDescriptor? = immutableServicesIndex.getFileDescriptorBySymbol(symbol) 178 | if (fd == null) { 179 | fd = mutableServicesIndex.getFileDescriptorBySymbol(symbol) 180 | } 181 | return fd 182 | } 183 | 184 | fun getFileDescriptorByExtensionAndNumber(type: String, extension: Int): FileDescriptor? { 185 | var fd: FileDescriptor? = immutableServicesIndex.getFileDescriptorByExtensionAndNumber(type, extension) 186 | if (fd == null) { 187 | fd = mutableServicesIndex.getFileDescriptorByExtensionAndNumber(type, extension) 188 | } 189 | return fd 190 | } 191 | 192 | fun getExtensionNumbersOfType(type: String): Set? { 193 | var extensionNumbers = immutableServicesIndex.getExtensionNumbersOfType(type) 194 | if (extensionNumbers == null) { 195 | extensionNumbers = mutableServicesIndex.getExtensionNumbersOfType(type) 196 | } 197 | return extensionNumbers 198 | } 199 | } 200 | 201 | /** 202 | * Provides a set of methods for answering reflection queries for the file descriptors underlying 203 | * a set of services. Used by [ServerReflectionIndex] to separately index immutable and 204 | * mutable services. 205 | */ 206 | private class FileDescriptorIndex(services: List) { 207 | private val serviceNames: MutableSet = HashSet() 208 | private val serviceFileDescriptors: MutableSet = HashSet() 209 | private val fileDescriptorsByName: MutableMap = HashMap() 210 | private val fileDescriptorsBySymbol: MutableMap = HashMap() 211 | private val fileDescriptorsByExtensionAndNumber: MutableMap> = HashMap() 212 | 213 | init { 214 | val fileDescriptorsToProcess: Queue = ArrayDeque() 215 | val seenFiles: MutableSet = HashSet() 216 | for (service in services) { 217 | val serviceDescriptor = service.serviceDescriptor 218 | if (serviceDescriptor.schemaDescriptor is ProtoFileDescriptorSupplier) { 219 | val fileDescriptor = (serviceDescriptor.schemaDescriptor as ProtoFileDescriptorSupplier).fileDescriptor 220 | val serviceName = serviceDescriptor.name 221 | require(!serviceNames.contains(serviceName)) { "Service already defined: $serviceName" } 222 | serviceFileDescriptors.add(fileDescriptor) 223 | serviceNames.add(serviceName) 224 | if (!seenFiles.contains(fileDescriptor.name)) { 225 | seenFiles.add(fileDescriptor.name) 226 | fileDescriptorsToProcess.add(fileDescriptor) 227 | } 228 | } 229 | } 230 | 231 | while (!fileDescriptorsToProcess.isEmpty()) { 232 | val currentFd = fileDescriptorsToProcess.remove() 233 | processFileDescriptor(currentFd) 234 | for (dependencyFd in currentFd.dependencies) { 235 | if (!seenFiles.contains(dependencyFd.name)) { 236 | seenFiles.add(dependencyFd.name) 237 | fileDescriptorsToProcess.add(dependencyFd) 238 | } 239 | } 240 | } 241 | } 242 | 243 | /** 244 | * Returns the file descriptors for the indexed services, but not their dependencies. This is 245 | * used to check if the server's mutable services have changed. 246 | */ 247 | private fun getServiceFileDescriptors(): Set { 248 | return Collections.unmodifiableSet(serviceFileDescriptors) 249 | } 250 | 251 | fun getServiceNames(): Set { 252 | return Collections.unmodifiableSet(serviceNames) 253 | } 254 | 255 | fun getFileDescriptorByName(name: String): FileDescriptor? { 256 | return fileDescriptorsByName[name] 257 | } 258 | 259 | fun getFileDescriptorBySymbol(symbol: String): FileDescriptor? { 260 | return fileDescriptorsBySymbol[symbol] 261 | } 262 | 263 | fun getFileDescriptorByExtensionAndNumber(type: String, number: Int): FileDescriptor? { 264 | return if (fileDescriptorsByExtensionAndNumber.containsKey(type)) { 265 | fileDescriptorsByExtensionAndNumber[type]!![number] 266 | } else null 267 | } 268 | 269 | fun getExtensionNumbersOfType(type: String): Set? { 270 | return if (fileDescriptorsByExtensionAndNumber.containsKey(type)) { 271 | Collections.unmodifiableSet(fileDescriptorsByExtensionAndNumber[type]!!.keys) 272 | } else null 273 | } 274 | 275 | private fun processFileDescriptor(fd: FileDescriptor) { 276 | val fdName: String = fd.name 277 | require(!fileDescriptorsByName.containsKey(fdName)) { "File name already used: $fdName" } 278 | fileDescriptorsByName[fdName] = fd 279 | for (service in fd.services) { 280 | processService(service, fd) 281 | } 282 | for (type in fd.messageTypes) { 283 | processType(type, fd) 284 | } 285 | for (extension in fd.extensions) { 286 | processExtension(extension, fd) 287 | } 288 | } 289 | 290 | private fun processService(service: ServiceDescriptor, fd: FileDescriptor) { 291 | val serviceName: String = service.fullName 292 | require(!fileDescriptorsBySymbol.containsKey(serviceName)) { "Service already defined: $serviceName" } 293 | fileDescriptorsBySymbol[serviceName] = fd 294 | for (method in service.methods) { 295 | val methodName: String = method.fullName 296 | require(!fileDescriptorsBySymbol.containsKey(methodName)) { "Method already defined: $methodName" } 297 | fileDescriptorsBySymbol[methodName] = fd 298 | } 299 | } 300 | 301 | private fun processType(type: Descriptor, fd: FileDescriptor) { 302 | val typeName: String = type.fullName 303 | require(!fileDescriptorsBySymbol.containsKey(typeName)) { "Type already defined: $typeName" } 304 | fileDescriptorsBySymbol[typeName] = fd 305 | for (extension in type.extensions) { 306 | processExtension(extension, fd) 307 | } 308 | for (nestedType in type.nestedTypes) { 309 | processType(nestedType, fd) 310 | } 311 | } 312 | 313 | private fun processExtension(extension: FieldDescriptor, fd: FileDescriptor) { 314 | val extensionName: String = extension.containingType.fullName 315 | val extensionNumber: Int = extension.number 316 | if (!fileDescriptorsByExtensionAndNumber.containsKey(extensionName)) { 317 | fileDescriptorsByExtensionAndNumber[extensionName] = HashMap() 318 | } 319 | require(!fileDescriptorsByExtensionAndNumber[extensionName]!!.containsKey(extensionNumber)) { 320 | "Extension name and number already defined: $extensionName, $extensionNumber" 321 | } 322 | fileDescriptorsByExtensionAndNumber[extensionName]!![extensionNumber] = fd 323 | } 324 | } 325 | } 326 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ServerServiceDefinition.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.grpc.ServiceDescriptor 5 | import java.lang.IllegalArgumentException 6 | 7 | interface BindableService { 8 | /** 9 | * Creates [ServerServiceDefinition] object for current instance of service implementation. 10 | * 11 | * @return ServerServiceDefinition object. 12 | */ 13 | fun bindService(): ServerServiceDefinition 14 | } 15 | 16 | class ServerServiceMethodsBuilder { 17 | private val methods = mutableMapOf>() 18 | 19 | fun addMethod(method: MethodDescriptor, handler: GrpcHandler) { 20 | methods[method.fullMethodName] = ServerMethodDefinition(method, handler) 21 | } 22 | 23 | fun build(): Map> = methods.toMap() 24 | } 25 | 26 | /** Definition of a service to be exposed via a Server. */ 27 | data class ServerServiceDefinition( 28 | val serviceDescriptor: ServiceDescriptor, 29 | private val methods: Map> 30 | ) { 31 | constructor(serviceDescriptor: ServiceDescriptor, configure: ServerServiceMethodsBuilder.() -> Unit) : 32 | this(serviceDescriptor, kotlin.run { 33 | val builder = ServerServiceMethodsBuilder() 34 | configure(builder) 35 | builder.build() 36 | }) 37 | 38 | /** 39 | * Gets all the methods of service. 40 | */ 41 | fun methods(): Collection> { 42 | return methods.values 43 | } 44 | 45 | /** 46 | * Look up a method by its fully qualified name. 47 | * 48 | * @param methodName the fully qualified name without leading slash. E.g., "com.foo.Foo/Bar" 49 | */ 50 | fun method(methodName: String): ServerMethodDefinition<*, *> { 51 | return methods[methodName] ?: throw IllegalArgumentException("Method $methodName is not found") 52 | } 53 | 54 | fun intercepted(interceptor: GrpcInterceptor): ServerServiceDefinition { 55 | val interceptedMethods = methods.mapValues { it.value.intercepted(interceptor) } 56 | return ServerServiceDefinition(serviceDescriptor, interceptedMethods) 57 | } 58 | 59 | fun intercepted(vararg interceptors: GrpcInterceptor): ServerServiceDefinition { 60 | if (interceptors.isEmpty()) return this 61 | 62 | val combinedInterceptor = combineInterceptors(*interceptors)!! 63 | return intercepted(combinedInterceptor) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/ServiceRegistry.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | class ServiceRegistry { 4 | private val services = mutableMapOf() 5 | private val methods = mutableMapOf>() 6 | 7 | fun addService(service: ServerServiceDefinition) { 8 | services[service.serviceDescriptor.name] = service 9 | for(method in service.methods()) { 10 | methods[method.methodDescriptor.fullMethodName] = method 11 | } 12 | } 13 | 14 | fun addService(service: BindableService) { 15 | addService(service.bindService()) 16 | } 17 | 18 | fun services(): List = services.values.toList() 19 | fun lookupMethod(methodName: String): ServerMethodDefinition<*, *>? = methods[methodName] 20 | } 21 | -------------------------------------------------------------------------------- /kert-grpc/src/main/kotlin/ws/leap/kert/grpc/Types.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.vertx.core.MultiMap 5 | import kotlinx.coroutines.flow.Flow 6 | import ws.leap.kert.http.Handler 7 | import kotlin.coroutines.AbstractCoroutineContextElement 8 | import kotlin.coroutines.CoroutineContext 9 | 10 | data class GrpcContext(val method: MethodDescriptor<*, *>): AbstractCoroutineContextElement(GrpcContext) { 11 | companion object Key : CoroutineContext.Key 12 | } 13 | 14 | data class GrpcStream( 15 | val metadata: MultiMap, 16 | val messages: Flow, 17 | ) 18 | 19 | typealias GrpcRequest = GrpcStream 20 | typealias GrpcResponse = GrpcStream 21 | 22 | typealias GrpcHandler = suspend (method: MethodDescriptor, req: GrpcRequest) -> GrpcResponse 23 | //interface GrpcHandler { 24 | // suspend operator fun invoke(method: MethodDescriptor, req: GrpcRequest): GrpcResponse 25 | //} 26 | typealias GrpcServerHandler = GrpcHandler 27 | typealias GrpcClientHandler = GrpcHandler 28 | 29 | interface GrpcInterceptor { 30 | suspend operator fun invoke(method: MethodDescriptor, 31 | req: GrpcRequest, 32 | next: GrpcHandler): GrpcResponse 33 | } 34 | 35 | //typealias GrpcInterceptor = suspend (method: MethodDescriptor<*, *>, 36 | // req: GrpcRequest<*>, 37 | // next: GrpcHandler<*, *>) -> GrpcResponse<*> 38 | 39 | typealias GrpcCallHandler = Handler, Flow> 40 | typealias GrpcServerCallHandler = GrpcCallHandler 41 | typealias GrpcClientCallHandler = GrpcCallHandler 42 | 43 | fun intercept(handler: GrpcHandler, interceptor: GrpcInterceptor): GrpcHandler { 44 | return { method, req -> 45 | interceptor(method, req, handler) 46 | } 47 | } 48 | 49 | fun GrpcHandler.intercepted(interceptor: GrpcInterceptor): GrpcHandler { 50 | return intercept(this, interceptor) 51 | } 52 | 53 | fun combineInterceptors(vararg interceptors: GrpcInterceptor): GrpcInterceptor? { 54 | if (interceptors.isEmpty()) return null 55 | 56 | return interceptors.reduce { left, right -> 57 | object: GrpcInterceptor { 58 | override suspend fun invoke(method: MethodDescriptor, 59 | req: GrpcRequest, 60 | next: GrpcHandler): GrpcResponse { 61 | return right(method, req) { m, r -> left(m, r, next) } 62 | } 63 | } 64 | } 65 | } 66 | 67 | fun combineInterceptors(current: GrpcInterceptor?, interceptor: GrpcInterceptor): GrpcInterceptor? { 68 | return current?.let { cur -> 69 | object: GrpcInterceptor { 70 | override suspend fun invoke(method: MethodDescriptor, 71 | req: GrpcRequest, 72 | next: GrpcHandler): GrpcResponse { 73 | return interceptor(method, req) { m, r -> cur(m, r, next) } 74 | } 75 | } 76 | } ?: interceptor 77 | } 78 | 79 | internal suspend fun handle(method: MethodDescriptor, req: GrpcRequest, handler: GrpcHandler, interceptor: GrpcInterceptor?): GrpcResponse { 80 | return interceptor?.let { it(method, req, handler) } 81 | ?: handler(method, req) 82 | } 83 | -------------------------------------------------------------------------------- /kert-grpc/src/main/proto/reflection.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2016 gRPC authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Service exported by server reflection 16 | 17 | syntax = "proto3"; 18 | 19 | package grpc.reflection.v1alpha; 20 | 21 | service ServerReflection { 22 | // The reflection service is structured as a bidirectional stream, ensuring 23 | // all related requests go to a single server. 24 | rpc ServerReflectionInfo(stream ServerReflectionRequest) 25 | returns (stream ServerReflectionResponse); 26 | } 27 | 28 | // The message sent by the client when calling ServerReflectionInfo method. 29 | message ServerReflectionRequest { 30 | string host = 1; 31 | // To use reflection service, the client should set one of the following 32 | // fields in message_request. The server distinguishes requests by their 33 | // defined field and then handles them using corresponding methods. 34 | oneof message_request { 35 | // Find a proto file by the file name. 36 | string file_by_filename = 3; 37 | 38 | // Find the proto file that declares the given fully-qualified symbol name. 39 | // This field should be a fully-qualified symbol name 40 | // (e.g. .[.] or .). 41 | string file_containing_symbol = 4; 42 | 43 | // Find the proto file which defines an extension extending the given 44 | // message type with the given field number. 45 | ExtensionRequest file_containing_extension = 5; 46 | 47 | // Finds the tag numbers used by all known extensions of the given message 48 | // type, and appends them to ExtensionNumberResponse in an undefined order. 49 | // Its corresponding method is best-effort: it's not guaranteed that the 50 | // reflection service will implement this method, and it's not guaranteed 51 | // that this method will provide all extensions. Returns 52 | // StatusCode::UNIMPLEMENTED if it's not implemented. 53 | // This field should be a fully-qualified type name. The format is 54 | // . 55 | string all_extension_numbers_of_type = 6; 56 | 57 | // List the full names of registered services. The content will not be 58 | // checked. 59 | string list_services = 7; 60 | } 61 | } 62 | 63 | // The type name and extension number sent by the client when requesting 64 | // file_containing_extension. 65 | message ExtensionRequest { 66 | // Fully-qualified type name. The format should be . 67 | string containing_type = 1; 68 | int32 extension_number = 2; 69 | } 70 | 71 | // The message sent by the server to answer ServerReflectionInfo method. 72 | message ServerReflectionResponse { 73 | string valid_host = 1; 74 | ServerReflectionRequest original_request = 2; 75 | // The server set one of the following fields accroding to the message_request 76 | // in the request. 77 | oneof message_response { 78 | // This message is used to answer file_by_filename, file_containing_symbol, 79 | // file_containing_extension requests with transitive dependencies. As 80 | // the repeated label is not allowed in oneof fields, we use a 81 | // FileDescriptorResponse message to encapsulate the repeated fields. 82 | // The reflection service is allowed to avoid sending FileDescriptorProtos 83 | // that were previously sent in response to earlier requests in the stream. 84 | FileDescriptorResponse file_descriptor_response = 4; 85 | 86 | // This message is used to answer all_extension_numbers_of_type requst. 87 | ExtensionNumberResponse all_extension_numbers_response = 5; 88 | 89 | // This message is used to answer list_services request. 90 | ListServiceResponse list_services_response = 6; 91 | 92 | // This message is used when an error occurs. 93 | ErrorResponse error_response = 7; 94 | } 95 | } 96 | 97 | // Serialized FileDescriptorProto messages sent by the server answering 98 | // a file_by_filename, file_containing_symbol, or file_containing_extension 99 | // request. 100 | message FileDescriptorResponse { 101 | // Serialized FileDescriptorProto messages. We avoid taking a dependency on 102 | // descriptor.proto, which uses proto2 only features, by making them opaque 103 | // bytes instead. 104 | repeated bytes file_descriptor_proto = 1; 105 | } 106 | 107 | // A list of extension numbers sent by the server answering 108 | // all_extension_numbers_of_type request. 109 | message ExtensionNumberResponse { 110 | // Full name of the base type, including the package name. The format 111 | // is . 112 | string base_type_name = 1; 113 | repeated int32 extension_number = 2; 114 | } 115 | 116 | // A list of ServiceResponse sent by the server answering list_services request. 117 | message ListServiceResponse { 118 | // The information of each service may be expanded in the future, so we use 119 | // ServiceResponse message to encapsulate it. 120 | repeated ServiceResponse service = 1; 121 | } 122 | 123 | // The information of a single service used by ListServiceResponse to answer 124 | // list_services request. 125 | message ServiceResponse { 126 | // Full name of a registered service, including its package name. The format 127 | // is . 128 | string name = 1; 129 | } 130 | 131 | // The error code and error message sent by the server when an error occurs. 132 | message ErrorResponse { 133 | // This field uses the error codes defined in grpc::StatusCode. 134 | int32 error_code = 1; 135 | string error_message = 2; 136 | } 137 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/EchoServiceImpl.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import ws.leap.kert.test.* 4 | import kotlinx.coroutines.delay 5 | import kotlinx.coroutines.flow.Flow 6 | import kotlinx.coroutines.flow.collect 7 | import kotlinx.coroutines.flow.flow 8 | import kotlinx.coroutines.flow.map 9 | import kotlinx.coroutines.runBlocking 10 | import mu.KotlinLogging 11 | import ws.leap.kert.http.httpServer 12 | 13 | object EchoTest { 14 | val streamSize = 500 15 | val message = "hello".repeat(1024) 16 | } 17 | 18 | private val logger = KotlinLogging.logger {} 19 | 20 | class EchoServiceImpl : EchoGrpcKt.EchoImplBase() { 21 | override suspend fun unary(req: EchoReq): EchoResp { 22 | return echoResp { 23 | id = req.id 24 | value = req.value 25 | } 26 | } 27 | 28 | override suspend fun serverStreaming(req: EchoCountReq): Flow { 29 | return flow { 30 | for(i in 0 until req.count) { 31 | val msg = echoResp { 32 | id = i 33 | value = EchoTest.message 34 | } 35 | emit(msg) 36 | logger.trace { "Server sent id=${msg.id}" } 37 | delay(1) 38 | } 39 | } 40 | } 41 | 42 | override suspend fun clientStreaming(req: Flow): EchoCountResp { 43 | var count = 0 44 | req.collect { msg -> 45 | logger.trace { "Server received id=${msg.id}" } 46 | count++ 47 | } 48 | 49 | return echoCountResp { this.count = count } 50 | } 51 | 52 | override suspend fun bidiStreaming(req: Flow): Flow { 53 | return req.map { msg -> 54 | logger.trace { "Server received id=${msg.id}" } 55 | delay(1) 56 | val respMsg = echoResp { 57 | id = msg.id 58 | value = msg.value 59 | } 60 | logger.trace { "Server sent id=${respMsg.id}" } 61 | respMsg 62 | } 63 | } 64 | } 65 | 66 | /* 67 | ghz --insecure -c 100 -z 30s --connections 100 \ 68 | --proto kert-grpc/src/test/proto/echo.proto \ 69 | --call ws.leap.kert.test.Echo.unary \ 70 | -d '{"id":1, "value":"hello"}' \ 71 | 0.0.0.0:8551 72 | 73 | With vertx-lang-kotlin-coroutines stream 74 | Summary: 75 | Count: 3248366 76 | Total: 30.00 s 77 | Slowest: 53.73 ms 78 | Fastest: 0.07 ms 79 | Average: 0.83 ms 80 | Requests/sec: 108270.75 81 | 82 | Response time histogram: 83 | 0.074 [1] | 84 | 5.440 [992545] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 85 | 10.805 [6404] | 86 | 16.170 [754] | 87 | 21.536 [158] | 88 | 26.901 [18] | 89 | 32.266 [15] | 90 | 37.632 [1] | 91 | 42.997 [99] | 92 | 48.363 [3] | 93 | 53.728 [2] | 94 | 95 | Latency distribution: 96 | 10 % in 0.31 ms 97 | 25 % in 0.46 ms 98 | 50 % in 0.66 ms 99 | 75 % in 0.91 ms 100 | 90 % in 1.31 ms 101 | 95 % in 1.88 ms 102 | 99 % in 4.78 ms 103 | 104 | Status code distribution: 105 | [Canceled] 2 responses 106 | [OK] 3248321 responses 107 | [Unavailable] 43 responses 108 | 109 | Error distribution: 110 | [43] rpc error: code = Unavailable desc = transport is closing 111 | [2] rpc error: code = Canceled desc = grpc: the client connection is closing 112 | 113 | 114 | 115 | With own stream 116 | Summary: 117 | Count: 3262371 118 | Total: 30.00 s 119 | Slowest: 30.28 ms 120 | Fastest: 0.07 ms 121 | Average: 0.82 ms 122 | Requests/sec: 108734.61 123 | 124 | Response time histogram: 125 | 0.074 [1] | 126 | 3.095 [978766] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 127 | 6.115 [15971] |∎ 128 | 9.136 [3717] | 129 | 12.156 [1110] | 130 | 15.177 [245] | 131 | 18.197 [98] | 132 | 21.218 [62] | 133 | 24.238 [13] | 134 | 27.259 [8] | 135 | 30.280 [9] | 136 | 137 | Latency distribution: 138 | 10 % in 0.31 ms 139 | 25 % in 0.46 ms 140 | 50 % in 0.65 ms 141 | 75 % in 0.90 ms 142 | 90 % in 1.30 ms 143 | 95 % in 1.84 ms 144 | 99 % in 4.69 ms 145 | 146 | Status code distribution: 147 | [Canceled] 1 responses 148 | [Unavailable] 15 responses 149 | [OK] 3262355 responses 150 | 151 | Error distribution: 152 | [1] rpc error: code = Canceled desc = grpc: the client connection is closing 153 | [15] rpc error: code = Unavailable desc = transport is closing 154 | */ 155 | fun main() = runBlocking { 156 | val server = httpServer(8551) { 157 | grpc { 158 | // enable server reflection 159 | serverReflection = true 160 | 161 | service(EchoServiceImpl()) 162 | } 163 | } 164 | server.start() 165 | } 166 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/EchoServiceJavaImpl.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.ServerBuilder 4 | import io.grpc.stub.StreamObserver 5 | import ws.leap.kert.test.* 6 | 7 | class EchoServiceJavaImpl : EchoGrpc.EchoImplBase() { 8 | override fun unary(request: EchoReq, responseObserver: StreamObserver) { 9 | val response = echoResp { 10 | id = request.id 11 | value = request.value 12 | } 13 | responseObserver.onNext(response) 14 | responseObserver.onCompleted() 15 | } 16 | 17 | override fun serverStreaming(request: EchoCountReq, responseObserver: StreamObserver) { 18 | super.serverStreaming(request, responseObserver) 19 | } 20 | 21 | override fun clientStreaming(responseObserver: StreamObserver): StreamObserver { 22 | return super.clientStreaming(responseObserver) 23 | } 24 | 25 | override fun bidiStreaming(responseObserver: StreamObserver): StreamObserver { 26 | return super.bidiStreaming(responseObserver) 27 | } 28 | } 29 | 30 | /* 31 | ghz --insecure -c 100 -z 30s --connections 100 \ 32 | --proto kert-grpc/src/test/proto/echo.proto \ 33 | --call ws.leap.kert.test.Echo.unary \ 34 | -d '{"id":1, "value":"hello"}' \ 35 | 0.0.0.0:8550 36 | 37 | Summary: 38 | Count: 2923518 39 | Total: 30.00 s 40 | Slowest: 29.55 ms 41 | Fastest: 0.10 ms 42 | Average: 0.94 ms 43 | Requests/sec: 97443.68 44 | 45 | Response time histogram: 46 | 0.097 [1] | 47 | 3.043 [969170] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ 48 | 5.988 [26428] |∎ 49 | 8.933 [3386] | 50 | 11.878 [662] | 51 | 14.824 [196] | 52 | 17.769 [115] | 53 | 20.714 [30] | 54 | 23.659 [6] | 55 | 26.605 [3] | 56 | 29.550 [3] | 57 | 58 | Latency distribution: 59 | 10 % in 0.35 ms 60 | 25 % in 0.48 ms 61 | 50 % in 0.69 ms 62 | 75 % in 1.03 ms 63 | 90 % in 1.66 ms 64 | 95 % in 2.40 ms 65 | 99 % in 4.75 ms 66 | 67 | Status code distribution: 68 | [OK] 2923462 responses 69 | [Unavailable] 56 responses 70 | 71 | Error distribution: 72 | [56] rpc error: code = Unavailable desc = transport is closing 73 | */ 74 | fun main() { 75 | val server = ServerBuilder 76 | .forPort(8550) 77 | .addService(EchoServiceJavaImpl()) 78 | .build() 79 | 80 | server.start() 81 | server.awaitTermination() 82 | } 83 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/Example.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.vertx.core.http.HttpVersion 5 | import kotlinx.coroutines.flow.map 6 | import kotlinx.coroutines.runBlocking 7 | import ws.leap.kert.http.httpClient 8 | import ws.leap.kert.http.httpServer 9 | import ws.leap.kert.http.response 10 | import ws.leap.kert.test.EchoGrpcKt 11 | import ws.leap.kert.test.EchoReq 12 | import ws.leap.kert.test.echoReq 13 | 14 | class Example { 15 | fun server() = runBlocking { 16 | val server = httpServer(8080) { 17 | // server side filter 18 | filter { req, next -> 19 | println("Serving request ${req.path}") 20 | next(req) 21 | } 22 | 23 | // http service 24 | router { 25 | // http request handler 26 | get("/ping") { 27 | response(body = "pong") 28 | } 29 | } 30 | 31 | // grpc service 32 | grpc { 33 | // enable server reflection 34 | serverReflection = true 35 | 36 | // grpc interceptor 37 | interceptor( object : GrpcInterceptor { 38 | override suspend fun invoke( 39 | method: MethodDescriptor, 40 | req: GrpcRequest, 41 | next: GrpcHandler 42 | ): GrpcResponse { 43 | // intercept the request 44 | if (req.metadata["authentication"] == null) throw IllegalArgumentException("Authentication header is missing") 45 | 46 | // intercept each message in the streaming request 47 | val filteredReq = req.copy(messages = req.messages.map { 48 | println(it) 49 | it 50 | }) 51 | return next(method, filteredReq) 52 | } 53 | }) 54 | 55 | // register service implementation 56 | service(EchoServiceImpl()) 57 | } 58 | } 59 | 60 | server.start() 61 | } 62 | 63 | fun client() = runBlocking { 64 | // http request 65 | val client = httpClient { 66 | options { 67 | defaultHost = "localhost" 68 | defaultPort = 8551 69 | protocolVersion = HttpVersion.HTTP_2 70 | } 71 | 72 | // a client side filter to set authorization header in request 73 | filter { req, next -> 74 | req.headers["authorization"] = "my-authorization-header" 75 | next(req) 76 | } 77 | } 78 | client.get("ping") 79 | 80 | // grpc request 81 | val stub = EchoGrpcKt.stub(client) 82 | stub.unary(echoReq { id = 1; value = "hello" }) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcBasicSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.matchers.collections.shouldHaveSize 5 | import io.kotest.matchers.shouldBe 6 | import kotlinx.coroutines.delay 7 | import kotlinx.coroutines.flow.collect 8 | import kotlinx.coroutines.flow.flow 9 | import kotlinx.coroutines.flow.map 10 | import kotlinx.coroutines.flow.toList 11 | import mu.KotlinLogging 12 | import ws.leap.kert.test.* 13 | 14 | private val logger = KotlinLogging.logger {} 15 | 16 | @DoNotParallelize 17 | class GrpcBasicSpec : GrpcSpec() { 18 | override fun configureServer(builder: GrpcServerBuilder) { 19 | builder.service(EchoServiceImpl()) 20 | } 21 | 22 | private val stub = EchoGrpcKt.stub(client) 23 | 24 | init { 25 | context("Grpc server/client") { 26 | test("unary") { 27 | val req = echoReq { id = 1; value = EchoTest.message } 28 | val resp = stub.unary(req) 29 | resp.id shouldBe 1 30 | resp.value shouldBe EchoTest.message 31 | } 32 | 33 | test("server stream") { 34 | val req = echoCountReq { count = EchoTest.streamSize } 35 | val resp = stub.serverStreaming(req) 36 | val respMsgs = resp.map { msg -> 37 | logger.trace { "Client received id=${msg.id}" } 38 | msg 39 | }.toList() 40 | respMsgs shouldHaveSize EchoTest.streamSize 41 | } 42 | 43 | test("client stream") { 44 | val req = flow { 45 | for(i in 0 until EchoTest.streamSize) { 46 | val msg = echoReq { id = i; value = EchoTest.message } 47 | emit(msg) 48 | logger.trace { "Client sent id=${msg.id}" } 49 | delay(1) 50 | } 51 | } 52 | 53 | val resp = stub.clientStreaming(req) 54 | resp.count shouldBe EchoTest.streamSize 55 | } 56 | 57 | test("bidi stream") { 58 | val req = flow { 59 | for(i in 0 until EchoTest.streamSize) { 60 | val msg = echoReq { id = i; value = EchoTest.message } 61 | emit(msg) 62 | logger.trace { "Client sent id=${msg.id}" } 63 | delay(1) 64 | } 65 | } 66 | 67 | val resp = stub.bidiStreaming(req) 68 | var count = 0 69 | resp.collect { msg -> 70 | count++ 71 | logger.trace { "Client received id=${msg.id}" } 72 | } 73 | count shouldBe EchoTest.streamSize 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcErrorSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.StatusException 4 | import io.kotest.assertions.throwables.shouldThrow 5 | import io.kotest.core.spec.DoNotParallelize 6 | import kotlinx.coroutines.delay 7 | import kotlinx.coroutines.flow.* 8 | import mu.KotlinLogging 9 | import ws.leap.kert.test.* 10 | 11 | private val logger = KotlinLogging.logger {} 12 | 13 | @DoNotParallelize 14 | class GrpcErrorSpec : GrpcSpec() { 15 | override fun configureServer(builder: GrpcServerBuilder) { 16 | val echoService = object: EchoGrpcKt.EchoImplBase() { 17 | override suspend fun unary(req: EchoReq): EchoResp { 18 | throw RuntimeException("mocked error") 19 | } 20 | 21 | override suspend fun serverStreaming(req: EchoCountReq): Flow { 22 | return flow { 23 | for(i in 0 until req.count / 2) { 24 | val msg = echoResp { id = i; value = EchoTest.message } 25 | emit(msg) 26 | logger.trace { "Server sent id=${msg.id}" } 27 | delay(1) 28 | } 29 | 30 | // throw error in the middle 31 | throw RuntimeException("mocked error") 32 | } 33 | } 34 | 35 | override suspend fun clientStreaming(req: Flow): EchoCountResp { 36 | var count = 0 37 | req.collect { msg -> 38 | logger.trace { "Server received id=${msg.id}" } 39 | count++ 40 | 41 | if (count > EchoTest.streamSize / 2) { 42 | // throw error in the middle 43 | throw RuntimeException("mocked error") 44 | } 45 | } 46 | 47 | return echoCountResp { this.count = count } 48 | } 49 | 50 | override suspend fun bidiStreaming(req: Flow): Flow { 51 | var count = 0 52 | return req.map { msg -> 53 | logger.trace { "Server received id=${msg.id}" } 54 | delay(1) 55 | val respMsg = echoResp { id = msg.id; value = msg.value } 56 | logger.trace { "Server sent id=${respMsg.id}" } 57 | 58 | count++ 59 | if (count > EchoTest.streamSize / 2) { 60 | // throw error in the middle 61 | throw RuntimeException("mocked error") 62 | } 63 | 64 | respMsg 65 | } 66 | } 67 | } 68 | 69 | builder.service(echoService) 70 | } 71 | 72 | private val stub = EchoGrpcKt.stub(client) 73 | 74 | init { 75 | context("Grpc should capture the errors") { 76 | test("unary") { 77 | val req = echoReq { id = 1; value = EchoTest.message } 78 | shouldThrow { 79 | stub.unary(req) 80 | } 81 | } 82 | 83 | test("server stream") { 84 | val req = echoCountReq { count = EchoTest.streamSize } 85 | val resp = stub.serverStreaming(req) 86 | 87 | shouldThrow { 88 | resp.map { msg -> 89 | logger.trace { "Client received id=${msg.id}" } 90 | msg 91 | }.toList() 92 | } 93 | } 94 | 95 | test("client stream") { 96 | val req = flow { 97 | for(i in 0 until EchoTest.streamSize) { 98 | val msg = echoReq { id = i; value = i.toString() } 99 | emit(msg) 100 | logger.trace { "Client sent id=${msg.id}" } 101 | delay(1) 102 | } 103 | } 104 | 105 | shouldThrow { 106 | stub.clientStreaming(req) 107 | } 108 | } 109 | 110 | test("bidi stream") { 111 | val req = flow { 112 | for(i in 0 until EchoTest.streamSize) { 113 | val msg = echoReq { id = i; value = i.toString() } 114 | emit(msg) 115 | logger.trace { "Client sent id=${msg.id}" } 116 | delay(1) 117 | } 118 | } 119 | 120 | val resp = stub.bidiStreaming(req) 121 | shouldThrow { 122 | resp.collect { msg -> 123 | logger.trace { "Client received id=${msg.id}" } 124 | } 125 | } 126 | } 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcInterceptorSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.MethodDescriptor 4 | import io.grpc.StatusException 5 | import io.kotest.assertions.throwables.shouldThrow 6 | import io.kotest.core.spec.DoNotParallelize 7 | import io.kotest.matchers.shouldBe 8 | import kotlinx.coroutines.flow.map 9 | import ws.leap.kert.test.EchoGrpcKt 10 | import ws.leap.kert.test.EchoReq 11 | import ws.leap.kert.test.echoReq 12 | 13 | @DoNotParallelize 14 | class GrpcInterceptorSpec : GrpcSpec() { 15 | override fun configureServer(builder: GrpcServerBuilder) = with(builder) { 16 | interceptor(object: GrpcInterceptor { 17 | override suspend fun invoke(method: MethodDescriptor, 18 | req: GrpcRequest, 19 | next: GrpcHandler): GrpcResponse { 20 | // fail if no authentication header 21 | if (req.metadata["authentication"] == null) throw IllegalArgumentException("Authentication header is missing") 22 | 23 | // fail if message value is "not-good" 24 | val filteredReq = req.copy(messages = req.messages.map { msg -> 25 | if (msg is EchoReq && msg.value == "not-good") throw IllegalArgumentException("Mocked exception") 26 | msg 27 | }) 28 | return next(method, filteredReq) 29 | } 30 | }) 31 | 32 | service(EchoServiceImpl()) 33 | } 34 | 35 | private val stub = EchoGrpcKt.stub(client) 36 | private val stubWithAuth = stub.intercepted(object: GrpcInterceptor { 37 | override suspend fun invoke(method: MethodDescriptor, 38 | req: GrpcRequest, 39 | next: GrpcHandler): GrpcResponse { 40 | req.metadata["authentication"] = "mocked-authentication" 41 | return next(method, req) 42 | } 43 | }) 44 | 45 | init { 46 | test("should fail if no authentication") { 47 | shouldThrow { 48 | stub.unary(echoReq { id = 1; value = "good" }) 49 | } 50 | } 51 | test("should succeed if message is good") { 52 | val resp = stubWithAuth.unary(echoReq { id = 1; value = "good" }) 53 | resp.value shouldBe "good" 54 | } 55 | 56 | test("should fail if message is not-good") { 57 | shouldThrow { 58 | stubWithAuth.unary(echoReq { id = 1; value = "not-good" }) 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcNestedBidiSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.matchers.shouldBe 5 | import kotlinx.coroutines.channels.Channel 6 | import kotlinx.coroutines.flow.Flow 7 | import kotlinx.coroutines.flow.collect 8 | import kotlinx.coroutines.flow.flow 9 | import mu.KotlinLogging 10 | import ws.leap.kert.test.* 11 | import java.util.concurrent.atomic.AtomicInteger 12 | 13 | private val logger = KotlinLogging.logger {} 14 | 15 | /** 16 | * A test to demonstrate feed request messages with response messages from server. 17 | */ 18 | @DoNotParallelize 19 | class GrpcNestedBidiSpec : GrpcSpec() { 20 | private val messageNum = 10 21 | private val omittedCount = AtomicInteger() 22 | 23 | override fun configureServer(builder: GrpcServerBuilder) { 24 | val echoService = object: EchoGrpcKt.EchoImplBase() { 25 | override suspend fun bidiStreaming(req: Flow): Flow { 26 | return flow { 27 | req.collect { reqMsg -> 28 | val reqValue = reqMsg.value.toInt() 29 | // if the value is less than 100, double it then send it back 30 | // otherwise omit it 31 | if (reqValue < 100) { 32 | logger.trace { "Server: Message id=${reqMsg.id} value=${reqMsg.value} bounce" } 33 | val respValue = (reqValue * 2).toString() 34 | val respMsg = echoResp { id = reqMsg.id; value = respValue } 35 | emit(respMsg) 36 | } else { 37 | logger.trace { "Server: Message id=${reqMsg.id} value=${reqMsg.value} omitted" } 38 | omittedCount.incrementAndGet() 39 | if(omittedCount.get() == messageNum) { 40 | // all messages are omitted, end the loop 41 | emit(echoResp { 42 | id = -1 43 | value = "end" 44 | }) 45 | } 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | builder.service(echoService) 53 | } 54 | 55 | private val stub = EchoGrpcKt.stub(client) 56 | 57 | init { 58 | context("Grpc") { 59 | test("can use response messages as request messages") { 60 | val channel = Channel() 61 | val req = flow { 62 | for(i in 1 .. messageNum) { 63 | // send initial 10 messages 64 | val msg = echoReq { id = i; value = i.toString() } 65 | emit(msg) 66 | logger.trace { "Client sent id=${msg.id} value=${msg.value}" } 67 | } 68 | 69 | // client always bounce the message back to server 70 | for(resp in channel) { 71 | logger.trace { "Client: Message id=${resp.id} value=${resp.value} bounce" } 72 | val msg = echoReq { id = resp.id; value = resp.value } 73 | emit(msg) 74 | } 75 | } 76 | 77 | val resp = stub.bidiStreaming(req) 78 | var count = 0 79 | resp.collect { msg -> 80 | logger.trace { "Client received id=${msg.id}" } 81 | if(msg.id == -1) { 82 | // server indicates end, end the loop 83 | channel.close() 84 | } else { 85 | count++ 86 | // put message to channel so it can be sent to server again 87 | channel.send(msg) 88 | } 89 | } 90 | count shouldBe 50 // all messages received, doesn't count the "end" message 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.kotest.core.spec.Spec 4 | import io.kotest.core.spec.style.FunSpec 5 | import io.vertx.core.http.HttpVersion 6 | import kotlinx.coroutines.runBlocking 7 | import ws.leap.kert.http.httpClient 8 | import ws.leap.kert.http.httpServer 9 | 10 | abstract class GrpcSpec : FunSpec() { 11 | protected val port = 8551 12 | protected val client = httpClient { 13 | options { 14 | protocolVersion = HttpVersion.HTTP_2 15 | defaultPort = port 16 | isHttp2ClearTextUpgrade = false 17 | } 18 | } 19 | 20 | protected val server = httpServer(port) { 21 | grpc { 22 | configureServer(this) 23 | } 24 | } 25 | protected abstract fun configureServer(builder: GrpcServerBuilder) 26 | 27 | init { 28 | beforeSpec { 29 | runBlocking { 30 | server.start() 31 | } 32 | } 33 | afterSpec { 34 | runBlocking { 35 | server.stop() 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/GrpcUnimplementedSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.grpc.Status 4 | import io.grpc.StatusException 5 | import io.grpc.StatusRuntimeException 6 | import io.kotest.assertions.throwables.shouldThrow 7 | import io.kotest.core.spec.DoNotParallelize 8 | import io.kotest.matchers.shouldBe 9 | import kotlinx.coroutines.flow.Flow 10 | import kotlinx.coroutines.flow.collect 11 | import kotlinx.coroutines.flow.flow 12 | import ws.leap.kert.test.* 13 | 14 | @DoNotParallelize 15 | class GrpcUnimplementedSpec : GrpcSpec() { 16 | override fun configureServer(builder: GrpcServerBuilder) { 17 | val echoService = object: EchoGrpcKt.EchoImplBase() { 18 | override suspend fun serverStreaming(req: EchoCountReq): Flow { 19 | throw StatusRuntimeException(Status.DATA_LOSS) 20 | } 21 | 22 | override suspend fun clientStreaming(req: Flow): EchoCountResp { 23 | throw StatusRuntimeException(Status.INTERNAL) 24 | } 25 | 26 | override suspend fun bidiStreaming(req: Flow): Flow { 27 | throw StatusException(Status.NOT_FOUND) 28 | } 29 | } 30 | builder.service(echoService) 31 | } 32 | 33 | private val stub = EchoGrpcKt.stub(client) 34 | 35 | init { 36 | context("Grpc") { 37 | test("unary is not implemented") { 38 | val req = echoReq { id = 1; value = EchoTest.message } 39 | val exception = shouldThrow { 40 | stub.unary(req) 41 | } 42 | exception.status.code shouldBe Status.Code.UNIMPLEMENTED 43 | } 44 | 45 | test("server stream should cause DATA_LOSS") { 46 | val req = echoCountReq { count = EchoTest.streamSize } 47 | val exception = shouldThrow { 48 | stub.serverStreaming(req) 49 | .collect {} // collect is required to raise exception from a failed flow 50 | } 51 | exception.status.code shouldBe Status.Code.DATA_LOSS 52 | } 53 | 54 | test("client stream") { 55 | val req = flow { 56 | for(i in 0 until EchoTest.streamSize) { 57 | val msg = echoReq { id = i; value = i.toString() } 58 | emit(msg) 59 | } 60 | } 61 | 62 | val exception = shouldThrow { 63 | stub.clientStreaming(req) 64 | } 65 | exception.status.code shouldBe Status.Code.INTERNAL 66 | } 67 | 68 | test("bidi stream") { 69 | val req = flow { 70 | for(i in 0 until EchoTest.streamSize) { 71 | val msg = echoReq { id = i; value = i.toString() } 72 | emit(msg) 73 | } 74 | } 75 | 76 | val exception = shouldThrow { 77 | stub.bidiStreaming(req) 78 | .collect {} // collect is required to raise exception from a failed flow 79 | } 80 | exception.status.code shouldBe Status.Code.NOT_FOUND 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /kert-grpc/src/test/kotlin/ws/leap/kert/grpc/ManualTestClient.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.grpc 2 | 3 | import io.vertx.core.http.HttpVersion 4 | import kotlinx.coroutines.runBlocking 5 | import ws.leap.kert.http.httpClient 6 | import ws.leap.kert.test.EchoGrpcKt 7 | import ws.leap.kert.test.echoReq 8 | 9 | /** 10 | * This is for manual test (debugging) to trigger one request. 11 | */ 12 | fun main() { 13 | val client = httpClient { 14 | options { 15 | protocolVersion = HttpVersion.HTTP_2 16 | defaultPort = 8551 17 | isHttp2ClearTextUpgrade = false 18 | } 19 | } 20 | val stub = EchoGrpcKt.stub(client) 21 | runBlocking { 22 | val req = echoReq { id = 1; value = EchoTest.message } 23 | val resp = stub.unary(req) 24 | println(resp) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /kert-grpc/src/test/proto/echo.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package ws.leap.kert.test; 3 | option java_outer_classname = "EchoProto"; 4 | option java_multiple_files = true; 5 | 6 | message EchoReq { 7 | int32 id = 1; 8 | string value = 2; 9 | } 10 | 11 | message EchoCountReq { 12 | int32 count = 1; 13 | } 14 | 15 | message EchoResp { 16 | int32 id = 1; 17 | string value = 2; 18 | } 19 | 20 | message EchoCountResp { 21 | int32 count = 1; 22 | } 23 | 24 | service Echo { 25 | rpc unary(EchoReq) returns (EchoResp); 26 | rpc serverStreaming(EchoCountReq) returns (stream EchoResp); 27 | rpc clientStreaming(stream EchoReq) returns (EchoCountResp); 28 | rpc bidiStreaming(stream EchoReq) returns (stream EchoResp); 29 | } 30 | -------------------------------------------------------------------------------- /kert-grpc/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /kert-http/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import build.* 2 | 3 | description = "Kert HTTP support" 4 | 5 | configureLibrary() 6 | 7 | dependencies { 8 | api(libs.bundles.kotlin) 9 | 10 | api(libs.kotlinx.coroutines) 11 | api(libs.kotlinx.coroutines.slf4j) 12 | 13 | api(libs.vertx.web) 14 | api(libs.vertx.lang.kotlin.coroutines) 15 | 16 | testImplementation(libs.vertx.web.client) 17 | } 18 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpClient.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.MultiMap 4 | import io.vertx.core.Vertx 5 | import io.vertx.core.http.HttpClientOptions 6 | import io.vertx.core.http.HttpMethod 7 | import io.vertx.core.http.HttpVersion 8 | import java.net.URL 9 | 10 | data class RequestOptions( 11 | val ssl: Boolean, 12 | val host: String, 13 | val port: Int 14 | ) 15 | 16 | interface HttpClient { 17 | suspend fun get(uri: String, headers: MultiMap? = null) = call(request(HttpMethod.GET, uri, headers)) 18 | suspend fun head(uri: String, headers: MultiMap? = null) = call(request(HttpMethod.HEAD, uri, headers)) 19 | suspend fun put(uri: String, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.PUT, uri, headers, body, contentLength)) 20 | suspend fun post(uri: String, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.POST, uri, headers, body, contentLength)) 21 | suspend fun delete(uri: String, headers: MultiMap? = null) = call(request(HttpMethod.DELETE, uri, headers)) 22 | suspend fun patch(uri: String, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.PATCH, uri, headers, body, contentLength)) 23 | 24 | suspend fun get(url: URL, headers: MultiMap? = null) = call(request(HttpMethod.GET, url, headers)) 25 | suspend fun head(url: URL, headers: MultiMap? = null) = call(request(HttpMethod.HEAD, url, headers)) 26 | suspend fun put(url: URL, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.PUT, url, headers, body, contentLength)) 27 | suspend fun post(url: URL, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.POST, url, headers, body, contentLength)) 28 | suspend fun delete(url: URL, headers: MultiMap? = null) = call(request(HttpMethod.DELETE, url, headers)) 29 | suspend fun patch(url: URL, body: Any, contentLength: Long? = null, headers: MultiMap? = null) = call(request(HttpMethod.PATCH, url, headers, body, contentLength)) 30 | 31 | suspend fun call(request: HttpClientRequest): HttpClientResponse 32 | 33 | suspend fun close() 34 | 35 | fun withFilter(filter: HttpClientFilter): HttpClient 36 | fun withFilters(vararg filters: HttpClientFilter): HttpClient 37 | fun withOptions(options: RequestOptions): HttpClient 38 | val protocolVersion: HttpVersion 39 | } 40 | 41 | interface HttpClientBuilderDsl { 42 | fun options(configure: HttpClientOptions.() -> Unit) 43 | fun filter(filter: HttpClientFilter) 44 | } 45 | 46 | class HttpClientBuilder(private val vertx: Vertx): HttpClientBuilderDsl { 47 | private val filters = mutableListOf() 48 | private val options = HttpClientOptions() 49 | 50 | override fun options(configure: HttpClientOptions.() -> Unit) { 51 | configure(options) 52 | } 53 | 54 | override fun filter(filter: HttpClientFilter) { 55 | filters.add(filter) 56 | } 57 | 58 | fun build(): HttpClient { 59 | val filter = combineFilters(*filters.toTypedArray()) 60 | val vertxClient = vertx.createHttpClient(options) as io.vertx.core.http.impl.HttpClientImpl 61 | return HttpClientImpl(vertxClient, filter) 62 | } 63 | } 64 | 65 | fun httpClient(vertx: Vertx, configure: (HttpClientBuilderDsl.() -> Unit)? = null): HttpClient { 66 | val builder = HttpClientBuilder(vertx) 67 | configure?.let { it(builder) } 68 | return builder.build() 69 | } 70 | 71 | 72 | fun httpClient(configure: (HttpClientBuilderDsl.() -> Unit)? = null): HttpClient { 73 | return httpClient(Kert.vertx, configure) 74 | } 75 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpClientImpl.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Vertx 4 | import io.vertx.core.http.HttpVersion 5 | import io.vertx.kotlin.coroutines.await 6 | import io.vertx.kotlin.coroutines.dispatcher 7 | import kotlinx.coroutines.CompletableDeferred 8 | import kotlinx.coroutines.CoroutineScope 9 | import kotlinx.coroutines.launch 10 | import mu.KotlinLogging 11 | import kotlin.coroutines.coroutineContext 12 | 13 | private val logger = KotlinLogging.logger {} 14 | 15 | internal class HttpClientImpl (private val underlying: io.vertx.core.http.impl.HttpClientImpl, 16 | private val filters: HttpClientFilter? = null, 17 | private val options: RequestOptions? = null) : HttpClient { 18 | override suspend fun call(request: HttpClientRequest): HttpClientResponse { 19 | return handle(request, ::callHttp, filters) 20 | } 21 | 22 | override suspend fun close() { 23 | underlying.close().await() 24 | } 25 | 26 | override fun withFilter(filter: HttpClientFilter): HttpClient { 27 | return HttpClientImpl(underlying, filter, options) 28 | } 29 | 30 | override fun withFilters(vararg filters: HttpClientFilter): HttpClient { 31 | if(filters.isEmpty()) return this 32 | 33 | val combinedFilter = combineFilters(*filters)!! 34 | return withFilter(combinedFilter) 35 | } 36 | 37 | override fun withOptions(options: RequestOptions): HttpClient { 38 | return HttpClientImpl(underlying, filters, options) 39 | } 40 | 41 | override val protocolVersion: HttpVersion = underlying.options().protocolVersion 42 | 43 | private suspend fun callHttp(request: HttpClientRequest): HttpClientResponse { 44 | val responseDeferred = CompletableDeferred() 45 | val scope = CoroutineScope(coroutineContext) 46 | 47 | underlying.request(requestOptions(request)) { ar -> 48 | logger.debug { "created request" } 49 | if (ar.succeeded()) { 50 | val vertxRequest = ar.result() 51 | val vertxContext = Vertx.currentContext() 52 | 53 | vertxRequest.headers().addAll(request.headers) 54 | vertxRequest.isChunked = request.chunked() 55 | 56 | // start send request body 57 | scope.launch(vertxContext.dispatcher()) { 58 | try { 59 | write(vertxContext, request.body, vertxRequest) 60 | vertxRequest.end().await() 61 | } catch (t: Throwable) { 62 | // send request body failed 63 | responseDeferred.completeExceptionally(t) 64 | } 65 | } 66 | 67 | vertxRequest.response { respResult -> 68 | if(respResult.succeeded()) { 69 | val vertxResponse = respResult.result() 70 | responseDeferred.complete(HttpClientResponse(vertxResponse, vertxContext)) 71 | } 72 | } 73 | } else { 74 | // start request failed 75 | responseDeferred.completeExceptionally(ar.cause()) 76 | } 77 | } 78 | 79 | return responseDeferred.await() 80 | } 81 | 82 | private fun requestOptions(request: HttpClientRequest): io.vertx.core.http.RequestOptions { 83 | val defaults = request.options ?: options 84 | val options = io.vertx.core.http.RequestOptions() 85 | options.run { 86 | method = request.method 87 | host = defaults?.host 88 | port = defaults?.port 89 | isSsl = defaults?.ssl 90 | uri = request.uri 91 | } 92 | return options 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpClientRequest.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.MultiMap 4 | import io.vertx.core.buffer.Buffer 5 | import io.vertx.core.http.HttpHeaders 6 | import io.vertx.core.http.HttpMethod 7 | import io.vertx.core.http.impl.headers.HeadersMultiMap 8 | import kotlinx.coroutines.flow.Flow 9 | import kotlinx.coroutines.flow.emptyFlow 10 | import java.net.URL 11 | 12 | data class HttpClientRequest internal constructor( 13 | override val method: HttpMethod, 14 | override val uri: String, 15 | override val headers: MultiMap = HeadersMultiMap(), 16 | override val body: Flow = emptyFlow(), 17 | internal val options: RequestOptions? = null, 18 | ): HttpRequest 19 | 20 | fun request(method: HttpMethod, url: URL, headers: MultiMap? = null, body: Any? = null, contentLength: Long? = null): HttpClientRequest { 21 | val theHeaders = constructHeaders(headers, contentLength, body) 22 | 23 | val requestUri = "${url.file}${url.ref?.map { "#$it" } ?: ""}" 24 | val actualPort = if (url.port != -1) url.port else url.defaultPort 25 | val defaults = RequestOptions(url.protocol == "https", url.host, actualPort) 26 | 27 | return HttpClientRequest(method, requestUri, theHeaders, toFlow(body), defaults) 28 | } 29 | 30 | fun request(method: HttpMethod, uri: String, headers: MultiMap? = null, body: Any? = null, contentLength: Long? = null): HttpClientRequest { 31 | val theHeaders = constructHeaders(headers, contentLength, body) 32 | 33 | return HttpClientRequest(method, uri, headers = theHeaders, body = toFlow(body)) 34 | } 35 | 36 | internal fun constructHeaders(headers: MultiMap?, contentLength: Long?, body: Any?): MultiMap { 37 | val theHeaders = headers ?: HeadersMultiMap() 38 | if (contentLength != null) { 39 | theHeaders[HttpHeaders.CONTENT_LENGTH] = contentLength.toString() 40 | } else { 41 | if (body == null) { 42 | theHeaders[HttpHeaders.CONTENT_LENGTH] = "0" 43 | } 44 | } 45 | return theHeaders 46 | } 47 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpClientResponse.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Context 4 | import io.vertx.core.MultiMap 5 | import io.vertx.core.buffer.Buffer 6 | import kotlinx.coroutines.flow.Flow 7 | import io.vertx.core.http.HttpClientResponse as VHttpClientResponse 8 | 9 | class HttpClientResponse (private val underlying: VHttpClientResponse, private val context: Context) : HttpResponse { 10 | override val headers: MultiMap = underlying.headers() 11 | override val trailers: () -> MultiMap = { 12 | underlying.trailers() 13 | } 14 | 15 | override val body: Flow = underlying.asFlow(context) 16 | override val statusCode: Int = underlying.statusCode() 17 | } 18 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpRouter.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Vertx 4 | import io.vertx.core.http.HttpMethod 5 | import io.vertx.ext.web.Router 6 | import io.vertx.kotlin.coroutines.await 7 | import io.vertx.kotlin.coroutines.dispatcher 8 | import kotlinx.coroutines.* 9 | import kotlinx.coroutines.slf4j.MDCContext 10 | import mu.KotlinLogging 11 | import kotlin.coroutines.AbstractCoroutineContextElement 12 | import kotlin.coroutines.CoroutineContext 13 | import io.vertx.ext.web.RoutingContext as VRoutingContext 14 | 15 | data class VertxRoutingContext( 16 | val routingContext: VRoutingContext 17 | ) : AbstractCoroutineContextElement(VertxRoutingContext) { 18 | companion object Key : CoroutineContext.Key 19 | override fun toString(): String = "VertxRoutingContext($routingContext)" 20 | } 21 | 22 | private val httpExceptionLogger = KotlinLogging.logger {} 23 | val defaultHttpExceptionHandler = CoroutineExceptionHandler { context, exception -> 24 | val routingContext = context[VertxRoutingContext]?.routingContext ?: throw IllegalStateException("Routing context is not available on coroutine context") 25 | httpExceptionLogger.warn(exception) { "HTTP call failed, path=${routingContext.request().path()}" } 26 | 27 | val response = routingContext.response() 28 | if (!response.ended()) { 29 | if (!response.headWritten()) { 30 | response.statusCode = 500 31 | response.statusMessage = exception.toString() 32 | response.end() 33 | } else { 34 | // head already sent, reset the connection 35 | response.reset() 36 | // response.close() 37 | } 38 | } 39 | } 40 | 41 | internal data class SubRouterDef( 42 | val path: String, 43 | val exceptionHandler: CoroutineExceptionHandler?, 44 | val configure: HttpRouterBuilder.() -> Unit 45 | ) 46 | 47 | internal data class HandlerDef( 48 | val method: HttpMethod, 49 | val path: String, 50 | val handler: HttpServerHandler 51 | ) 52 | 53 | open class HttpRouterBuilder(private val vertx: Vertx, 54 | internal val underlying: Router, 55 | private var parentFilter: HttpServerFilter?, 56 | private val exceptionHandler: CoroutineExceptionHandler?): HttpRouterDsl { 57 | private val filters = mutableListOf() 58 | private val subRouters = mutableListOf() 59 | private val handlers = mutableListOf() 60 | 61 | override fun filter(filter: HttpServerFilter) { 62 | filters.add(filter) 63 | } 64 | 65 | override fun subRouter(path: String, exceptionHandler: CoroutineExceptionHandler?, configure: HttpRouterBuilder.() -> Unit) { 66 | subRouters.add(SubRouterDef(path, exceptionHandler, configure)) 67 | } 68 | 69 | override fun call(method: HttpMethod, path: String, handler: HttpServerHandler) { 70 | handlers.add(HandlerDef(method, path, handler)) 71 | } 72 | 73 | fun build() { 74 | val combinedFilter = combineFilters(*filters.toTypedArray()) 75 | val finalFilter = combineFilters(combinedFilter, parentFilter) 76 | 77 | // configure handlers 78 | for(handlerDef in handlers) { 79 | registerCall(handlerDef.method, handlerDef.path, handlerDef.handler, finalFilter) 80 | } 81 | 82 | // configure sub routers 83 | for(subRouter in subRouters) { 84 | val vertxRouter = Router.router(vertx) 85 | val builder = HttpRouterBuilder(vertx, vertxRouter, finalFilter, subRouter.exceptionHandler ?: exceptionHandler) 86 | subRouter.configure(builder) 87 | builder.build() 88 | 89 | underlying.mountSubRouter(subRouter.path, vertxRouter) 90 | } 91 | } 92 | 93 | private fun exceptionHandler(): CoroutineExceptionHandler = exceptionHandler ?: defaultHttpExceptionHandler 94 | 95 | private fun createContext(routingContext: VRoutingContext): CoroutineContext { 96 | val context = Vertx.currentContext() 97 | return context.dispatcher() + VertxRoutingContext(routingContext) + MDCContext() + exceptionHandler() 98 | } 99 | 100 | private fun registerCall(method: HttpMethod, path: String, handler: HttpServerHandler, filter: HttpServerFilter?) { 101 | underlying.route(method, path).handler { routingContext -> 102 | val request = HttpServerRequest(routingContext.request(), routingContext) 103 | val context = Vertx.currentContext() 104 | 105 | CoroutineScope(createContext(routingContext)).launch { 106 | val response = filter?.let { it(request, handler) } ?: handler(request) 107 | val vertxResponse = routingContext.response() 108 | 109 | // copy status code 110 | vertxResponse.statusCode = response.statusCode 111 | 112 | // copy headers 113 | vertxResponse.headers().addAll(response.headers) 114 | vertxResponse.isChunked = response.chunked() 115 | 116 | // write body 117 | write(context, response.body, vertxResponse) 118 | 119 | // write trailers 120 | vertxResponse.trailers().addAll(response.trailers()) 121 | 122 | // end response 123 | vertxResponse.end().await() 124 | } 125 | } 126 | } 127 | } 128 | 129 | 130 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpRouterDsl.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.http.HttpMethod 4 | import kotlinx.coroutines.CoroutineExceptionHandler 5 | 6 | interface HttpRouterDsl { 7 | fun filter(filter: HttpServerFilter) 8 | 9 | fun subRouter(path: String, exceptionHandler: CoroutineExceptionHandler? = null, configure: HttpRouterBuilder.() -> Unit) 10 | 11 | fun call(method: HttpMethod, path: String, handler: HttpServerHandler) 12 | 13 | fun get(path: String, handler: HttpServerHandler) { 14 | call(HttpMethod.GET, path, handler) 15 | } 16 | 17 | fun head(path: String, handler: HttpServerHandler) { 18 | call(HttpMethod.HEAD, path, handler) 19 | } 20 | 21 | fun post(path: String, handler: HttpServerHandler) { 22 | call(HttpMethod.POST, path, handler) 23 | } 24 | 25 | fun put(path: String, handler: HttpServerHandler) { 26 | call(HttpMethod.PUT, path, handler) 27 | } 28 | 29 | fun delete(path: String, handler: HttpServerHandler) { 30 | call(HttpMethod.DELETE, path, handler) 31 | } 32 | 33 | fun patch(path: String, handler: HttpServerHandler) { 34 | call(HttpMethod.PATCH, path, handler) 35 | } 36 | 37 | fun options(path: String, handler: HttpServerHandler) { 38 | call(HttpMethod.OPTIONS, path, handler) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpServer.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.* 4 | import io.vertx.core.http.HttpServer as VHttpServer 5 | import io.vertx.core.http.HttpServerOptions 6 | import io.vertx.ext.web.Router 7 | import io.vertx.kotlin.coroutines.await 8 | import kotlinx.coroutines.CoroutineExceptionHandler 9 | 10 | internal data class RouterDef( 11 | val exceptionHandler: CoroutineExceptionHandler?, 12 | val configure: HttpRouterBuilder.() -> Unit 13 | ) 14 | 15 | interface HttpServerBuilderDsl { 16 | fun options(configure: HttpServerOptions.() -> Unit) 17 | fun filter(filter: HttpServerFilter) 18 | fun router(exceptionHandler: CoroutineExceptionHandler? = null, configure: HttpRouterDsl.() -> Unit) 19 | } 20 | 21 | class HttpServerBuilder(private val vertx: Vertx, private val port: Int): HttpServerBuilderDsl { 22 | private val options = HttpServerOptions() 23 | private val filters = mutableListOf() 24 | private val routers = mutableListOf() 25 | var exceptionHandler: CoroutineExceptionHandler = defaultHttpExceptionHandler 26 | 27 | override fun options(configure: HttpServerOptions.() -> Unit) { 28 | configure(options) 29 | } 30 | 31 | override fun filter(filter: HttpServerFilter) { 32 | filters.add(filter) 33 | } 34 | 35 | override fun router(exceptionHandler: CoroutineExceptionHandler?, configure: HttpRouterDsl.() -> Unit) { 36 | routers.add(RouterDef(exceptionHandler, configure)) 37 | } 38 | 39 | fun build(): HttpServer { 40 | val filter = combineFilters(*filters.toTypedArray()) 41 | 42 | val vertxRouter = Router.router(vertx) 43 | for(router in routers) { 44 | val builder = HttpRouterBuilder(vertx, vertxRouter, filter, router.exceptionHandler ?: exceptionHandler) 45 | router.configure(builder) 46 | builder.build() 47 | } 48 | 49 | return HttpServer(vertx, port, options, vertxRouter) 50 | } 51 | } 52 | 53 | internal class ServerVerticle(private val port: Int, private val options: HttpServerOptions, private val router: Router) : AbstractVerticle() { 54 | private lateinit var server: VHttpServer 55 | 56 | private fun createServer(vertx: Vertx): VHttpServer { 57 | return vertx.createHttpServer(options) 58 | } 59 | 60 | override fun deploymentID(): String { 61 | return "kert-http" 62 | } 63 | 64 | override fun init(vertx: Vertx, context: Context) { 65 | server = createServer(vertx) 66 | server.requestHandler(router) 67 | } 68 | 69 | override fun start(startPromise: Promise) { 70 | server.listen(port) { ar -> 71 | if(ar.succeeded()) startPromise.complete() 72 | else startPromise.fail(ar.cause()) 73 | } 74 | } 75 | 76 | override fun stop(stopPromise: Promise) { 77 | server.close { ar -> 78 | if(ar.succeeded()) stopPromise.complete() 79 | else stopPromise.fail(ar.cause()) 80 | } 81 | } 82 | } 83 | 84 | class HttpServer(private val vertx: Vertx, private val port: Int, private val options: HttpServerOptions, private val router: Router) { 85 | private var deployId: String? = null 86 | 87 | suspend fun start() { 88 | if(deployId != null) return 89 | 90 | val desiredInstances = VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE 91 | val deploymentOptions = DeploymentOptions().setInstances(desiredInstances) 92 | deployId = vertx.deployVerticle({ ServerVerticle(port, options, router) }, deploymentOptions).await() 93 | } 94 | 95 | suspend fun stop() { 96 | deployId?.let { 97 | vertx.undeploy(it).await() 98 | deployId = null 99 | } 100 | } 101 | } 102 | 103 | fun httpServer(vertx: Vertx, port: Int, configure: HttpServerBuilderDsl.() -> Unit): HttpServer { 104 | val builder = HttpServerBuilder(vertx, port) 105 | configure(builder) 106 | return builder.build() 107 | } 108 | 109 | fun httpServer(port: Int, configure: HttpServerBuilderDsl.() -> Unit): HttpServer { 110 | val builder = HttpServerBuilder(Kert.vertx, port) 111 | configure(builder) 112 | return builder.build() 113 | } 114 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpServerRequest.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.MultiMap 4 | import io.vertx.core.Vertx 5 | import io.vertx.core.buffer.Buffer 6 | import io.vertx.core.http.HttpMethod 7 | import io.vertx.core.http.HttpServerRequest 8 | import io.vertx.core.http.HttpVersion 9 | import io.vertx.ext.web.RoutingContext 10 | import kotlinx.coroutines.flow.Flow 11 | 12 | class HttpServerRequest(private val underlying: HttpServerRequest, private val routingContext: RoutingContext): HttpRequest { 13 | private val context = Vertx.currentContext() ?: throw IllegalStateException("Request must be created on vertx context") 14 | 15 | override val method: HttpMethod = underlying.method() 16 | override val uri: String = underlying.uri() 17 | override val headers: MultiMap = underlying.headers() 18 | override val body: Flow = underlying.asFlow(context) 19 | 20 | val params: MultiMap = underlying.params() 21 | val path: String = underlying.path() 22 | val pathParams: MutableMap = routingContext.pathParams() 23 | val version: HttpVersion = underlying.version() 24 | } 25 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/HttpServerResponse.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.MultiMap 4 | import io.vertx.core.buffer.Buffer 5 | import io.vertx.core.http.HttpHeaders 6 | import io.vertx.core.http.impl.headers.HeadersMultiMap 7 | import kotlinx.coroutines.flow.Flow 8 | import kotlinx.coroutines.flow.emptyFlow 9 | import kotlinx.coroutines.flow.flowOf 10 | import java.lang.IllegalArgumentException 11 | 12 | data class HttpServerResponse internal constructor( 13 | override val statusCode: Int = 200, 14 | override val headers: MultiMap = HeadersMultiMap(), 15 | override val body: Flow = emptyFlow(), 16 | override val trailers: () -> MultiMap = { HeadersMultiMap() }): HttpResponse { 17 | } 18 | 19 | fun response(statusCode: Int = 200, headers: MultiMap? = null) = 20 | HttpServerResponse(statusCode, headers = headers ?: HeadersMultiMap()) 21 | 22 | fun response(statusCode: Int = 200, 23 | headers: MultiMap? = null, 24 | body: Any? = null, 25 | contentType: String? = null, 26 | contentLength: Long? = null, 27 | trailers: (() -> MultiMap)? = null): HttpServerResponse { 28 | val theHeaders = constructHeaders(headers, contentLength, body) 29 | contentType?.let { theHeaders[HttpHeaders.CONTENT_TYPE] = it } 30 | val theBody = body?.let { toFlow(it) } ?: emptyFlow() 31 | val theTrailers = trailers ?: { HeadersMultiMap() } 32 | return HttpServerResponse(statusCode, theHeaders, theBody, theTrailers) 33 | } 34 | 35 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/Kert.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Vertx 4 | import io.vertx.core.VertxOptions 5 | import io.vertx.kotlin.coroutines.await 6 | 7 | object Kert { 8 | internal val vertx by lazy { 9 | val options = VertxOptions() 10 | .setEventLoopPoolSize(VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE) 11 | Vertx.vertx(options) 12 | } 13 | 14 | suspend fun close() { 15 | vertx.close().await() 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/Message.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.MultiMap 4 | import io.vertx.core.buffer.Buffer 5 | import io.vertx.core.http.HttpHeaders 6 | import io.vertx.core.http.HttpMethod 7 | import kotlinx.coroutines.flow.* 8 | 9 | interface HttpMessage { 10 | val headers: MultiMap 11 | val body: Flow 12 | 13 | fun chunked(): Boolean = headers[HttpHeaders.TRANSFER_ENCODING] == "chunked" || contentLength() == null 14 | fun contentLength(): Long? = headers[HttpHeaders.CONTENT_LENGTH]?.toLong() 15 | 16 | fun header(name: String): String? = headers[name] 17 | 18 | suspend fun body(): Buffer { 19 | val buf = Buffer.buffer() 20 | body.collect { 21 | buf.appendBuffer(it) 22 | } 23 | return buf 24 | } 25 | } 26 | 27 | interface HttpRequest : HttpMessage { 28 | val method: HttpMethod 29 | val uri: String 30 | } 31 | 32 | interface HttpResponse: HttpMessage { 33 | val statusCode: Int 34 | val trailers: () -> MultiMap 35 | } 36 | 37 | 38 | internal fun toFlow(body: Any?): Flow { 39 | return when(body) { 40 | null -> emptyFlow() 41 | is Flow<*> -> body.map { toBuffer(it!!) } 42 | is ByteArray, is Buffer, is String -> flowOf(toBuffer(body)) 43 | else -> throw IllegalArgumentException("Unsupported data type ${body.javaClass.name}") 44 | } 45 | } 46 | 47 | internal fun toBuffer(data: Any): Buffer { 48 | return when(data) { 49 | is ByteArray -> Buffer.buffer(data) 50 | is Buffer -> data 51 | is String -> Buffer.buffer(data) 52 | else -> throw IllegalArgumentException("Unsupported data type ${data.javaClass.name}") 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/Stream.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Context 4 | import io.vertx.core.buffer.Buffer 5 | import io.vertx.core.streams.ReadStream 6 | import io.vertx.core.streams.WriteStream 7 | import io.vertx.kotlin.coroutines.await 8 | import kotlinx.coroutines.channels.Channel 9 | import kotlinx.coroutines.flow.Flow 10 | import kotlinx.coroutines.flow.collect 11 | import kotlinx.coroutines.flow.flow 12 | import mu.KotlinLogging 13 | 14 | private val logger = KotlinLogging.logger {} 15 | 16 | fun ReadStream.asFlow(context: Context): Flow { 17 | // return toChannel(context).consumeAsFlow() 18 | return toFlow(context) 19 | } 20 | 21 | /** 22 | * Another implementation of ReadStream to Flow, without launching a new coroutine for each send. 23 | * Throughput and latency seems slightly better (0.5%) 24 | * Causes io.vertx.core.VertxException: Connection was closed for GrpcBasicSpec "bidi stream" 25 | * Sending too fast at beginning because the buffered channel? 26 | */ 27 | fun ReadStream.toFlow(context: Context): Flow { 28 | pause() 29 | 30 | val channel = Channel(Channel.BUFFERED) 31 | handler { msg -> 32 | val result = channel.trySend(msg) 33 | if(!result.isSuccess) { 34 | throw IllegalStateException("Element $msg was not added to channel, result=$result") 35 | } 36 | } 37 | endHandler { 38 | channel.close(null) 39 | } 40 | exceptionHandler { exception -> 41 | channel.close(exception) 42 | } 43 | return flow { 44 | fetch(1) 45 | 46 | while(true) { 47 | val result = channel.receiveCatching() 48 | when { 49 | result.isClosed -> { 50 | break 51 | } 52 | else -> { 53 | val message = result.getOrThrow() 54 | emit(message) 55 | fetch(1) 56 | } 57 | } 58 | } 59 | } 60 | } 61 | 62 | suspend fun write(context: Context, body: Flow, stream: WriteStream) { 63 | body.collect { data -> 64 | logger.trace { "Sending to channel length=${data.length()}" } 65 | // TODO not efficient since waiting for message to be sent 66 | stream.write(data).await() 67 | } 68 | 69 | // val channel = stream.toChannel(context, Channel.RENDEZVOUS) 70 | // body.collect { data -> 71 | // logger.trace { "Sending to channel length=${data.length()}" } 72 | // channel.send(data) 73 | // } 74 | // channel.close() 75 | } 76 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/StreamChannel.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Copied from vertx-lang-kotlin-coroutines to fix compiling issue with latest Kotlin. 3 | */ 4 | 5 | package ws.leap.kert.http 6 | 7 | import io.vertx.core.Context 8 | import io.vertx.core.Vertx 9 | import io.vertx.core.buffer.Buffer 10 | import io.vertx.core.streams.ReadStream 11 | import io.vertx.core.streams.WriteStream 12 | import io.vertx.kotlin.coroutines.dispatcher 13 | import kotlinx.coroutines.CoroutineScope 14 | import kotlinx.coroutines.ExperimentalCoroutinesApi 15 | import kotlinx.coroutines.channels.Channel 16 | import kotlinx.coroutines.channels.ReceiveChannel 17 | import kotlinx.coroutines.channels.SendChannel 18 | import kotlinx.coroutines.launch 19 | import mu.KotlinLogging 20 | import kotlin.coroutines.CoroutineContext 21 | 22 | private val logger = KotlinLogging.logger {} 23 | 24 | private const val DEFAULT_CAPACITY = 16 25 | 26 | fun ReadStream.toChannel(context: Context): ReceiveChannel { 27 | this.pause() 28 | val ret = ChannelReadStream( 29 | stream = this, 30 | channel = Channel(0), 31 | context = context 32 | ) 33 | ret.subscribe() 34 | this.fetch(1) 35 | return ret 36 | } 37 | 38 | private class ChannelReadStream(val stream: ReadStream, 39 | val channel: Channel, 40 | context: Context 41 | ) : Channel by channel, CoroutineScope { 42 | 43 | override val coroutineContext: CoroutineContext = context.dispatcher() 44 | fun subscribe() { 45 | stream.endHandler { 46 | close() 47 | } 48 | stream.exceptionHandler { err -> 49 | close(err) 50 | } 51 | stream.handler { event -> 52 | launch { 53 | send(event) 54 | stream.fetch(1) 55 | } 56 | } 57 | } 58 | } 59 | 60 | @ExperimentalCoroutinesApi 61 | fun WriteStream.toChannel(vertx: Vertx, capacity: Int = DEFAULT_CAPACITY): SendChannel { 62 | return toChannel(vertx.getOrCreateContext(), capacity) 63 | } 64 | 65 | /** 66 | * Adapts the current write stream to Kotlin [SendChannel]. 67 | * 68 | * The channel can be used to write items, the coroutine is suspended when the stream is full 69 | * and resumed when the stream is drained. 70 | * 71 | * @param context the vertx context 72 | * @param capacity the channel buffering capacity 73 | */ 74 | @ExperimentalCoroutinesApi 75 | fun WriteStream.toChannel(context: Context, capacity: Int = DEFAULT_CAPACITY): SendChannel { 76 | val ret = ChannelWriteStream( 77 | stream = this, 78 | channel = Channel(capacity), 79 | context = context 80 | ) 81 | ret.subscribe() 82 | return ret 83 | } 84 | 85 | private class ChannelWriteStream(val stream: WriteStream, 86 | val channel: Channel, 87 | context: Context 88 | ) : Channel by channel, CoroutineScope { 89 | private var total = 0L 90 | override val coroutineContext: CoroutineContext = context.dispatcher() 91 | 92 | @ExperimentalCoroutinesApi 93 | fun subscribe() { 94 | stream.exceptionHandler { 95 | channel.close(it) 96 | } 97 | 98 | launch { 99 | while (true) { 100 | val elt = channel.receiveCatching().getOrNull() 101 | if (stream.writeQueueFull()) { 102 | logger.trace { "WriteStream.writeQueueFull" } 103 | stream.drainHandler { 104 | logger.trace { "WriteStream.drainHandler" } 105 | if (dispatch(elt)) { 106 | subscribe() 107 | } 108 | } 109 | break 110 | } else { 111 | if (!dispatch(elt)) { 112 | break 113 | } 114 | } 115 | } 116 | } 117 | } 118 | 119 | fun dispatch(elt: T?): Boolean { 120 | return if (elt != null) { 121 | total += size(elt) 122 | logger.trace { "WriteStream.write($total)" } 123 | stream.write(elt) 124 | true 125 | } else { 126 | close() 127 | false 128 | } 129 | } 130 | 131 | private fun size(elt: T?): Int { 132 | return when(elt) { 133 | is Buffer -> elt.length() 134 | else -> 0 135 | } 136 | } 137 | 138 | override fun close(cause: Throwable?): Boolean { 139 | val ret = channel.close(cause) 140 | if (ret) stream.end() 141 | return ret 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /kert-http/src/main/kotlin/ws/leap/kert/http/Types.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | typealias Handler = suspend (req: REQ) -> RESP 4 | typealias Filter = suspend (req: REQ, next: Handler) -> RESP 5 | 6 | fun withFilters(handler: Handler, vararg filters: Filter): Handler { 7 | if(filters.isEmpty()) return handler 8 | 9 | val combinedFilter = combineFilters(*filters)!! 10 | return { req: REQ -> 11 | combinedFilter(req, handler) 12 | } 13 | } 14 | 15 | fun Handler.filters(vararg filters: Filter): Handler { 16 | return withFilters(this, *filters) 17 | } 18 | 19 | fun filtered(handler: Handler, filter: Filter): Handler { 20 | return { req: REQ -> 21 | filter(req, handler) 22 | } 23 | } 24 | 25 | fun Handler.filter(filter: Filter): Handler { 26 | return filtered(this, filter) 27 | } 28 | 29 | /** 30 | * Combine the [filters] into one filter, with the order of inner to outer (last filter get called first). 31 | */ 32 | fun combineFilters(vararg filters: Filter): Filter? { 33 | if (filters.isEmpty()) return null 34 | 35 | return filters.reduce { left, right -> 36 | { req, next -> 37 | right(req) { left(it, next) } 38 | } 39 | } 40 | } 41 | 42 | /** 43 | * Combine filters by wrapping the [filter] on the [current] filter, which means the [filter] get called first, then the [current] filter. 44 | */ 45 | fun combineFilters(current: Filter?, filter: Filter?): Filter? { 46 | return current?.let { cur -> 47 | if (filter != null) { 48 | { req, next -> filter(req) { cur(it, next) } } 49 | } else { 50 | cur 51 | } 52 | } ?: filter 53 | } 54 | 55 | internal suspend fun handle(req: REQ, handler: Handler, filter: Filter?): RESP { 56 | return filter?.let { it(req, handler) } ?: handler(req) 57 | } 58 | 59 | typealias HttpClientHandler = Handler 60 | typealias HttpClientFilter = Filter 61 | 62 | typealias HttpServerHandler = Handler 63 | typealias HttpServerFilter = Filter 64 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/ClientServerSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.core.spec.Spec 5 | import io.kotest.core.spec.style.FunSpec 6 | import io.kotest.core.test.TestCaseOrder 7 | import io.vertx.core.Vertx 8 | 9 | interface TestServer { 10 | fun start() 11 | fun stop() 12 | } 13 | 14 | /** 15 | * Use different http clients (Vertx http client, web client, Kert http client) 16 | * to test with different servers (Vertx http server, http server in verticle, Kert http server) 17 | * with 4 different request patterns (unary, server streaming, client streaming, bidirectional streaming) 18 | * for cross reference 19 | * to make sure there is no compatibility issue and easier to identify bugs. 20 | */ 21 | @DoNotParallelize 22 | abstract class ClientServerSpec : FunSpec() { 23 | protected val vertx = Vertx.vertx() 24 | protected open val port: Int = 8500 25 | protected abstract val server: TestServer 26 | 27 | // TODO enable this when all problems fixed 28 | override fun testCaseOrder() = TestCaseOrder.Random 29 | 30 | init { 31 | beforeSpec { 32 | server.start() 33 | } 34 | afterSpec { 35 | server.stop() 36 | vertx.close() 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/HttpClientSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.style.FunSpec 4 | import io.kotest.matchers.shouldBe 5 | import io.vertx.core.Vertx 6 | import java.net.URL 7 | 8 | class HttpClientSpec : FunSpec() { 9 | private val client = httpClient(Vertx.vertx()) 10 | init { 11 | test("call with url") { 12 | val resp = client.get(URL("https://www.google.com")) 13 | resp.statusCode shouldBe 200 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/HttpFilterSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.Spec 4 | import io.kotest.core.spec.style.FunSpec 5 | import io.kotest.matchers.shouldBe 6 | import io.vertx.core.Vertx 7 | import io.vertx.core.http.HttpVersion 8 | import kotlinx.coroutines.runBlocking 9 | import mu.KotlinLogging 10 | 11 | class HttpFilterSpec : FunSpec() { 12 | private val vertx = Vertx.vertx() 13 | private val logger = KotlinLogging.logger {} 14 | private val server = httpServer(vertx,8550) { 15 | options { 16 | isSsl = false 17 | } 18 | 19 | router { 20 | // a filter to track response time 21 | filter { req, next -> 22 | val start = System.currentTimeMillis() 23 | val resp = next(req) 24 | val time = System.currentTimeMillis() - start 25 | logger.trace { "${req.path} server response time is $time millis" } 26 | resp 27 | } 28 | 29 | // request handler with it's own filter 30 | get("/ping", filtered({ 31 | response(body = "pong") 32 | }, { req, next -> 33 | logger.trace { "ping with it's own filter" } 34 | next(req) 35 | }) ) 36 | 37 | 38 | subRouter("/sub") { 39 | // a filter to verify the authentication header must be available 40 | filter { req, next -> 41 | if (req.headers["authentication"] == null) throw IllegalArgumentException("Authentication header is missing") 42 | next(req) 43 | } 44 | 45 | get("/ping") { 46 | response(body = "pong") 47 | } 48 | } 49 | } 50 | } 51 | 52 | private val client = httpClient(vertx) { 53 | options { 54 | defaultPort = 8550 55 | protocolVersion = HttpVersion.HTTP_2 56 | } 57 | 58 | // a filter to set authentication header in request 59 | filter { req, next -> 60 | req.headers["authentication"] = "mocked-authentication" 61 | next(req) 62 | } 63 | 64 | // a filter to measure client side response time 65 | filter { req, next -> 66 | val start = System.currentTimeMillis() 67 | val resp = next(req) 68 | val time = System.currentTimeMillis() - start 69 | logger.trace { "${req.uri} client response time is $time millis" } 70 | resp 71 | } 72 | } 73 | 74 | // a client doesn't have authentication header injected 75 | private val clientNoAuth = httpClient(vertx) { 76 | options { 77 | defaultPort = 8550 78 | } 79 | } 80 | 81 | init { 82 | beforeSpec { 83 | server.start() 84 | } 85 | 86 | afterSpec { 87 | server.stop() 88 | } 89 | 90 | context("filter on sub router") { 91 | test("/sub/ping works with authentication header") { 92 | val resp = client.get("/sub/ping") 93 | resp.statusCode shouldBe 200 94 | resp.body().toString() shouldBe "pong" 95 | } 96 | 97 | test("/sub/ping works with authentication header from filter") { 98 | val filteredClient = clientNoAuth.withFilter { req, next -> 99 | req.headers["authentication"] = "mocked-authentication" 100 | next(req) 101 | } 102 | val resp = filteredClient.get("/sub/ping") 103 | resp.statusCode shouldBe 200 104 | resp.body().toString() shouldBe "pong" 105 | } 106 | 107 | test("/sub/ping fails without authentication header") { 108 | val resp = clientNoAuth.get("/sub/ping") 109 | resp.statusCode shouldBe 500 110 | } 111 | } 112 | 113 | test("/ping doesn't require authentication header") { 114 | val resp = clientNoAuth.get("/ping") 115 | resp.statusCode shouldBe 200 116 | resp.body().toString() shouldBe "pong" 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/KertClientSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.matchers.shouldBe 5 | import io.vertx.core.buffer.Buffer 6 | import io.vertx.core.http.HttpVersion 7 | import kotlinx.coroutines.delay 8 | import kotlinx.coroutines.flow.collect 9 | import kotlinx.coroutines.flow.flow 10 | import mu.KotlinLogging 11 | 12 | private val logger = KotlinLogging.logger {} 13 | 14 | /** 15 | * Use Kert http client to test with different servers 16 | */ 17 | abstract class KertClientSpec : ClientServerSpec() { 18 | private val client by lazy { 19 | httpClient(vertx) { 20 | options { 21 | protocolVersion = HttpVersion.HTTP_2 22 | defaultPort = port 23 | isHttp2ClearTextUpgrade = false 24 | } 25 | } 26 | } 27 | 28 | init { 29 | test("ping pong") { 30 | val resp = client.get("/ping") 31 | resp.statusCode shouldBe 200 32 | resp.body().toString() shouldBe "pong" 33 | } 34 | 35 | test("server stream") { 36 | val resp = client.get("/server-stream") 37 | var total = 0L 38 | resp.body.collect { data -> 39 | delay(1) 40 | total += data.length() 41 | } 42 | println("received data $total bytes") 43 | } 44 | 45 | // TODO this test fails when running it only with KertServer 46 | // but it doesn't fail when running with other tests in the spec 47 | // or running with VertxServer or VertxVerticleServer 48 | // so looks like it's a problem with KertServer to serve first request 49 | test("client stream") { 50 | val body = flow { 51 | for (i in 0 until 500) { 52 | // delay(10) 53 | emit(Buffer.buffer(ByteArray(8 * 1024))) 54 | } 55 | } 56 | val resp = client.post("/client-stream", body) 57 | resp.statusCode shouldBe 200 58 | resp.body().toString(Charsets.UTF_8) shouldBe "${500 * 8 * 1024}" 59 | } 60 | 61 | test("bidi stream") { 62 | val body = flow { 63 | for (i in 0 until 500) { 64 | emit(Buffer.buffer(ByteArray(8 * 1024))) 65 | } 66 | } 67 | 68 | val resp = client.post("/bidi-stream", body) 69 | 70 | var total = 0L 71 | resp.body.collect { data -> 72 | total += data.length() 73 | logger.trace { "received data, total=$total" } 74 | } 75 | } 76 | } 77 | } 78 | 79 | @DoNotParallelize 80 | class KertClientVertxServerSpec : KertClientSpec() { 81 | override val server = createVertxServer(vertx, port) 82 | } 83 | 84 | @DoNotParallelize 85 | class KertClientVertxVerticleServerSpec : KertClientSpec() { 86 | override val server = createVertxVerticleServer(vertx, port) 87 | } 88 | 89 | @DoNotParallelize 90 | class KertClientKertServerSpec : KertClientSpec() { 91 | override val server = createKertServer(vertx, port) 92 | } 93 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/KertTestServer.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Vertx 4 | import io.vertx.core.buffer.Buffer 5 | import kotlinx.coroutines.delay 6 | import kotlinx.coroutines.flow.collect 7 | import kotlinx.coroutines.flow.flow 8 | import kotlinx.coroutines.flow.map 9 | import kotlinx.coroutines.runBlocking 10 | import mu.KotlinLogging 11 | 12 | private fun createHttpServer(vertx: Vertx, port: Int): HttpServer = httpServer(vertx, port) { 13 | options { 14 | isUseAlpn = true 15 | isSsl = false 16 | } 17 | val logger = KotlinLogging.logger {} 18 | 19 | // global filter 20 | filter { req, next -> 21 | logger.trace { "request ${req.path} in filter1" } 22 | val resp = next(req) 23 | logger.trace { "response for $resp in filter1" } 24 | resp 25 | } 26 | 27 | router { 28 | filter { req, next -> 29 | logger.trace { "request ${req.path} in filter2" } 30 | val resp = next(req) 31 | logger.trace { "response for $resp in filter2" } 32 | resp 33 | } 34 | 35 | get("/ping") { 36 | val data = Buffer.buffer("pong".toByteArray()) 37 | response(body = data) 38 | } 39 | 40 | get("/server-stream") { 41 | var total = 0L 42 | val data = flow { 43 | for(i in 0 until 500) { 44 | val buf = Buffer.buffer(ByteArray(8 * 1024)) 45 | total += buf.length() 46 | emit(buf) 47 | logger.trace { "sent data, total=$total" } 48 | } 49 | } 50 | 51 | response(body = data) 52 | } 53 | 54 | post("/client-stream") { req -> 55 | var total = 0L 56 | req.body.collect { data -> 57 | total += data.length() 58 | delay(1) 59 | logger.trace { "received data, total=$total" } 60 | } 61 | 62 | response(body = total.toString()) 63 | } 64 | 65 | post("/bidi-stream") { req -> 66 | var total = 0L 67 | val data = req.body.map { data -> 68 | total += data.length() 69 | delay(1) 70 | logger.trace { "received data, total=$total" } 71 | data 72 | } 73 | 74 | response(body = data) 75 | } 76 | 77 | subRouter("/sub") { 78 | filter { req, next -> 79 | logger.trace { "request ${req.path} in sub filter" } 80 | val resp = next(req) 81 | logger.trace { "response for $resp in sub filter" } 82 | resp 83 | } 84 | 85 | get("/hello") { 86 | response(body ="world") 87 | } 88 | } 89 | } 90 | } 91 | 92 | fun createKertServer(vertx: Vertx, port: Int): TestServer { 93 | val server = createHttpServer(vertx, port) 94 | return object: TestServer { 95 | override fun start() { 96 | runBlocking { 97 | server.start() 98 | } 99 | } 100 | 101 | override fun stop() { 102 | runBlocking { 103 | server.stop() 104 | } 105 | } 106 | } 107 | } 108 | 109 | fun main() = runBlocking { 110 | val server = createHttpServer(Vertx.vertx(), 8000) 111 | server.start() 112 | } 113 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/MockReadStream.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.Handler 4 | import io.vertx.core.Vertx 5 | import io.vertx.core.buffer.Buffer 6 | import io.vertx.core.streams.ReadStream 7 | import mu.KotlinLogging 8 | import kotlin.math.min 9 | 10 | private val logger = KotlinLogging.logger {} 11 | 12 | class MockReadStream(vertx: Vertx, size: Int) : ReadStream { 13 | private val timer = vertx.periodicStream(1) 14 | private var remaining = size 15 | private val chunkSize = 8 * 1024 16 | private var endHandler: Handler? = null 17 | private var readTotal = 0 18 | 19 | override fun exceptionHandler(handler: Handler?): ReadStream { 20 | timer.exceptionHandler(handler) 21 | return this 22 | } 23 | 24 | override fun handler(handler: Handler?): ReadStream { 25 | if(handler != null) { 26 | timer.handler { 27 | val size = min(remaining, chunkSize) 28 | val bytes = ByteArray(size) 29 | remaining -= size 30 | readTotal += size 31 | try { 32 | logger.trace { "read data total=${readTotal}, remaining=${remaining}" } 33 | handler.handle(Buffer.buffer(bytes)) 34 | } catch (t: Throwable) { 35 | logger.error(t) { "Error when handling data" } 36 | } 37 | 38 | if(remaining == 0) { 39 | timer.cancel() 40 | logger.trace("end") 41 | endHandler?.handle(null) 42 | } 43 | } 44 | } else { 45 | timer.handler(null) 46 | } 47 | return this 48 | } 49 | 50 | override fun pause(): ReadStream { 51 | logger.trace("pause") 52 | timer.pause() 53 | return this 54 | } 55 | 56 | override fun resume(): ReadStream { 57 | logger.trace("resume") 58 | timer.resume() 59 | return this 60 | } 61 | 62 | override fun fetch(amount: Long): ReadStream { 63 | logger.trace("fetch amount=${amount}") 64 | timer.fetch(amount) 65 | return this 66 | } 67 | 68 | override fun endHandler(endHandler: Handler?): ReadStream { 69 | this.endHandler = endHandler 70 | return this 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/MockWriteStream.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.AsyncResult 4 | import io.vertx.core.Future 5 | import io.vertx.core.Handler 6 | import io.vertx.core.buffer.Buffer 7 | import io.vertx.core.impl.future.FailedFuture 8 | import io.vertx.core.impl.future.SucceededFuture 9 | import io.vertx.core.streams.WriteStream 10 | import mu.KotlinLogging 11 | 12 | private val logger = KotlinLogging.logger {} 13 | 14 | class MockWriteStream(private val size: Int) : WriteStream { 15 | private var writtenTotal = 0 16 | override fun exceptionHandler(handler: Handler?): WriteStream { 17 | return this 18 | } 19 | 20 | override fun write(data: Buffer): Future { 21 | writtenTotal += data.length() 22 | logger.trace { "Write data total=${writtenTotal}" } 23 | return SucceededFuture(null) 24 | } 25 | 26 | override fun write(data: Buffer, handler: Handler>) { 27 | writtenTotal += data.length() 28 | logger.trace { "Write data total=${writtenTotal}" } 29 | handler.handle(SucceededFuture(null)) 30 | } 31 | 32 | override fun end(handler: Handler>) { 33 | logger.trace { "end" } 34 | if(writtenTotal == size) { 35 | handler.handle(SucceededFuture(null)) 36 | } else { 37 | handler.handle(FailedFuture("Expected size $size, actual $writtenTotal")) 38 | } 39 | } 40 | 41 | override fun setWriteQueueMaxSize(maxSize: Int): WriteStream { 42 | return this 43 | } 44 | 45 | override fun writeQueueFull(): Boolean { 46 | return false 47 | } 48 | 49 | override fun drainHandler(handler: Handler?): WriteStream { 50 | return this 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/TestConfig.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.config.AbstractProjectConfig 4 | 5 | object ProjectConfig : AbstractProjectConfig() { 6 | override val parallelism = 4 7 | } 8 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/VertxClientSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.matchers.shouldBe 5 | import io.vertx.core.buffer.Buffer 6 | import io.vertx.core.http.* 7 | import io.vertx.kotlin.coroutines.await 8 | import mu.KotlinLogging 9 | 10 | 11 | private val logger = KotlinLogging.logger {} 12 | 13 | /** 14 | * Use Vertx http client to test with different servers 15 | */ 16 | abstract class VertxClientSpec : ClientServerSpec() { 17 | private val client by lazy { 18 | val options = HttpClientOptions() 19 | .setDefaultPort(port) 20 | .setProtocolVersion(HttpVersion.HTTP_2) 21 | // FIXME set it to true cause the error below for bidi stream test 22 | // https://github.com/netty/netty/issues/7079 23 | // Max content exceeded 65536 bytes. 24 | // io.netty.handler.codec.TooLongFrameException: Max content exceeded 65536 bytes. 25 | // at io.vertx.core.http.impl.Http2UpgradedClientConnection$UpgradingStream$2.channelRead(Http2UpgradedClientConnection.java:257) 26 | .setHttp2ClearTextUpgrade(false) 27 | 28 | vertx.createHttpClient(options) 29 | } 30 | 31 | init { 32 | test("ping pong") { 33 | client.request(HttpMethod.GET, "/ping").flatMap { req -> 34 | req.end() 35 | req.response().flatMap { resp -> 36 | resp.statusCode() shouldBe 200 37 | resp.body().map { body -> 38 | body.toString(Charsets.UTF_8) shouldBe "pong" 39 | } 40 | } 41 | }.await() 42 | } 43 | 44 | // TODO this test fails when running it only with KertServer 45 | test("client stream") { 46 | client.request(HttpMethod.POST, "/client-stream").flatMap { req -> 47 | req.isChunked = true 48 | val stream = MockReadStream(vertx, streamSize) 49 | stream.pipe().to(req).flatMap { 50 | req.end() 51 | req.response().flatMap { resp -> 52 | resp.statusCode() shouldBe 200 53 | resp.body().map { body -> 54 | body.toString(Charsets.UTF_8) shouldBe "$streamSize" 55 | } 56 | } 57 | } 58 | }.await() 59 | } 60 | 61 | test("client stream no backpressure") { 62 | client.request(HttpMethod.POST, "/client-stream").flatMap { req -> 63 | req.isChunked = true 64 | val stream = MockReadStream(vertx, streamSize) 65 | stream.pipe().to(req).flatMap { 66 | var sentTotal = 0L 67 | for(i in 0 until 500) { 68 | sentTotal += 8 * 1024 69 | logger.trace { "req.write($sentTotal)" } 70 | req.write(Buffer.buffer(ByteArray(8 * 1024))) 71 | } 72 | req.end() 73 | req.response().flatMap { resp -> 74 | resp.statusCode() shouldBe 200 75 | resp.body().map { body -> 76 | body.toString(Charsets.UTF_8) shouldBe "$streamSize" 77 | } 78 | } 79 | } 80 | }.await() 81 | } 82 | 83 | test("server stream") { 84 | client.request(HttpMethod.GET, "/server-stream").flatMap { req -> 85 | req.end() 86 | req.response().flatMap { resp -> 87 | resp.pipe().to(MockWriteStream(streamSize)) 88 | } 89 | }.await() 90 | } 91 | 92 | test("bidi stream") { 93 | client.request(HttpMethod.POST, "/bidi-stream").flatMap { req -> 94 | req.isChunked = true 95 | val stream = MockReadStream(vertx, streamSize) 96 | stream.pipe().to(req).map { 97 | req.end() 98 | } 99 | req.response().flatMap { resp -> 100 | resp.pipe().to(MockWriteStream(streamSize)) 101 | } 102 | }.await() 103 | } 104 | } 105 | } 106 | 107 | @DoNotParallelize 108 | class VertxClientVertxServerSpec : VertxClientSpec() { 109 | override val server = createVertxServer(vertx, port) 110 | } 111 | 112 | @DoNotParallelize 113 | class VertxClientVertxVerticleServerSpec : VertxClientSpec() { 114 | override val server = createVertxVerticleServer(vertx, port) 115 | } 116 | 117 | @DoNotParallelize 118 | class VertxClientKertServerSpec : VertxClientSpec() { 119 | override val server = createKertServer(vertx, port) 120 | } 121 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/VertxTestServer.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.vertx.core.* 4 | import io.vertx.core.http.HttpServer 5 | import io.vertx.core.http.HttpServerOptions 6 | import io.vertx.core.streams.Pump 7 | import io.vertx.ext.web.Router 8 | import io.vertx.kotlin.coroutines.await 9 | import kotlinx.coroutines.runBlocking 10 | import mu.KotlinLogging 11 | 12 | // 500 loops with 8K for each message 13 | const val streamSize = 4000 * 1024 14 | 15 | private val logger = KotlinLogging.logger {} 16 | 17 | private fun createHttpServer(vertx: Vertx): HttpServer { 18 | val options = HttpServerOptions() 19 | .setSsl(false) 20 | .setUseAlpn(true) 21 | val server = vertx.createHttpServer(options) 22 | 23 | val router = Router.router(vertx) 24 | 25 | router.get("/ping").handler { ctx -> 26 | ctx.response().send("pong") 27 | } 28 | 29 | router.get("/server-stream").handler { ctx -> 30 | val resp = ctx.response() 31 | resp.isChunked = true 32 | val stream = MockReadStream(vertx, streamSize) 33 | stream.pipe().to(resp).onComplete { ar -> 34 | if(!ar.succeeded()) { 35 | resp.close() 36 | } 37 | } 38 | } 39 | 40 | router.post("/client-stream").handler { ctx -> 41 | val req = ctx.request() 42 | val resp = ctx.response() 43 | var receivedTotal = 0L 44 | 45 | req.exceptionHandler { e -> 46 | logger.error(e) { "client-stream error" } 47 | resp.statusCode = 500 48 | resp.end(e.message) 49 | } 50 | 51 | req.endHandler { 52 | logger.trace("received total $receivedTotal bytes") 53 | resp.end("$receivedTotal") 54 | } 55 | 56 | req.handler { buf -> 57 | logger.trace{ "received ${buf.length()} bytes" } 58 | receivedTotal += buf.length() 59 | // simulate a slow server 60 | Thread.sleep(1) 61 | } 62 | } 63 | 64 | router.post("/bidi-stream").handler { ctx -> 65 | val resp = ctx.response() 66 | resp.isChunked = true 67 | ctx.request().endHandler { 68 | resp.end() 69 | } 70 | val pump = Pump.pump(ctx.request(), resp) 71 | pump.start() 72 | } 73 | 74 | server.requestHandler(router) 75 | return server 76 | } 77 | 78 | fun createVertxServer(vertx: Vertx, port: Int): TestServer { 79 | val server = createHttpServer(vertx) 80 | return object: TestServer { 81 | override fun start() { 82 | server.listen(port) 83 | } 84 | 85 | override fun stop() { 86 | server.close() 87 | } 88 | } 89 | } 90 | 91 | fun createVertxVerticleServer(vertx: Vertx, port: Int): TestServer { 92 | class ServerVerticle: AbstractVerticle() { 93 | private lateinit var server: HttpServer 94 | override fun init(vertx: Vertx, context: Context) { 95 | server = createHttpServer(vertx) 96 | } 97 | 98 | override fun start() { 99 | server.listen(port) 100 | } 101 | 102 | override fun stop() { 103 | server.close() 104 | } 105 | } 106 | 107 | return object: TestServer { 108 | private var deployId: String? = null 109 | 110 | override fun start() { 111 | if(deployId != null) return 112 | runBlocking { 113 | deployId = vertx.deployVerticle( { ServerVerticle() }, DeploymentOptions().setInstances(VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE)).await() 114 | } 115 | } 116 | 117 | override fun stop() { 118 | if(deployId == null) return 119 | runBlocking { 120 | vertx.undeploy(deployId).await() 121 | deployId = null 122 | } 123 | } 124 | } 125 | } 126 | 127 | fun main() { 128 | val vertx = Vertx.vertx() 129 | val server = createVertxVerticleServer(vertx, 8000) 130 | server.start() 131 | } 132 | -------------------------------------------------------------------------------- /kert-http/src/test/kotlin/ws/leap/kert/http/VertxWebClientSpec.kt: -------------------------------------------------------------------------------- 1 | package ws.leap.kert.http 2 | 3 | import io.kotest.core.spec.DoNotParallelize 4 | import io.kotest.matchers.shouldBe 5 | import io.vertx.core.http.* 6 | import io.vertx.ext.web.client.WebClient 7 | import io.vertx.ext.web.client.WebClientOptions 8 | import io.vertx.ext.web.codec.BodyCodec 9 | import io.vertx.kotlin.coroutines.await 10 | 11 | /** 12 | * Use Vertx web client to test with different servers 13 | */ 14 | abstract class VertxWebClientSpec : ClientServerSpec() { 15 | private val client by lazy { 16 | val options = WebClientOptions() 17 | .setDefaultPort(port) 18 | .setProtocolVersion(HttpVersion.HTTP_2) 19 | .setHttp2ClearTextUpgrade(false) 20 | WebClient.create(vertx, options) 21 | } 22 | 23 | init { 24 | test("ping pong") { 25 | val future = client.get("/ping") 26 | .send() 27 | .onSuccess { resp -> 28 | val result = resp.body().bytes.toString(Charsets.UTF_8) 29 | result shouldBe "pong" 30 | } 31 | future.await() 32 | } 33 | 34 | test("client stream") { 35 | val future = client.post("/client-stream") 36 | .sendStream(MockReadStream(vertx, streamSize)) 37 | .onSuccess { resp -> 38 | val result = resp.body().bytes.toString(Charsets.UTF_8) 39 | result shouldBe "$streamSize" 40 | } 41 | future.await() 42 | } 43 | 44 | test("server stream") { 45 | val future = client.get("/server-stream") 46 | .`as`(BodyCodec.pipe(MockWriteStream(streamSize))) 47 | .send() 48 | .onSuccess { resp -> 49 | resp.statusCode() shouldBe 200 50 | } 51 | future.await() 52 | } 53 | 54 | test("bidi stream") { 55 | val future = client.post("/bidi-stream") 56 | .`as`(BodyCodec.pipe(MockWriteStream(streamSize))) 57 | .sendStream(MockReadStream(vertx, streamSize)) 58 | .onSuccess { resp -> 59 | resp.statusCode() shouldBe 200 60 | } 61 | future.await() 62 | } 63 | } 64 | } 65 | 66 | @DoNotParallelize 67 | class VertxWebClientVertxServerSpec : VertxWebClientSpec() { 68 | override val server = createVertxServer(vertx, port) 69 | } 70 | 71 | @DoNotParallelize 72 | class VertxWebClientVertxVerticleServerSpec : VertxWebClientSpec() { 73 | override val server = createVertxVerticleServer(vertx, port) 74 | } 75 | 76 | @DoNotParallelize 77 | class VertxWebClientKertServerSpec : VertxWebClientSpec() { 78 | override val server = createKertServer(vertx, port) 79 | } 80 | -------------------------------------------------------------------------------- /kert-http/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wsleap/kert/e2bbded0d3b177868cadc50f922361a0dc423d68/logo.png -------------------------------------------------------------------------------- /publish.md: -------------------------------------------------------------------------------- 1 | ## Publish 2 | Publish to Maven Central 3 | 4 | ### You must have these properties in your '~/.gradle/gradle.properties' 5 | ```properties 6 | ossrhUsername= 7 | ossrhPassword= 8 | signing.keyId= 9 | signing.password= 10 | signing.secretKeyRingFile= ex. "/home//.gnupg/secring.gpg" 11 | ``` 12 | 13 | ### Register project / package (one time setup) 14 | Create JIRA ticket at https://issues.sonatype.org/ 15 | * Project: Community Support - Open Source Project Repository Hosting (OSSRH) 16 | * Issue Type: New Project 17 | * Group Id: ws.leap.kert 18 | * Project URL: https://github.com/wsleap/kert 19 | * SCM url: https://github.com/wsleap/kert.git 20 | 21 | Ticket: https://issues.sonatype.org/browse/OSSRH-62510 22 | 23 | ### Publish to Sonatype 24 | 1. Update version number (remove SNAPSHOT) 25 | 1. Publish to staging area 26 | ```shell 27 | # Linux 64bit 28 | gradle clean :kert-grpc-compiler:publish -PtargetOs=linux -PtargetArch=x86_64 29 | # Windows 64bit 30 | gradle clean :kert-grpc-compiler:publish -PtargetOs=windows -PtargetArch=x86_64 31 | # Mac Intel 32 | gradle clean :kert-grpc-compiler:publish -PtargetOs=osx -PtargetArch=x86_64 33 | # Mac M1/M2 34 | gradle clean :kert-grpc-compiler:publish -PtargetOs=osx -PtargetArch=aarch_64 35 | gradle :kert-http:publish 36 | gradle :kert-grpc:publish 37 | gradle :kert-graphql:publish 38 | ``` 39 | 1. Bump version number (add SNAPSHOT back) 40 | 41 | Use this to check file format for the compiler 42 | ```shell 43 | file kert-grpc-compiler/build/exe/protoc-gen-grpc-kert 44 | ``` 45 | 46 | ### Promote to Maven Central 47 | * Go to https://oss.sonatype.org/#stagingRepositories 48 | * Close the staging repository if there is no problem. 49 | * Release the repository if close succeeded. 50 | -------------------------------------------------------------------------------- /publish.sh: -------------------------------------------------------------------------------- 1 | ./gradlew clean :kert-grpc-compiler:publish -PtargetOs=linux -PtargetArch=x86_64 2 | ./gradlew clean :kert-grpc-compiler:publish -PtargetOs=windows -PtargetArch=x86_64 3 | ./gradlew clean :kert-grpc-compiler:publish -PtargetOs=osx -PtargetArch=x86_64 4 | ./gradlew :kert-http:publish 5 | ./gradlew :kert-grpc:publish 6 | ./gradlew :kert-graphql:publish 7 | 8 | echo Artifacts are uploaded to Sonatype. 9 | echo Go to https://oss.sonatype.org/#stagingRepositories to release the artifacts to Maven Central. 10 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "kert" 2 | 3 | include(":kert-http") 4 | include(":kert-grpc") 5 | include(":kert-grpc-compiler") 6 | include(":kert-graphql") 7 | --------------------------------------------------------------------------------