-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup grpc context, slim service method
- Loading branch information
Showing
7 changed files
with
62 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/com/stabledata/context/GrpcContextInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.stabledata.context | ||
|
||
import io.grpc.* | ||
import io.ktor.http.* | ||
|
||
class GrpcContextInterceptor : ServerInterceptor { | ||
override fun <ReqT, RespT> interceptCall( | ||
call: ServerCall<ReqT, RespT>, | ||
headers: Metadata?, | ||
next: ServerCallHandler<ReqT, RespT>? | ||
): ServerCall.Listener<ReqT> { | ||
|
||
val tokenKey = Metadata.Key.of(HttpHeaders.Authorization, Metadata.ASCII_STRING_MARSHALLER) | ||
val eventIdKey = Metadata.Key.of(StableEventIdHeader, Metadata.ASCII_STRING_MARSHALLER) | ||
val eventCreatedKey = Metadata.Key.of(StableEventCreatedOnHeader, Metadata.ASCII_STRING_MARSHALLER) | ||
|
||
val token: String? = headers?.get(tokenKey) | ||
val eventId: String? = headers?.get(eventIdKey) | ||
val eventCreatedAt: Long? = headers?.get(eventCreatedKey)?.toLong() | ||
|
||
val context: Context = Context.current() | ||
.withValue(tokenContext, token) | ||
.withValue(eventIdContext, eventId) | ||
.withValue(eventCreatedAtContext, eventCreatedAt) | ||
|
||
return Contexts.interceptCall(context, call, headers, next) | ||
} | ||
|
||
companion object { | ||
val tokenContext: Context.Key<Any> = Context.key(HttpHeaders.Authorization) | ||
val eventIdContext: Context.Key<Any> = Context.key(StableEventIdHeader) | ||
val eventCreatedAtContext: Context.Key<Any> = Context.key(StableEventCreatedOnHeader) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.stabledata.grpc | ||
|
||
import com.stabledata.context.WriteRequestContext | ||
import com.stabledata.model.Collection | ||
import com.stabledata.model.toMessage | ||
import com.stabledata.workload.schema.createCollectionWorkload | ||
import io.grpc.stub.StreamObserver | ||
import stable.LogEntry.LogEntryMessage | ||
import stable.Schema | ||
import stable.SchemaServiceGrpc | ||
|
||
|
||
class SchemaService : SchemaServiceGrpc.SchemaServiceImplBase() { | ||
override fun createCollection( | ||
request: Schema.CollectionRequest, | ||
responseObserver: StreamObserver<LogEntryMessage> | ||
) { | ||
val ctx = WriteRequestContext.fromGrpcContext { | ||
Collection.fromMessage(request) | ||
} | ||
val result = createCollectionWorkload(ctx) | ||
val response = result.toMessage() | ||
responseObserver.onNext(response) | ||
responseObserver.onCompleted() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters