-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.kt
More file actions
61 lines (54 loc) · 2.27 KB
/
Copy pathApp.kt
File metadata and controls
61 lines (54 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import com.linecorp.armeria.server.Server
import com.linecorp.armeria.server.docs.DocService
import com.linecorp.armeria.server.grpc.GrpcService
import io.grpc.BindableService
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import kotlinx.serialization.protobuf.ProtoBuf
import sample.grpc.AddRequest
import sample.grpc.AddResponse
import sample.grpc.SampleService
import sample.grpc.SampleServiceGrpcKt
import sample.grpc.SampleServiceOuterClass
import sample.grpc.SayHelloRequest
import sample.grpc.SayHelloResponse
class SampleServiceImpl : SampleService() {
override suspend fun sayHello(sayHelloRequest: SayHelloRequest): SayHelloResponse {
return SayHelloResponse(
message = "Hello, ${sayHelloRequest.name}!",
)
}
override suspend fun add(addRequest: AddRequest): AddResponse {
return AddResponse(
result = (addRequest.a ?: 0) + (addRequest.b ?: 0),
)
}
}
fun SampleService.asBindable(): BindableService {
return object : SampleServiceGrpcKt.SampleServiceCoroutineImplBase() {
override suspend fun add(request: SampleServiceOuterClass.AddRequest): SampleServiceOuterClass.AddResponse {
val requestKt = ProtoBuf.decodeFromByteArray<AddRequest>(request.toByteArray())
val responseBinary = ProtoBuf.encodeToByteArray(this@asBindable.add(requestKt))
return SampleServiceOuterClass.AddResponse.parseFrom(responseBinary)
}
override suspend fun sayHello(request: SampleServiceOuterClass.SayHelloRequest): SampleServiceOuterClass.SayHelloResponse {
val requestKt = ProtoBuf.decodeFromByteArray<SayHelloRequest>(request.toByteArray())
val responseBinary = ProtoBuf.encodeToByteArray(this@asBindable.sayHello(requestKt))
return SampleServiceOuterClass.SayHelloResponse.parseFrom(responseBinary)
}
}
}
fun main() {
val grpcService =
GrpcService.builder()
.addService(SampleServiceImpl().asBindable())
.enableUnframedRequests(true)
.build()
val server =
Server.builder()
.http(9000)
.service(grpcService)
.serviceUnder("/docs", DocService())
.build()
server.start().join()
}